-
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.
Merge pull request #62 from frontendcafe/issue-49-auth-firestore
[MAIN][FEATURE] Issue 49 auth firestore in server side, refreshtoken
- Loading branch information
Showing
10 changed files
with
130 additions
and
10 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 |
---|---|---|
|
@@ -35,3 +35,6 @@ yarn-error.log* | |
# typescript | ||
*.tsbuildinfo | ||
next-env.d.ts | ||
|
||
# firebase admin SDK private key | ||
privateKey.json |
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 admin, { ServiceAccount } from "firebase-admin"; | ||
|
||
import serviceAccount from "@/privateKey.json"; | ||
|
||
export const verifyIdToken = (token: string) => { | ||
if (!admin.apps.length) { | ||
admin.initializeApp({ | ||
credential: admin.credential.cert(serviceAccount as ServiceAccount), | ||
}); | ||
} | ||
return admin | ||
.auth() | ||
.verifyIdToken(token) | ||
.catch((error: any) => { | ||
throw error; | ||
}); | ||
}; |
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,65 @@ | ||
import React, { createContext, useContext, useEffect, useMemo, useState } from "react"; | ||
import type { User, UserCredential } from "firebase/auth"; | ||
import nookies from "nookies"; | ||
|
||
import { auth } from "@/firebaseConfig"; | ||
|
||
interface AuthProviderProps { | ||
children?: React.ReactNode; | ||
} | ||
|
||
interface AuthContextType { | ||
user: User | UserCredential | null; | ||
setUser: (user: User | UserCredential | null) => void; | ||
} | ||
|
||
const AuthContext = createContext<AuthContextType>({ | ||
user: null, | ||
setUser: () => {}, | ||
}); | ||
|
||
const refreshTokenMinutes = 10 * 60 * 1000; | ||
|
||
export const AuthProvider = ({ children }: AuthProviderProps) => { | ||
const [user, setUser] = useState<User | UserCredential | null>(null); | ||
|
||
useEffect(() => { | ||
if (typeof window !== "undefined") { | ||
(window as any).nookies = nookies; | ||
} | ||
return auth.onIdTokenChanged(async (loggedUser) => { | ||
if (!loggedUser) { | ||
setUser(null); | ||
nookies.destroy(null, "token"); | ||
nookies.set(null, "token", "", { path: "/" }); | ||
return; | ||
} | ||
|
||
const token = await loggedUser.getIdToken(); | ||
setUser(loggedUser); | ||
nookies.destroy(null, "token"); | ||
nookies.set(null, "token", token, { path: "/" }); | ||
}); | ||
}, []); | ||
|
||
// force refresh the token every 10 minutes | ||
useEffect(() => { | ||
const handleRefreshToken = setInterval(async () => { | ||
const updatedUser = auth.currentUser; | ||
if (updatedUser) await updatedUser.getIdToken(true); | ||
}, refreshTokenMinutes); | ||
return () => { | ||
return clearInterval(handleRefreshToken); | ||
}; | ||
}, []); | ||
|
||
const value = useMemo(() => { | ||
return { user, setUser }; | ||
}, [user]); | ||
|
||
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>; | ||
}; | ||
|
||
export const 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
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