Merge pull request 'fix/auth' (#7) from fix/auth into main
All checks were successful
Sync to GitHub / sync (push) Successful in 8s

Reviewed-on: #7
This commit is contained in:
2026-02-18 12:58:27 +07:00
8 changed files with 50 additions and 30 deletions

View File

@ -0,0 +1,7 @@
import { cookies } from "next/headers";
import { NextResponse } from "next/server";
export const GET = async (request: Request) => {
(await cookies()).delete("auth_token");
return NextResponse.redirect(new URL("/", request.url), 303);
};

View File

@ -1,6 +1,7 @@
import type { Metadata } from "next"; import type { Metadata } from "next";
import { Geist, Geist_Mono, Inter } from "next/font/google"; import { Geist, Geist_Mono, Inter } from "next/font/google";
import "./globals.css"; import "./globals.css";
import { Toaster } from "@/shared/libs/shadcn/ui/sonner";
const inter = Inter({ subsets: ["latin"], variable: "--font-sans" }); const inter = Inter({ subsets: ["latin"], variable: "--font-sans" });
@ -29,7 +30,8 @@ export default function RootLayout({
<body <body
className={`${geistSans.variable} ${geistMono.variable} antialiased`} className={`${geistSans.variable} ${geistMono.variable} antialiased`}
> >
{children} <main>{children}</main>
<Toaster />
</body> </body>
</html> </html>
); );

View File

@ -47,6 +47,7 @@ export const submitProviderCallback = async (
} catch (error) { } catch (error) {
return { return {
success: false, success: false,
status: 500,
message: "Error submitting provider callback", message: "Error submitting provider callback",
error: error, error: error,
}; };

View File

@ -5,6 +5,7 @@ import { UAParser } from "ua-parser-js";
export interface BackendResponse<T = unknown> { export interface BackendResponse<T = unknown> {
success: boolean; success: boolean;
status: number;
message: string; message: string;
data?: T; data?: T;
error?: unknown; error?: unknown;
@ -34,16 +35,11 @@ export const backendFetch = async (path: string, options: RequestInit = {}) => {
...options.headers, ...options.headers,
}, },
cache: "default", cache: "default",
}); }).then((response) => response.json());
const resJson = (await res.json()) as BackendResponse; return res as BackendResponse;
} catch (res) {
if (!res.ok) { if (process.env.NODE_ENV === "development") return res;
throw new Error(`Elysia error: ${resJson.error}`);
}
return resJson;
} catch {
redirect("/status?reason=backend-unreachable"); redirect("/status?reason=backend-unreachable");
} }
}; };

View File

@ -1,18 +1,16 @@
"use server"; "use server";
import { backendFetch } from "@/shared/helpers/backendFetch"; import { backendFetch, BackendResponse } from "@/shared/helpers/backendFetch";
import { cookies } from "next/headers";
export const logout = async () => { export const logout = async () => {
const res = await backendFetch("auth/logout", { const res = (await backendFetch("auth/logout", {
method: "POST", method: "POST",
}); })) as BackendResponse;
if (res.success) { if (res.success) {
(await cookies()).delete("auth_token");
return { return {
success: true, success: true,
message: "Logged out successfully", message: "Logout successful",
}; };
} else { } else {
return { return {

View File

@ -1,7 +1,7 @@
"use server"; "use server";
import { backendFetch, BackendResponse } from "@/shared/helpers/backendFetch"; import { backendFetch, BackendResponse } from "@/shared/helpers/backendFetch";
import { cookies } from "next/headers"; import { redirect } from "next/navigation";
export interface UserSession { export interface UserSession {
id: string; id: string;
@ -30,18 +30,14 @@ export interface UserSession {
} }
export const validateAndDecodeJWT = async (): Promise<UserSession | null> => { export const validateAndDecodeJWT = async (): Promise<UserSession | null> => {
const cookieHeader = (await cookies()).get("auth_token")?.value; "use server";
if (!cookieHeader) {
return null;
}
const res = (await backendFetch("auth/token/validate", { const res = (await backendFetch("auth/token/validate", {
method: "POST", method: "POST",
body: JSON.stringify({
token: cookieHeader,
}),
})) as BackendResponse<UserSession>; })) as BackendResponse<UserSession>;
return res.data!; if (res.status === 403) {
redirect("/auth/logout");
}
return res.data ?? null;
}; };

View File

@ -11,7 +11,9 @@ import {
import { Spinner } from "@/shared/libs/shadcn/ui/spinner"; import { Spinner } from "@/shared/libs/shadcn/ui/spinner";
import { logout } from "@/shared/models/auth/logout"; import { logout } from "@/shared/models/auth/logout";
import { Button } from "@base-ui/react"; import { Button } from "@base-ui/react";
import { useRouter } from "next/navigation";
import React from "react"; import React from "react";
import { toast } from "sonner";
const LogoutAlert = ({ const LogoutAlert = ({
openState, openState,
@ -20,12 +22,30 @@ const LogoutAlert = ({
openState: boolean; openState: boolean;
setOpenState: React.Dispatch<React.SetStateAction<boolean>>; setOpenState: React.Dispatch<React.SetStateAction<boolean>>;
}) => { }) => {
const router = useRouter();
const [isLoading, setIsLoading] = React.useState(false); const [isLoading, setIsLoading] = React.useState(false);
const continueLogout = async () => { const continueLogout = async () => {
setIsLoading(true); setIsLoading(true);
await logout().then((res) => const res = await logout();
res.success ? window.location.reload() : setIsLoading(false), if (!res.success) {
); setIsLoading(false);
toast.error(res.message || "Logout failed", {
position: "bottom-right",
description:
"An error occurred while logging out. Please try again later.",
richColors: true,
});
} else {
toast.success(res.message || "Logout successful", {
position: "bottom-right",
description: "You have been logged out successfully.",
richColors: true,
});
router.push("/auth/logout");
setTimeout(() => {
window.location.reload();
}, 2000);
}
}; };
return ( return (