Skip to content

Commit ac081a0

Browse files
committed
Use next.js next/navigation useRouter to route to login on missing refreshToken
1 parent d25aa04 commit ac081a0

File tree

5 files changed

+24
-12
lines changed

5 files changed

+24
-12
lines changed

frontend/app/context/authContext.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
"use client";
2+
13
import React, { useState, useContext, useEffect } from "react";
4+
import { useRouter } from "next/navigation";
25

36
interface AuthContextType {
47
getAccessToken: () => Promise<string>;
@@ -14,6 +17,7 @@ export function useAuth() {
1417
export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
1518
const [accessToken, setAccessToken] = useState("");
1619
const [expiryTime, setExpiryTime] = useState(0);
20+
const [shouldRedirect, setShouldRedirect] = useState(false);
1721

1822
async function getAccessToken(): Promise<string> {
1923
if (Date.now() >= expiryTime) {
@@ -24,28 +28,38 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
2428
}
2529

2630
useEffect(() => {
31+
if (typeof window === "undefined") return;
2732
getAccessToken();
2833
}, []);
2934

35+
const router = useRouter();
3036
async function refreshAccessToken(): Promise<string> {
3137
try {
3238
const response = await fetch("http://localhost:8000/refresh", {
3339
method: "POST",
3440
credentials: "include",
3541
});
36-
37-
if (!response.ok) throw new Error("Failed to refresh token");
3842
const data = await response.json();
39-
43+
if (!response.ok) {
44+
setShouldRedirect(true);
45+
console.log("Failed to refresh access token");
46+
}
4047
setAccessToken(data.access_token);
4148
setExpiryTime(Date.now() + 15 * 60 * 1000); // 15 minutes
4249
return data.access_token;
4350
} catch (error) {
44-
console.error("Error refreshing access token:", error);
51+
setShouldRedirect(true);
52+
console.log("Error refreshing access token:", error);
4553
return "";
4654
}
4755
}
4856

57+
useEffect(() => {
58+
if (shouldRedirect) {
59+
router.push("/login");
60+
}
61+
}, [shouldRedirect, router]);
62+
4963
return (
5064
<AuthContext.Provider value={{ getAccessToken }}>
5165
{children}

frontend/app/dashboard/page.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { AuthProvider } from "../context/authContext";
66

77
export default function Home() {
88
return (
9-
<AuthProvider>
10-
<Dashboard />
11-
</AuthProvider>
9+
<AuthProvider>
10+
<Dashboard />
11+
</AuthProvider>
1212
);
1313
}

frontend/app/hooks/useWebsocketDashboard.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function useWebSocketDashboard(setCircles: React.Dispatch<React.SetStateA
1717
useEffect(() => {
1818
const connectWebSocket = async () => {
1919
const token = await getAccessToken();
20-
ws.current = new WebSocket("wss://127.0.0.1:8000/ws", [token]);
20+
ws.current = new WebSocket("wss://localhost:8000/ws", [token]);
2121
ws.current.onopen = () => console.log("ws opened");
2222
ws.current.onclose = () => console.log("ws closed");
2323
const wsCurrent = ws.current;

frontend/app/layout.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ const geistMono = localFont({
1212
variable: "--font-geist-mono",
1313
weight: "100 900",
1414
});
15-
1615
export const metadata: Metadata = {
17-
title: "Create Next App",
18-
description: "Generated by create next app",
16+
title: "Chat App",
17+
description: "A real time chat application designed for performance and scalability.",
1918
};
2019

2120
export default function RootLayout({

frontend/app/page.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ import { redirect } from 'next/navigation';
22

33
export default function Home() {
44
redirect('/login');
5-
return null;
65
}

0 commit comments

Comments
 (0)