Skip to content

Commit b7ab1ff

Browse files
committed
fix bug on logout journey
1 parent 042550e commit b7ab1ff

20 files changed

Lines changed: 132 additions & 142 deletions

File tree

src/app/(pages)/api/auth/checkLogin/route.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/app/(pages)/api/auth/checkToken/route.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/app/(pages)/api/auth/login/verify/otp/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export async function POST(req: NextRequest) {
1717
firstName: 'Saghar',
1818
role: UserRole.Admin,
1919
lastLogin: new Date(),
20-
token: 'mockAuthToken',
20+
token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6InNhZ2hhciBmYWRhZWkiLCJpYXQiOjE1MTYyMzkwMjJ9.B5SGLT-b7aweYbaUMVD7gjivCQTM9atFeKUlW8Fo2yU',
2121
refreshToken: 'mockRefreshAuthToken'
2222

2323
}

src/app/(pages)/api/remove-tockens/route.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export async function POST() {
2121
secure: true,
2222
sameSite: 'strict'
2323
});
24+
2425

2526
return response;
2627
}

src/app/(pages)/api/user/account/userprofile/route.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ export async function GET(req: NextRequest) {
1313
lastName: 'Fadaei',
1414
firstName: 'Saghar',
1515
role: UserRole.Admin,
16-
lastLogin: new Date(),
17-
token: 'string',
18-
refreshToken: 'string'
16+
lastLogin: new Date()
1917

2018
}
2119
return NextResponse.json({ message: 'get user information successfully', data }, { status: 200 });

src/app/clientWrapper.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import React, { useEffect, useState } from "react";
33
import isMobileView from "@/app/utils/isMobileView";
44
import Header from "@/app/components/Header/webHeader";
55
import { usePathname } from "next/navigation";
6-
import Menu from "./components/SafeLLM/Menu";
76
import { useShowInnerComponent } from "./ShowInnerComponentContext";
7+
import Menu from "./components/SafeLLM/Menu";
88

99
const pages = {
1010
'/dashboard': 'Dashboard',

src/app/components/Chat/WebChat/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default function WebChatComponent({ initialChatId }: any) {
99

1010
return (
1111
<Sidebar isSidebarOpen={isSidebarOpen} setIsSidebarOpen={setIsSidebarOpen}>
12-
<div className="flex flex-col w-full">
12+
<div className="flex flex-col w-full flex-1">
1313
<div className="flex-grow p-4 overflow-y-auto" ref={chatBoxRef}>
1414
{messages.map((message, index) => (
1515
<div key={index} className={`my-2 ${message.sender === "user" ? "text-right" : "text-left"}`}>
@@ -33,7 +33,7 @@ export default function WebChatComponent({ initialChatId }: any) {
3333
))}
3434
<div ref={messagesEndRef} />
3535
</div>
36-
<div className="p-4 border-t border-gray-300">
36+
<div className="p-4 border-t border-gray-300 mt-auto">
3737
<input
3838
type="text"
3939
value={userInput}
Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,35 @@
1-
import { GetServerSideProps } from 'next';
2-
import { parseCookies } from 'nookies';
1+
"use client"
32
import HeaderItems from "@/app/components/Header/webHeader/headerItems";
43
import Link from "next/link";
5-
import React from "react";
4+
import React, { useEffect, useState } from "react";
65
import IButton from "@/app/components/Common/Button";
76
import { UserProfileResponseInterface } from "@/store/userProfile/interface";
87
import getUserProfileService from "@/app/services/getUserProfile.service";
98

10-
// SSR function to check login status
11-
export const getServerSideProps: GetServerSideProps = async (ctx) => {
12-
const cookies = parseCookies(ctx);
13-
const token = cookies['auth-token']; // Get the auth-token from cookies
14-
15-
if (!token) {
16-
// If the user is not logged in, redirect to login page
17-
return {
18-
redirect: {
19-
destination: '/login',
20-
permanent: false,
21-
},
22-
};
23-
}
9+
export default function Header() {
2410

25-
// Fetch user profile if token exists
26-
const userProfile = await getUserProfileService(false);
11+
const [userProfile, setUserProfile] = useState<UserProfileResponseInterface>();
2712

28-
return {
29-
props: { userProfile },
30-
};
31-
}
13+
const isLogin = localStorage.getItem('isLogin');
14+
15+
useEffect(() => {
16+
17+
if (isLogin) {
18+
getUserProfileService(false).then((res: any) => {
19+
setUserProfile(res)
20+
})
21+
}
22+
}, []);
3223

33-
export default function Header({ userProfile }: { userProfile: UserProfileResponseInterface }) {
3424
return (
3525
<div className={"fixed w-full z-50"}>
3626
<div className={"flex justify-between items-center h-18 bg-white px-16"}>
37-
<Link href={"/"} className={'h-full flex'}>
27+
<Link href={"/"} className={'h-full flex'} >
3828
<img src={"/safellm.svg"} alt={"SafeLLM"} />
29+
3930
</Link>
4031
<HeaderItems />
41-
{userProfile ?
32+
{isLogin ?
4233
<div className={'flex gap-6 items-center justify-center'}>
4334
<Link href={"/profile"}>
4435
<h5>Profile</h5>
@@ -62,4 +53,5 @@ export default function Header({ userProfile }: { userProfile: UserProfileRespon
6253
</div>
6354
</div>
6455
);
65-
}
56+
57+
}

src/app/components/Login/Forms/OtpForm/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export default function OtpForm(props: OTPFormProps) {
6969
className={'ml-2'}
7070
onComplete={() => setTimerCompleted(true)}
7171
renderer={counterProps =>
72-
<div> {counterProps.seconds} : {counterProps.minutes} </div>}/>
72+
<div> {counterProps.minutes} : {counterProps.seconds}</div>}/>
7373
<span> {' Seconds '} )</span>
7474
</div>
7575
}

src/app/components/Login/index.tsx

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,35 @@ export default function LoginComponent() {
3636
}
3737
}, [loginStates.isDone]);
3838

39+
3940
useEffect(() => {
40-
if (loginOtpStates.isDone) {
41-
setUserProfile(loginOtpStates.response.data);
42-
axios.post("/api/set-tokens", {
43-
token: loginOtpStates.response.data.token,
44-
refreshToken: loginOtpStates.response.data.refresh_token
45-
});
46-
if (loginOtpStates.response.data.lastLogin == null) {
47-
router.push('/security');
48-
} else {
49-
router.push('/');
41+
const handleLogin = async () => {
42+
if (loginOtpStates.isDone) {
43+
try {
44+
45+
await axios.post("/api/set-tokens", {
46+
token: loginOtpStates.response.data.token,
47+
refreshToken: loginOtpStates.response.data.refresh_token
48+
});
49+
50+
setUserProfile(loginOtpStates.response.data);
51+
debugger;
52+
localStorage.setItem('isLogin', loginOtpStates.response.data.token);
53+
54+
if (loginOtpStates.response.data.lastLogin == null) {
55+
router.push('/security');
56+
} else {
57+
router.push('/');
58+
}
59+
} catch (error) {
60+
console.error("Error setting tokens or during redirect:", error);
61+
}
5062
}
51-
}
63+
};
64+
65+
handleLogin();
5266
}, [loginOtpStates.isDone]);
67+
5368

5469
async function acceptMobileForm(email: string, password: string) {
5570
//api call for mobile number & password

0 commit comments

Comments
 (0)