Buncha changes I CBA
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
'use server'
|
||||
|
||||
import { Model } from "sequelize";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
import { Auth, Post, User } from "@/model/sequelize/NewModels";
|
||||
|
||||
|
||||
import { APIError} from "@/api/error"
|
||||
import { UserAuth, parseBasicAuth, getAssociatedUser } from "@/api/user"
|
||||
|
||||
|
||||
async function tryAuth(request:Request){
|
||||
|
||||
// await User.sync();
|
||||
await Auth.sync();
|
||||
|
||||
const auth:string|null = request.headers.get("authorization");
|
||||
|
||||
if(!auth){
|
||||
return new Response("unauthorized",{status:403});
|
||||
}
|
||||
|
||||
const userAuth = parseBasicAuth(auth);
|
||||
const user = await getAssociatedUser(userAuth);
|
||||
|
||||
if (!user || !user.id)
|
||||
return new Response("error",{status:500});
|
||||
|
||||
const authentication = await Auth.create({
|
||||
user_id: user.id,
|
||||
})
|
||||
|
||||
const foundAuth = await Auth.findOne({
|
||||
include: 'user',
|
||||
where: {
|
||||
user_id: user.id
|
||||
}
|
||||
})
|
||||
if(!foundAuth)
|
||||
return new Response("error",{status:500});
|
||||
|
||||
|
||||
const kanker = await authentication.getUser();
|
||||
|
||||
// @ts-ignore
|
||||
cookies().set('auth', JSON.stringify(authentication));
|
||||
|
||||
return new Response(
|
||||
JSON.stringify(
|
||||
{
|
||||
credentials: userAuth,
|
||||
auth: authentication,
|
||||
kanker: kanker,
|
||||
foundAuth: foundAuth
|
||||
}),
|
||||
{
|
||||
status: 200,
|
||||
headers:{
|
||||
"Content-Type": "text/JSON"
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
export async function POST(request:Request){
|
||||
try{
|
||||
return await tryAuth(request);
|
||||
}
|
||||
catch(e){
|
||||
if (e instanceof APIError){
|
||||
return new Response(e.info.responseText,{status:e.info.status});
|
||||
}
|
||||
else{
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user