mirror of
https://github.com/kevin-DL/commander_league_web.git
synced 2026-01-12 02:15:17 +00:00
Initial commit from Create Next App
This commit is contained in:
24
utils/auth/firebaseAdmin.js
Normal file
24
utils/auth/firebaseAdmin.js
Normal file
@@ -0,0 +1,24 @@
|
||||
import * as admin from 'firebase-admin'
|
||||
|
||||
export const verifyIdToken = (token) => {
|
||||
const firebasePrivateKey = process.env.FIREBASE_PRIVATE_KEY
|
||||
|
||||
if (!admin.apps.length) {
|
||||
admin.initializeApp({
|
||||
credential: admin.credential.cert({
|
||||
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
|
||||
clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
|
||||
// https://stackoverflow.com/a/41044630/1332513
|
||||
privateKey: firebasePrivateKey.replace(/\\n/g, '\n'),
|
||||
}),
|
||||
databaseURL: process.env.NEXT_PUBLIC_FIREBASE_DATABASE_URL,
|
||||
})
|
||||
}
|
||||
|
||||
return admin
|
||||
.auth()
|
||||
.verifyIdToken(token)
|
||||
.catch((error) => {
|
||||
throw error
|
||||
})
|
||||
}
|
||||
15
utils/auth/initFirebase.js
Normal file
15
utils/auth/initFirebase.js
Normal file
@@ -0,0 +1,15 @@
|
||||
import firebase from 'firebase/app'
|
||||
import 'firebase/auth'
|
||||
|
||||
const config = {
|
||||
apiKey: process.env.NEXT_PUBLIC_FIREBASE_PUBLIC_API_KEY,
|
||||
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
|
||||
databaseURL: process.env.NEXT_PUBLIC_FIREBASE_DATABASE_URL,
|
||||
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
|
||||
}
|
||||
|
||||
export default function initFirebase() {
|
||||
if (!firebase.apps.length) {
|
||||
firebase.initializeApp(config)
|
||||
}
|
||||
}
|
||||
8
utils/auth/mapUserData.js
Normal file
8
utils/auth/mapUserData.js
Normal file
@@ -0,0 +1,8 @@
|
||||
export const mapUserData = (user) => {
|
||||
const { uid, email, xa, ya } = user
|
||||
return {
|
||||
id: uid,
|
||||
email,
|
||||
token: xa || ya,
|
||||
}
|
||||
}
|
||||
63
utils/auth/useUser.js
Normal file
63
utils/auth/useUser.js
Normal file
@@ -0,0 +1,63 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useRouter } from 'next/router'
|
||||
import firebase from 'firebase/app'
|
||||
import 'firebase/auth'
|
||||
import initFirebase from '../auth/initFirebase'
|
||||
import {
|
||||
removeUserCookie,
|
||||
setUserCookie,
|
||||
getUserFromCookie,
|
||||
} from './userCookies'
|
||||
import { mapUserData } from './mapUserData'
|
||||
|
||||
initFirebase()
|
||||
|
||||
const useUser = () => {
|
||||
const [user, setUser] = useState()
|
||||
const router = useRouter()
|
||||
|
||||
const logout = async () => {
|
||||
return firebase
|
||||
.auth()
|
||||
.signOut()
|
||||
.then(() => {
|
||||
// Sign-out successful.
|
||||
router.push('/auth')
|
||||
})
|
||||
.catch((e) => {
|
||||
console.error(e)
|
||||
})
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
// Firebase updates the id token every hour, this
|
||||
// makes sure the react state and the cookie are
|
||||
// both kept up to date
|
||||
const cancelAuthListener = firebase.auth().onIdTokenChanged((user) => {
|
||||
if (user) {
|
||||
const userData = mapUserData(user)
|
||||
setUserCookie(userData)
|
||||
setUser(userData)
|
||||
} else {
|
||||
removeUserCookie()
|
||||
setUser()
|
||||
}
|
||||
})
|
||||
|
||||
const userFromCookie = getUserFromCookie()
|
||||
if (!userFromCookie) {
|
||||
router.push('/')
|
||||
return
|
||||
}
|
||||
setUser(userFromCookie)
|
||||
|
||||
return () => {
|
||||
cancelAuthListener()
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [])
|
||||
|
||||
return { user, logout }
|
||||
}
|
||||
|
||||
export { useUser }
|
||||
19
utils/auth/userCookies.js
Normal file
19
utils/auth/userCookies.js
Normal file
@@ -0,0 +1,19 @@
|
||||
import cookies from 'js-cookie'
|
||||
|
||||
export const getUserFromCookie = () => {
|
||||
const cookie = cookies.get('auth')
|
||||
if (!cookie) {
|
||||
return
|
||||
}
|
||||
return JSON.parse(cookie)
|
||||
}
|
||||
|
||||
export const setUserCookie = (user) => {
|
||||
cookies.set('auth', user, {
|
||||
// firebase id tokens expire in one hour
|
||||
// set cookie expiry to match
|
||||
expires: 1 / 24,
|
||||
})
|
||||
}
|
||||
|
||||
export const removeUserCookie = () => cookies.remove('auth')
|
||||
Reference in New Issue
Block a user