-
Notifications
You must be signed in to change notification settings - Fork 0
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
40f3286
commit 0ed80cc
Showing
8 changed files
with
134 additions
and
33 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,20 @@ | ||
import { getApps, initializeApp } from 'firebase/app'; | ||
import { getAuth } from 'firebase/auth'; | ||
|
||
const firebaseClientConfig = { | ||
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY, | ||
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN, | ||
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID, | ||
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET, | ||
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID, | ||
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID | ||
}; | ||
|
||
function initFirebase() { | ||
const apps = getApps(); | ||
if (!apps.length) initializeApp(firebaseClientConfig); | ||
} | ||
|
||
initFirebase(); | ||
|
||
export const auth = getAuth(); |
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,83 @@ | ||
import cookies from 'js-cookie'; | ||
import { GoogleAuthProvider, onIdTokenChanged, signInWithPopup, signOut } from 'firebase/auth'; | ||
import type { User } from 'firebase/auth'; | ||
import { useRouter } from 'next/router'; | ||
import { createContext, useContext, useEffect, useState } from 'react'; | ||
import { auth } from '~config/firebase/client'; | ||
import PATHS from '~constants/paths'; | ||
import { ReactNode } from 'hoist-non-react-statics/node_modules/@types/react'; | ||
|
||
interface IAuthContext { | ||
authenticated: boolean; | ||
logout: () => void; | ||
signInWithGoogle: () => void; | ||
user: User | null; | ||
} | ||
|
||
const AuthContext = createContext<IAuthContext>({ | ||
authenticated: false, | ||
logout: () => null, | ||
signInWithGoogle: () => null, | ||
user: null | ||
}); | ||
|
||
export const AuthProvider = ({ children }: { children: ReactNode }) => { | ||
const [user, setUser] = useState<User | null>(null); | ||
const router = useRouter(); | ||
|
||
const setTokenCookie = (token: string) => { | ||
cookies.set('token', token, { | ||
expires: 1 / 24 | ||
}); | ||
}; | ||
|
||
const removeTokenCookie = () => cookies.remove('token'); | ||
|
||
const signInWithGoogle = () => { | ||
const provider = new GoogleAuthProvider(); | ||
signInWithPopup(auth, provider) | ||
.then(() => { | ||
router.push(PATHS.HOME); | ||
}) | ||
.catch((e) => { | ||
throw new Error(`Error signing in: ${e}`); | ||
}); | ||
}; | ||
|
||
const logout = () => { | ||
signOut(auth) | ||
.then(() => { | ||
router.push(PATHS.HOME); | ||
}) | ||
.catch((e) => { | ||
throw new Error(`Error signing out: ${e}`); | ||
}); | ||
}; | ||
|
||
useEffect(() => { | ||
const cancelAuthListener = onIdTokenChanged(auth, async (user) => { | ||
if (user) { | ||
const token = await user.getIdToken(); | ||
setTokenCookie(token); | ||
setUser(user); | ||
} else { | ||
removeTokenCookie(); | ||
setUser(null); | ||
} | ||
}); | ||
|
||
return () => { | ||
cancelAuthListener(); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<AuthContext.Provider value={{ user, authenticated: !!user, logout, signInWithGoogle }}> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
}; | ||
|
||
export function useAuth() { | ||
return useContext(AuthContext); | ||
} |
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
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,11 +1,14 @@ | ||
import type { AppProps } from 'next/app'; | ||
import { AuthProvider } from '~hooks/useAuth'; | ||
import GlobalStyles from '~styles/GlobalStyles'; | ||
|
||
function MyApp({ Component, pageProps }: AppProps) { | ||
return ( | ||
<GlobalStyles> | ||
<Component {...pageProps} /> | ||
</GlobalStyles> | ||
<AuthProvider> | ||
<GlobalStyles> | ||
<Component {...pageProps} /> | ||
</GlobalStyles> | ||
</AuthProvider> | ||
); | ||
} | ||
export default MyApp; |
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