changes
This commit is contained in:
+47
-43
@@ -1,68 +1,66 @@
|
||||
'use server'
|
||||
|
||||
import { constructAPIUrl } from "@/util/Utils"
|
||||
// import { signIn } from '@/auth'
|
||||
import { cookies } from "next/headers"
|
||||
import { cookies } from "next/headers"
|
||||
import { parseSetCookie } from "@/util/parseSetCookie";
|
||||
import makeFetchCookie from 'fetch-cookie';
|
||||
import fetchCookie from "fetch-cookie";
|
||||
import makeFetchCookie from 'fetch-cookie';
|
||||
import fetchCookie from "fetch-cookie";
|
||||
import { Attributes } from "@sequelize/core";
|
||||
import { User } from "@/model/User";
|
||||
|
||||
type LoginReturn = {
|
||||
cookie?:unknown,
|
||||
errorMessage?:string;
|
||||
}
|
||||
|
||||
async function attemptAPILogin(method:string,b:FormData):Promise<LoginReturn|null>
|
||||
async function attemptAPILogin(method:string,formData: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 { CookieJar, Cookie } = fetchCookie.toughCookie;
|
||||
const cj = new CookieJar()
|
||||
const fetchKoek = makeFetchCookie(fetch, cj);
|
||||
// Set Basic Auth
|
||||
headers.set('Authorization', `Basic ${Buffer.from(`${b.get('input_username')}:${b.get('input_password')}`).toString('base64')}`);
|
||||
let res = await fetchKoek(constructAPIUrl("auth"), {
|
||||
method:'POST',
|
||||
credentials: 'include',
|
||||
headers:headers,
|
||||
});
|
||||
console.log(cj.store.idx['localhost']['/']);
|
||||
// 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);
|
||||
// Check if form data is present with required fields, return null if not
|
||||
if(!formData || !formData.get('input_username') || !formData.get('input_password')) return null;
|
||||
|
||||
// Instantiate header object
|
||||
let headers:Headers = new Headers();
|
||||
|
||||
// Prepare fetchCookie
|
||||
const { CookieJar, Cookie } = fetchCookie.toughCookie;
|
||||
const jar = new CookieJar()
|
||||
const fetchWithCookie = makeFetchCookie(fetch, jar);
|
||||
|
||||
// Set Basic Auth
|
||||
headers.set('Authorization', `Basic ${Buffer.from(`${formData.get('input_username')}:${formData.get('input_password')}`).toString('base64')}`);
|
||||
let res = await fetchWithCookie(constructAPIUrl("auth"), {
|
||||
method:'POST',
|
||||
credentials: 'include',
|
||||
headers:headers,
|
||||
});
|
||||
|
||||
console.log(jar.store.idx['localhost']['/']);
|
||||
|
||||
let koek = res.headers.getSetCookie();
|
||||
|
||||
let cookieDict = parseSetCookie(koek);
|
||||
|
||||
await cookies().set('auth', cookieDict.auth);
|
||||
return {
|
||||
cookie:cookieDict.auth,
|
||||
errorMessage:""
|
||||
};
|
||||
// console.log(koek);
|
||||
}
|
||||
|
||||
export async function serverAttemptAuthenticateUser(_currentState: unknown, formData: FormData):Promise<LoginReturn|null>
|
||||
{
|
||||
console.log("action triggered")
|
||||
try {
|
||||
try {
|
||||
const signInStatus = await attemptAPILogin('credentials', formData)
|
||||
return signInStatus;
|
||||
} catch (error:any) {
|
||||
if (error) {
|
||||
switch (error.type) {
|
||||
case 'CredentialsSignin':
|
||||
return {
|
||||
errorMessage: 'invalidCredentials'
|
||||
}
|
||||
default:
|
||||
return {
|
||||
errorMessage: 'Something went wrong.'
|
||||
}
|
||||
case 'CredentialsSignin': return { errorMessage: 'invalidCredentials' };
|
||||
default: return { errorMessage: 'Something went wrong.' };
|
||||
}
|
||||
}
|
||||
throw error
|
||||
throw Error
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,4 +76,10 @@ export async function serverValidateSessionCookie(koek:string):Promise<boolean>
|
||||
return true
|
||||
else
|
||||
return false
|
||||
}
|
||||
|
||||
export async function userIsAdmin(user:Partial<Attributes<User>>):Promise<boolean>
|
||||
{
|
||||
|
||||
return false;
|
||||
}
|
||||
Reference in New Issue
Block a user