Merge pull request 'fix/auth' (#7) from fix/auth into main
All checks were successful
Sync to GitHub / sync (push) Successful in 8s
All checks were successful
Sync to GitHub / sync (push) Successful in 8s
Reviewed-on: #7
This commit is contained in:
7
app/(safe-mode-page)/auth/logout/route.tsx
Normal file
7
app/(safe-mode-page)/auth/logout/route.tsx
Normal 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);
|
||||
};
|
||||
@ -1,6 +1,7 @@
|
||||
import type { Metadata } from "next";
|
||||
import { Geist, Geist_Mono, Inter } from "next/font/google";
|
||||
import "./globals.css";
|
||||
import { Toaster } from "@/shared/libs/shadcn/ui/sonner";
|
||||
|
||||
const inter = Inter({ subsets: ["latin"], variable: "--font-sans" });
|
||||
|
||||
@ -29,7 +30,8 @@ export default function RootLayout({
|
||||
<body
|
||||
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
|
||||
>
|
||||
{children}
|
||||
<main>{children}</main>
|
||||
<Toaster />
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
|
||||
@ -47,6 +47,7 @@ export const submitProviderCallback = async (
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
status: 500,
|
||||
message: "Error submitting provider callback",
|
||||
error: error,
|
||||
};
|
||||
|
||||
@ -5,6 +5,7 @@ import { UAParser } from "ua-parser-js";
|
||||
|
||||
export interface BackendResponse<T = unknown> {
|
||||
success: boolean;
|
||||
status: number;
|
||||
message: string;
|
||||
data?: T;
|
||||
error?: unknown;
|
||||
@ -34,16 +35,11 @@ export const backendFetch = async (path: string, options: RequestInit = {}) => {
|
||||
...options.headers,
|
||||
},
|
||||
cache: "default",
|
||||
});
|
||||
}).then((response) => response.json());
|
||||
|
||||
const resJson = (await res.json()) as BackendResponse;
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(`Elysia error: ${resJson.error}`);
|
||||
}
|
||||
|
||||
return resJson;
|
||||
} catch {
|
||||
return res as BackendResponse;
|
||||
} catch (res) {
|
||||
if (process.env.NODE_ENV === "development") return res;
|
||||
redirect("/status?reason=backend-unreachable");
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,18 +1,16 @@
|
||||
"use server";
|
||||
|
||||
import { backendFetch } from "@/shared/helpers/backendFetch";
|
||||
import { cookies } from "next/headers";
|
||||
import { backendFetch, BackendResponse } from "@/shared/helpers/backendFetch";
|
||||
|
||||
export const logout = async () => {
|
||||
const res = await backendFetch("auth/logout", {
|
||||
const res = (await backendFetch("auth/logout", {
|
||||
method: "POST",
|
||||
});
|
||||
})) as BackendResponse;
|
||||
|
||||
if (res.success) {
|
||||
(await cookies()).delete("auth_token");
|
||||
return {
|
||||
success: true,
|
||||
message: "Logged out successfully",
|
||||
message: "Logout successful",
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
"use server";
|
||||
|
||||
import { backendFetch, BackendResponse } from "@/shared/helpers/backendFetch";
|
||||
import { cookies } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
export interface UserSession {
|
||||
id: string;
|
||||
@ -30,18 +30,14 @@ export interface UserSession {
|
||||
}
|
||||
|
||||
export const validateAndDecodeJWT = async (): Promise<UserSession | null> => {
|
||||
const cookieHeader = (await cookies()).get("auth_token")?.value;
|
||||
|
||||
if (!cookieHeader) {
|
||||
return null;
|
||||
}
|
||||
|
||||
"use server";
|
||||
const res = (await backendFetch("auth/token/validate", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
token: cookieHeader,
|
||||
}),
|
||||
})) as BackendResponse<UserSession>;
|
||||
|
||||
return res.data!;
|
||||
if (res.status === 403) {
|
||||
redirect("/auth/logout");
|
||||
}
|
||||
|
||||
return res.data ?? null;
|
||||
};
|
||||
|
||||
@ -11,7 +11,9 @@ import {
|
||||
import { Spinner } from "@/shared/libs/shadcn/ui/spinner";
|
||||
import { logout } from "@/shared/models/auth/logout";
|
||||
import { Button } from "@base-ui/react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import React from "react";
|
||||
import { toast } from "sonner";
|
||||
|
||||
const LogoutAlert = ({
|
||||
openState,
|
||||
@ -20,12 +22,30 @@ const LogoutAlert = ({
|
||||
openState: boolean;
|
||||
setOpenState: React.Dispatch<React.SetStateAction<boolean>>;
|
||||
}) => {
|
||||
const router = useRouter();
|
||||
const [isLoading, setIsLoading] = React.useState(false);
|
||||
const continueLogout = async () => {
|
||||
setIsLoading(true);
|
||||
await logout().then((res) =>
|
||||
res.success ? window.location.reload() : setIsLoading(false),
|
||||
);
|
||||
const res = await logout();
|
||||
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 (
|
||||
|
||||
Reference in New Issue
Block a user