aboutsummaryrefslogtreecommitdiffhomepage
path: root/frontend/app/.server/auth.ts
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2024-07-28 17:13:01 +0900
committernsfisis <nsfisis@gmail.com>2024-07-28 17:13:01 +0900
commitaadc8cf037855b99cb82798c7b0ebaafc5bb025b (patch)
tree4ea9f9db9dbe7cf1b7720205ae281a6b8bcca8e9 /frontend/app/.server/auth.ts
parent90741e8336b4ffba090bf08c3b899992860e2d98 (diff)
parent2d5f913a431c4223a16c88551ffff4100ac483c4 (diff)
downloadphperkaigi-2025-albatross-aadc8cf037855b99cb82798c7b0ebaafc5bb025b.tar.gz
phperkaigi-2025-albatross-aadc8cf037855b99cb82798c7b0ebaafc5bb025b.tar.zst
phperkaigi-2025-albatross-aadc8cf037855b99cb82798c7b0ebaafc5bb025b.zip
Merge branch 'game-entry'
Diffstat (limited to 'frontend/app/.server/auth.ts')
-rw-r--r--frontend/app/.server/auth.ts27
1 files changed, 8 insertions, 19 deletions
diff --git a/frontend/app/.server/auth.ts b/frontend/app/.server/auth.ts
index a3496af..988b30c 100644
--- a/frontend/app/.server/auth.ts
+++ b/frontend/app/.server/auth.ts
@@ -9,7 +9,7 @@ import { components } from "./api/schema";
export const authenticator = new Authenticator<string>(sessionStorage);
async function login(username: string, password: string): Promise<string> {
- const { data, error } = await apiClient.POST("/api/login", {
+ const { data, error } = await apiClient.POST("/login", {
body: {
username,
password,
@@ -30,15 +30,7 @@ authenticator.use(
"default",
);
-type JwtPayload = components["schemas"]["JwtPayload"];
-
-export type User = {
- userId: number;
- username: string;
- displayName: string;
- iconPath: string | null;
- isAdmin: boolean;
-};
+export type User = components["schemas"]["JwtPayload"];
export async function isAuthenticated(
request: Request | Session,
@@ -47,7 +39,7 @@ export async function isAuthenticated(
failureRedirect?: never;
headers?: never;
},
-): Promise<User | null>;
+): Promise<{ user: User; token: string } | null>;
export async function isAuthenticated(
request: Request | Session,
options: {
@@ -63,7 +55,7 @@ export async function isAuthenticated(
failureRedirect: string;
headers?: HeadersInit;
},
-): Promise<User>;
+): Promise<{ user: User; token: string }>;
export async function isAuthenticated(
request: Request | Session,
options: {
@@ -95,7 +87,7 @@ export async function isAuthenticated(
failureRedirect: string;
headers?: HeadersInit;
} = {},
-): Promise<User | null> {
+): Promise<{ user: User; token: string } | null> {
// This function's signature should be compatible with `authenticator.isAuthenticated` but TypeScript does not infer it correctly.
let jwt;
const { successRedirect, failureRedirect, headers } = options;
@@ -122,12 +114,9 @@ export async function isAuthenticated(
if (!jwt) {
return null;
}
- const payload = jwtDecode<JwtPayload>(jwt);
+ const user = jwtDecode<User>(jwt);
return {
- userId: payload.user_id,
- username: payload.username,
- displayName: payload.display_name,
- iconPath: payload.icon_path ?? null,
- isAdmin: payload.is_admin,
+ user,
+ token: jwt,
};
}