Update user routing, added user authorization with JWT cookie management
This commit is contained in:
parent
662b26ad47
commit
f055e77eea
7 changed files with 97 additions and 10 deletions
23
lib/server_functions/dataAccessLayer.ts
Normal file
23
lib/server_functions/dataAccessLayer.ts
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import { cookies } from "next/headers";
|
||||
import { cache } from "react";
|
||||
|
||||
import "server-only";
|
||||
import { getCurrentUser } from "../api/helpers/users";
|
||||
import { User } from "../api/types";
|
||||
|
||||
export const verifySession = cache(async () => {
|
||||
const cookie = (await cookies()).get("access_token")?.value;
|
||||
if (!cookie) {
|
||||
return { isAuthed: false };
|
||||
}
|
||||
const response = await getCurrentUser();
|
||||
if (response.status == 401) {
|
||||
return { isAuthed: false };
|
||||
}
|
||||
const user = (await response.json()) as User;
|
||||
return {
|
||||
isAuthed: true,
|
||||
surface_auth_level: user.authority,
|
||||
username: user.username,
|
||||
};
|
||||
});
|
||||
|
|
@ -9,7 +9,7 @@ export async function login(formData: FormData) {
|
|||
"use server";
|
||||
const token = await loginUser(formData);
|
||||
await createSession(token);
|
||||
redirect("/admin/overview");
|
||||
redirect("/Admin/Overview");
|
||||
}
|
||||
|
||||
export async function signup(formData: FormData) {
|
||||
|
|
@ -17,7 +17,7 @@ export async function signup(formData: FormData) {
|
|||
"use server";
|
||||
const token = await createUser(formData);
|
||||
await createSession(token);
|
||||
redirect("/admin/overview");
|
||||
redirect("/Admin/Overview");
|
||||
}
|
||||
|
||||
export async function createSession(token: Token) {
|
||||
|
|
@ -26,8 +26,16 @@ export async function createSession(token: Token) {
|
|||
|
||||
cookieStore.set("access_token", access_token, {
|
||||
httpOnly: true,
|
||||
secure: true,
|
||||
secure: process.env.NODE_ENV === "production",
|
||||
sameSite: "lax",
|
||||
path: "/",
|
||||
});
|
||||
}
|
||||
|
||||
export async function getSessionToken() {
|
||||
const cookieStore = await cookies();
|
||||
console.log(cookieStore.getAll());
|
||||
const token = cookieStore.get("access_token")?.value;
|
||||
|
||||
return token ? token : null;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue