Skip to content

Commit fa60e68

Browse files
authored
Merge pull request #13 from Dialogue-Bot/DIAL-19-logout
DIAL-19-logout
2 parents a7d712e + 26393bf commit fa60e68

File tree

16 files changed

+88
-10
lines changed

16 files changed

+88
-10
lines changed

client/src/apis/auth.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ class Auth {
3737
loginWithIdToken(idToken: string): Promise<TBaseResponse<TToken>> {
3838
return http_client.post(ENDPOINTS.AUTH.WITH_ID_TOKEN, { idToken });
3939
}
40+
41+
logout(): Promise<TBaseResponse<null>> {
42+
return http_client.post(ENDPOINTS.AUTH.LOGOUT);
43+
}
4044
}
4145

4246
export const auth = new Auth();

client/src/constants/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const ENDPOINTS = {
99
FORGOT_PASSWORD: '/auth/forgot-password',
1010
RESET_PASSWORD: '/auth/reset-password',
1111
WITH_ID_TOKEN: '/auth/with-id-token',
12+
LOGOUT: '/auth/logout',
1213
},
1314
UPLOAD: {
1415
SINGLE: '/upload/single',

client/src/hooks/auth/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ export * from './use-forgot-pass';
22
export * from './use-login';
33
export * from './use-login-with-id-token';
44
export * from './use-login-with-provider';
5+
export * from './use-logout';
56
export * from './use-register';
67
export * from './use-set-pass';

client/src/hooks/auth/use-login-with-provider.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ export const useLoginWithProvider = (provider: AuthProvider) => {
2020

2121
return auth.loginWithIdToken(idToken);
2222
},
23-
onSuccess(data) {
24-
toast.success(data.message);
25-
23+
async onSuccess() {
2624
window.location.href = search.redirect || '/dashboard';
2725
},
2826
onError(err: any) {

client/src/hooks/auth/use-login.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ export const useLogin = () => {
1515
mutationFn: (data: TLogin) => {
1616
return auth.login(data);
1717
},
18-
onSuccess(data) {
19-
toast.success(data.message);
20-
18+
async onSuccess() {
2119
window.location.href = search.redirect || '/dashboard';
2220
},
2321
onError(err: any) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { auth } from '@/apis/auth';
2+
import { auth as fAuth } from '@/lib/firebase';
3+
import { useUserStore } from '@/store/use-user';
4+
import { useMutation } from '@tanstack/react-query';
5+
6+
export const useLogout = () => {
7+
const { user } = useUserStore();
8+
return useMutation({
9+
mutationFn: async () => {
10+
if (user?.provider !== 'local') {
11+
await fAuth.signOut();
12+
}
13+
14+
await auth.logout();
15+
},
16+
async onSuccess() {
17+
window.location.href = '/login';
18+
},
19+
onError() {
20+
window.location.href = '/login';
21+
},
22+
});
23+
};

client/src/routes/_private/chatbots.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1+
import { useLogout } from '@/hooks/auth';
12
import { useUserStore } from '@/store/use-user';
23
import { createFileRoute } from '@tanstack/react-router';
34

45
const Chatbots = () => {
56
const { user } = useUserStore();
7+
const logoutMutaion = useLogout();
68

7-
return <div>{JSON.stringify(user)}</div>;
9+
return (
10+
<div>
11+
{JSON.stringify(user)}
12+
<button
13+
onClick={() => {
14+
logoutMutaion.mutate();
15+
}}
16+
>
17+
Logout
18+
</button>
19+
</div>
20+
);
821
};
922

1023
export const Route = createFileRoute('/_private/chatbots')({

client/src/store/use-user.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { devtools } from 'zustand/middleware';
44

55
type TUserState = {
66
user: TUser | null;
7-
setUser: (user: TUser) => void;
7+
setUser: (user: TUser | null) => void;
88
};
99

1010
export const useUserStore = create<TUserState>()(

server/src/constants/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const ENDPOINTS = {
2121
FORGOT_PASSWORD: '/auth/forgot-password',
2222
RESET_PASSWORD: '/auth/reset-password',
2323
WITH_ID_TOKEN: '/auth/with-id-token',
24+
LOGOUT: '/auth/logout',
2425
},
2526
UPLOAD: {
2627
SINGLE: '/upload/single',

server/src/controllers/auth.controller.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ export class AuthController {
9999
});
100100
});
101101

102+
public logout = catchAsync(async (req: RequestWithUser, res) => {
103+
104+
await this.authService.logout(req.user?.id as string)
105+
106+
this.clearTokensCookie(res);
107+
108+
res.status(StatusCodes.OK).json({
109+
message: this.localeService.i18n().AUTH.LOGOUT_SUCCESS(),
110+
data: null,
111+
});
112+
});
113+
102114
private setTokensCookie = (res: Response, tokens: TTokenData) => {
103115
const { accessToken, refreshToken } = tokens;
104116
res.cookie('access_token', accessToken, {
@@ -115,4 +127,9 @@ export class AuthController {
115127
domain: 'localhost', //TODO: THIS WILL BE CHANGED IN PRODUCTION
116128
});
117129
};
130+
131+
private clearTokensCookie = (res: Response) => {
132+
res.clearCookie('access_token',);
133+
res.clearCookie('refresh_token');
134+
};
118135
}

0 commit comments

Comments
 (0)