diff --git a/frontend/core/src/app/layout.tsx b/frontend/core/src/app/layout.tsx
index 360f0cc..a6bd076 100644
--- a/frontend/core/src/app/layout.tsx
+++ b/frontend/core/src/app/layout.tsx
@@ -3,11 +3,11 @@ import { Inter } from "next/font/google";
const inter = Inter({ subsets: ["latin"] });
-import { Metadata } from 'next'
+import { Metadata } from "next";
export const metadata: Metadata = {
- title: 'Turbo',
-}
+ title: "Turbo",
+};
export default function RootLayout({
children,
@@ -15,7 +15,7 @@ export default function RootLayout({
children: React.ReactNode;
}) {
return (
-
+
{children}
);
diff --git a/frontend/core/src/app/login/layout.tsx b/frontend/core/src/app/login/layout.tsx
new file mode 100644
index 0000000..05df4e3
--- /dev/null
+++ b/frontend/core/src/app/login/layout.tsx
@@ -0,0 +1,26 @@
+import { Inter } from "next/font/google";
+
+const inter = Inter({ subsets: ["latin"] });
+
+import { Metadata } from "next";
+import AuthContainer from "@/components/auth";
+
+export const metadata: Metadata = {
+ title: "Turbo",
+};
+
+export default function RootLayout({
+ children,
+}: {
+ children: React.ReactNode;
+}) {
+ return (
+
+
+ {children}
+
+
+ );
+}
+
+export const dynamic = "error";
diff --git a/frontend/core/src/components/auth/index.tsx b/frontend/core/src/components/auth/index.tsx
index 16bf442..4416b5c 100644
--- a/frontend/core/src/components/auth/index.tsx
+++ b/frontend/core/src/components/auth/index.tsx
@@ -5,6 +5,7 @@ import { User } from "@/services/api/models/user";
import { useRouter } from "next/navigation";
import { useContext, useEffect, useState, createContext } from "react";
import route from "@/lib/route";
+import { AxiosError } from "axios";
interface IAuthContext {
user?: User;
@@ -18,7 +19,12 @@ const AuthContext = createContext({
initialized: false,
});
-const AuthContainer = ({ children }: { children: React.ReactNode }) => {
+interface Props {
+ children: React.ReactNode;
+ isPublic?: boolean;
+}
+
+const AuthContainer = ({ children, isPublic }: Props) => {
const router = useRouter();
const [user, setUser] = useState();
const [initialized, setInitialized] = useState(false);
@@ -27,6 +33,7 @@ const AuthContainer = ({ children }: { children: React.ReactNode }) => {
const init = async () => {
try {
if (!getAccessToken()) {
+ debugger
const uuid = getUserId();
const r1 = await api.refreshToken({ uuid });
api.client.setAccessToken(r1.data.accessToken);
@@ -35,9 +42,22 @@ const AuthContainer = ({ children }: { children: React.ReactNode }) => {
const r2 = await api.fetchUser();
const user = factory.user(r2.data.data);
setUser(user);
- setInitialized(true);
+ if (isPublic) {
+ router.push(route.main.toString());
+ }
} catch (e) {
- router.push(route.login.with("?error=loginRequired"));
+ const error = e as AxiosError;
+ if (!isPublic && error.response?.status === 401) {
+ router.push(route.login.with("?error=loginRequired"));
+ return;
+ }
+
+ if (error.response?.status === 401) {
+ return;
+ }
+ throw e;
+ } finally {
+ setInitialized(true);
}
};
@@ -45,13 +65,15 @@ const AuthContainer = ({ children }: { children: React.ReactNode }) => {
}, []);
const logout = async () => {
- await api.logout()
+ await api.logout();
setUser(undefined);
api.client.setAccessToken("");
router.push(route.login.toString());
};
+ console.log("initialized ===========", initialized);
+
if (!initialized) {
return null;
}
diff --git a/frontend/core/src/services/api/index.ts b/frontend/core/src/services/api/index.ts
index decee64..0774cf9 100644
--- a/frontend/core/src/services/api/index.ts
+++ b/frontend/core/src/services/api/index.ts
@@ -1,9 +1,7 @@
import axios, { AxiosError, AxiosInstance } from "axios";
import mockApi from "./mock";
-let accessToken: string;
-
-export const getAccessToken = () => accessToken;
+export const getAccessToken = () => sessionStorage?.getItem('token') || '';
export const setUserId = (uuid: string) => localStorage.setItem("uuid", uuid);
export const getUserId = () => localStorage.getItem("uuid") || "";
@@ -26,6 +24,7 @@ class Client {
...(config.headers || {}),
},
});
+ this._instance.defaults.headers["Authorization"] = `Bearer ${getAccessToken()}`;
}
get instance() {
@@ -37,7 +36,7 @@ class Client {
}
setAccessToken = (token: string) => {
- accessToken = token;
+ sessionStorage?.setItem('token', token)
if (this._instance) {
this._instance.defaults.headers["Authorization"] = `Bearer ${token}`;
}