-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(frontend): implement user authentication flow
- Loading branch information
1 parent
107fc63
commit c05a536
Showing
14 changed files
with
89 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# Makefile for Docker Compose commands | ||
include .env | ||
|
||
# Variables | ||
DEEPEVAL := ${PWD}/backend/.venv/bin/deepeval | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
NEXT_PUBLIC_CHATBOT_CORE_BACKEND_URL="CHATBOT_CORE_BACKEND_URL" | ||
NEXT_PUBLIC_API_VERSION="v1" | ||
|
||
NEXT_PUBLIC_GOOGLE_CLIENT_ID="GOOGLE_CLIENT_ID" | ||
NEXT_PUBLIC_GOOGLE_REDIRECT_URI="http://localhost:3000" | ||
NEXT_PUBLIC_ACCESS_TOKEN_KEY="ezhr-access-token" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ yarn-error.log* | |
|
||
# local env files | ||
.env*.local | ||
!.env.example | ||
|
||
# vercel | ||
.vercel | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
chatbot-core/frontend-v2/src/hooks/user/use-user-logout.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { useQueryClient } from '@tanstack/react-query'; | ||
import { useRouter } from 'next/navigation'; | ||
|
||
import { LOCAL_STORAGE_ACCESS_TOKEN_KEY } from '@/configs/misc'; | ||
|
||
export const useUserLogout = () => { | ||
const queryClient = useQueryClient(); | ||
const router = useRouter(); | ||
|
||
return () => { | ||
localStorage.removeItem(LOCAL_STORAGE_ACCESS_TOKEN_KEY); | ||
queryClient.invalidateQueries({ | ||
type: 'active', | ||
}); | ||
router.push('/'); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import axios from 'axios'; | ||
|
||
import { LOCAL_STORAGE_ACCESS_TOKEN_KEY } from '@/configs/misc'; | ||
|
||
const api = axios.create({ | ||
headers: { 'Content-Type': 'application/json' }, | ||
withCredentials: true, | ||
}); | ||
|
||
api.interceptors.request.use(config => { | ||
const accessToken = localStorage.getItem(LOCAL_STORAGE_ACCESS_TOKEN_KEY); | ||
|
||
if (accessToken) { | ||
config.headers.Authorization = `Bearer ${accessToken}`; | ||
} | ||
|
||
return config; | ||
}); | ||
|
||
export default api; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
chatbot-core/frontend-v2/src/services/user/get-current-user.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 13 additions & 6 deletions
19
chatbot-core/frontend-v2/src/services/user/get-user-oauth-access-token.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,20 @@ | ||
import axios from 'axios'; | ||
|
||
import api from '@/lib/axios'; | ||
import { IApiResponse } from '@/types/api-response'; | ||
import { getAuthUrl } from '@/utils/get-api-url'; | ||
|
||
export const getUserOauthAccessToken = async (code: string) => { | ||
interface IAccessToken { | ||
access_token: string; | ||
token_type: string; | ||
} | ||
|
||
export const getUserOauthAccessToken = async (code: string): Promise<IAccessToken> => { | ||
try { | ||
await axios.get(getAuthUrl(`/oauth/google?code=${code}`)); | ||
return true; | ||
const res = await api.get<IApiResponse<IAccessToken>>(getAuthUrl(`/oauth/google?code=${code}`)); | ||
return res.data.data; | ||
} catch (error) { | ||
// We want to show the Error here, however, we still throw the Error | ||
// once again for the `useQuery` to catch it. | ||
console.error('[UserAccessToken]: ', error); | ||
return false; | ||
throw new Error('Error getting user access token'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface IApiResponse<T> { | ||
message: string; | ||
headers: string | null; | ||
data: T; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters