commit working state
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
'use server'
|
||||
|
||||
import { constructAPIUrl } from "@/util/Utils"
|
||||
// import { signIn } from '@/auth'
|
||||
import { cookies } from "next/headers"
|
||||
import { parseSetCookie } from "@/util/parseSetCookie";
|
||||
import makeFetchCookie from 'fetch-cookie';
|
||||
|
||||
type LoginReturn = {
|
||||
cookie?:unknown,
|
||||
errorMessage?:string;
|
||||
}
|
||||
|
||||
async function signIn(method:string,b:FormData):Promise<LoginReturn|null>
|
||||
{
|
||||
console.log("form data:");
|
||||
console.log(b);
|
||||
|
||||
if(!b || !b.get('input_username') || !b.get('input_password')) return null;
|
||||
let headers:Headers = new Headers();
|
||||
const fetchCookie = makeFetchCookie(fetch)
|
||||
headers.set('Authorization', 'Basic ' + Buffer.from(b.get('input_username') + ":" + b.get('input_password')).toString('base64'));
|
||||
let res = await fetchCookie(constructAPIUrl("auth"), {
|
||||
method:'POST',
|
||||
credentials: 'include',
|
||||
headers:headers,
|
||||
});
|
||||
|
||||
// if(res.headers.get('set-coo')) console.log(res.headers['set-cookie'])
|
||||
let koek = res.headers.getSetCookie();
|
||||
// console.log("api koek:" + koek.map((k)=>decodeURIComponent(k)));
|
||||
let cookieDict = parseSetCookie(koek);
|
||||
console.log("testing cookiedict")
|
||||
console.log(cookieDict);
|
||||
await cookies().set('auth', cookieDict.auth);
|
||||
return {
|
||||
cookie:cookieDict.auth,
|
||||
errorMessage:""
|
||||
};
|
||||
// console.log(koek);
|
||||
}
|
||||
|
||||
export async function authenticate(_currentState: unknown, formData: FormData):Promise<LoginReturn|null>
|
||||
{
|
||||
console.log("action triggered")
|
||||
try {
|
||||
const signInStatus = await signIn('credentials', formData)
|
||||
return signInStatus;
|
||||
} catch (error:any) {
|
||||
if (error) {
|
||||
switch (error.type) {
|
||||
case 'CredentialsSignin':
|
||||
return {
|
||||
errorMessage: 'invalidCredentials'
|
||||
}
|
||||
default:
|
||||
return {
|
||||
errorMessage: 'Something went wrong.'
|
||||
}
|
||||
}
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
export async function koekValid(koek:string):Promise<boolean>
|
||||
{
|
||||
|
||||
|
||||
const validateSession = await fetch(constructAPIUrl("auth/validate"),{
|
||||
method:"POST",
|
||||
headers:{
|
||||
Cookie: `auth=${koek};`
|
||||
}
|
||||
});
|
||||
if(validateSession.status == 200)
|
||||
return true
|
||||
else
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user