-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
225a102
commit 2f2f9f0
Showing
29 changed files
with
836 additions
and
18 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
NEXT_PUBLIC_BACKEND_URL= | ||
NEXT_PUBLIC_GOOGLE_CLIENT_ID= |
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 | ||
|
||
# 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,58 @@ | ||
import Image from "next/image"; | ||
import { useEffect } from "react"; | ||
import { IMAGES } from "../../constants/images"; | ||
import { INFO } from "../../constants/info"; | ||
import useGoogleAuth from "../../hooks/useGoogleAuth"; | ||
|
||
const AuthIndex = () => { | ||
const { handleGoogle, loading } = useGoogleAuth(); | ||
|
||
useEffect(() => { | ||
const google = (window as any).google; | ||
if (google) { | ||
google.accounts.id.initialize({ | ||
client_id: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID, | ||
callback: handleGoogle, | ||
}); | ||
google.accounts.id.renderButton(document.getElementById("authDiv"), { | ||
type: "standard", | ||
size: "large", | ||
text: "continue_with", | ||
shape: "pill", | ||
}); | ||
google.accounts.id.prompt(); | ||
} | ||
}, [handleGoogle]); | ||
|
||
return ( | ||
<div className="grid h-screen grid-cols-1 text-gray-900 md:grid-cols-2"> | ||
<div | ||
style={{ backgroundImage: `url(${IMAGES.Auth_background})` }} | ||
className="flex h-full w-full flex-col bg-white bg-cover bg-no-repeat p-8 md:!bg-none" | ||
> | ||
<div className="flex cursor-pointer flex-wrap items-baseline gap-2"> | ||
<Image src={IMAGES.Logo} alt="logo" width={50} height={50} /> | ||
<h1 className="whitespace-nowrap font-serif text-xl font-bold text-white md:text-gray-900"> | ||
{INFO.Title} | ||
</h1> | ||
</div> | ||
<div className="flex flex-1 flex-col items-center justify-center gap-2"> | ||
<p className="text-center text-lg font-medium text-white md:text-gray-900"> | ||
Connectez vous pour continuer: | ||
</p> | ||
{loading ? ( | ||
<div>Loading....</div> | ||
) : ( | ||
<div id="authDiv" data-text="continue_with"></div> | ||
)} | ||
</div> | ||
</div> | ||
<div | ||
className="hidden h-full w-full bg-cover bg-no-repeat md:block" | ||
style={{ backgroundImage: `url(${IMAGES.Auth_background})` }} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default AuthIndex; |
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,18 @@ | ||
import { RotatingSquare } from "react-loader-spinner"; | ||
|
||
const Loading = () => { | ||
return ( | ||
<div className="flex h-screen w-screen items-center justify-center bg-white"> | ||
<RotatingSquare | ||
height="100" | ||
width="100" | ||
color="#1C3988" | ||
ariaLabel="rotating-square-loading" | ||
strokeWidth="4" | ||
visible={true} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Loading; |
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,26 @@ | ||
import Axios from "axios"; | ||
import TokenService from "../services/token.service"; | ||
|
||
const tokenService = TokenService.getInstance(); | ||
|
||
const BASE_URL = process.env.NEXT_PUBLIC_BACKEND_URL; | ||
|
||
const axios = Axios.create({ | ||
baseURL: BASE_URL, | ||
}); | ||
|
||
axios.interceptors.request.use( | ||
(config) => { | ||
const token = tokenService.getAccessToken(); | ||
if (token) { | ||
if (config && config.headers) | ||
config.headers["Authorization"] = `Bearer ${token}`; | ||
} | ||
return config; | ||
}, | ||
(error) => { | ||
return Promise.reject(error); | ||
} | ||
); | ||
|
||
export default axios; |
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 +1,4 @@ | ||
export const IMAGES = {}; | ||
export const IMAGES = { | ||
Logo: "/logo.svg", | ||
Auth_background: "/auth-background.png", | ||
}; |
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 +1,7 @@ | ||
export const INFO = {}; | ||
import { IMAGES } from "./images"; | ||
|
||
export const INFO = { | ||
Title: "Future Immobilier", | ||
Icon: IMAGES.Logo, | ||
Description: "", | ||
}; |
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,4 @@ | ||
export enum ROLES { | ||
ADMIN = "A", | ||
USER = "U", | ||
} |
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,14 @@ | ||
import { ROLES } from "./roles"; | ||
|
||
export const ROUTES = { | ||
AUTH: { | ||
path: "/auth", | ||
pathname: "/auth", | ||
allowedRoles: [] as ROLES[], | ||
}, | ||
HOME: { | ||
path: "/", | ||
pathname: "/", | ||
allowedRoles: [ROLES.ADMIN, ROLES.USER] as ROLES[], | ||
}, | ||
}; |
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,107 @@ | ||
import { useRouter } from "next/router"; | ||
import React, { createContext, useEffect, useMemo, useState } from "react"; | ||
import Loading from "../components/shared/Loading"; | ||
import { ROUTES } from "../constants/routes"; | ||
import TokenService from "../services/token.service"; | ||
import UserService from "../services/user.service"; | ||
import { Auth } from "../typings/user"; | ||
|
||
const tokenService = TokenService.getInstance(); | ||
const userService = UserService.getInstance(); | ||
|
||
export const AuthContext = createContext<{ updateUser?: () => Promise<void> }>( | ||
{} | ||
); | ||
|
||
const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ | ||
children, | ||
}) => { | ||
const router = useRouter(); | ||
const [pageLoading, setPageLoading] = useState<boolean>(true); | ||
const [currentUser, setCurrentUser] = useState<Auth.User | null | undefined>( | ||
undefined | ||
); | ||
|
||
const updateUser = async () => { | ||
try { | ||
const response = await userService.updateUser(); | ||
setCurrentUser(response.data); | ||
} catch (e) { | ||
throw e; | ||
} | ||
}; | ||
|
||
const handleAuth = async () => { | ||
setPageLoading(true); | ||
if (tokenService.getAccessToken()) { | ||
if (router.pathname === ROUTES.AUTH.path) { | ||
await router.replace(ROUTES.HOME.path); | ||
} | ||
const user = userService.getUserInfo(); // get user if exist | ||
if (user) { | ||
setCurrentUser(user); // get the user | ||
setPageLoading(false); | ||
} | ||
try { | ||
await updateUser(); | ||
setPageLoading(false); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
} else { | ||
await router.replace(ROUTES.AUTH.path); | ||
setPageLoading(false); | ||
setCurrentUser(null); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
handleAuth(); | ||
}, []); | ||
|
||
useEffect(() => { | ||
const handleRouteChange = async () => { | ||
if (currentUser !== undefined) { | ||
if (currentUser === null) { | ||
await router.replace(ROUTES.AUTH.path); | ||
} else { | ||
if (router.pathname === ROUTES.AUTH.path) { | ||
await router.replace(ROUTES.HOME.path); | ||
} | ||
} | ||
} | ||
}; | ||
handleRouteChange(); | ||
}, [currentUser]); | ||
|
||
useEffect(() => { | ||
if (currentUser) { | ||
if ( | ||
!Object.entries(ROUTES) | ||
.find(([_, item]) => item.pathname === router.pathname)?.[1] | ||
.allowedRoles.includes(currentUser.role) | ||
) { | ||
router.replace(ROUTES.HOME.path); | ||
} | ||
} | ||
}, [router.pathname, currentUser]); | ||
|
||
const contextValue = useMemo(() => ({ updateUser }), [updateUser]); | ||
|
||
if ( | ||
pageLoading || | ||
currentUser === undefined || | ||
(currentUser && | ||
!Object.entries(ROUTES) | ||
.find(([_, item]) => item.pathname === router.pathname)?.[1] | ||
.allowedRoles.includes(currentUser.role)) | ||
) { | ||
return <Loading />; | ||
} | ||
|
||
return ( | ||
<AuthContext.Provider value={contextValue}>{children}</AuthContext.Provider> | ||
); | ||
}; | ||
|
||
export default AuthProvider; |
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,12 @@ | ||
import { useContext } from "react"; | ||
import { AuthContext } from "../context/AuthContext"; | ||
|
||
const useAuth = () => { | ||
if (AuthContext) { | ||
return useContext(AuthContext); | ||
} else { | ||
throw new Error("AuthProvider is required"); | ||
} | ||
}; | ||
|
||
export default useAuth; |
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,28 @@ | ||
import { useState } from "react"; | ||
import AuthService from "../services/auth.service"; | ||
import TokenService from "../services/token.service"; | ||
import useAuth from "./useAuth"; | ||
|
||
const authService = AuthService.getInstance(); | ||
const tokenService = TokenService.getInstance(); | ||
|
||
const useGoogleAuth = () => { | ||
const [loading, setLoading] = useState<boolean>(false); | ||
const { updateUser } = useAuth(); | ||
|
||
const handleGoogle = async (response: any) => { | ||
setLoading(true); | ||
try { | ||
const res = await authService.postGoogleCredential(response.credential); | ||
tokenService.updateAccessToken(res.data.token); | ||
updateUser && (await updateUser()); | ||
} catch (e) { | ||
console.log(e); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
return { loading, handleGoogle }; | ||
}; | ||
|
||
export default useGoogleAuth; |
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,6 +1,17 @@ | ||
import '../styles/globals.css' | ||
import type { AppProps } from 'next/app' | ||
import { NextSeo } from "next-seo"; | ||
import type { AppProps } from "next/app"; | ||
import { INFO } from "../constants/info"; | ||
import AuthProvider from "../context/AuthContext"; | ||
import "../styles/globals.css"; | ||
|
||
export default function App({ Component, pageProps }: AppProps) { | ||
return <Component {...pageProps} /> | ||
return ( | ||
<AuthProvider> | ||
<NextSeo | ||
titleTemplate={`${INFO.Title} | %s`} | ||
description={INFO.Description} | ||
/> | ||
<Component {...pageProps} /> | ||
</AuthProvider> | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { NextSeo } from "next-seo"; | ||
|
||
import AuthIndex from "../components/auth/AuthIndex"; | ||
|
||
const Auth = () => { | ||
return ( | ||
<> | ||
<NextSeo title="Auth" /> | ||
<AuthIndex /> | ||
</> | ||
); | ||
}; | ||
|
||
export default Auth; |
Oops, something went wrong.