-
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.
Can set firebase admin custom claim and use it in the frontend app
- Loading branch information
1 parent
a101fde
commit 8fcb488
Showing
8 changed files
with
891 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 |
---|---|---|
|
@@ -32,3 +32,6 @@ yarn-error.log* | |
|
||
# vercel | ||
.vercel | ||
|
||
# firebase | ||
serviceAccount.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,20 @@ | ||
import * as admin from 'firebase-admin'; | ||
import type { ServiceAccount } from 'firebase-admin'; | ||
import * as serviceAccount from './serviceAccount.json'; | ||
|
||
const initializeAdminApp = () => { | ||
if (!admin.apps.length) { | ||
try { | ||
admin.initializeApp({ | ||
credential: admin.credential.cert(serviceAccount as ServiceAccount) | ||
}); | ||
} catch { | ||
// eslint-disable-next-line no-console | ||
console.error('Firebase admin initialization error'); | ||
} | ||
} | ||
}; | ||
|
||
initializeAdminApp(); | ||
|
||
export const auth = admin.auth(); |
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,4 +1,5 @@ | ||
export type TUser = { | ||
email: string; | ||
id: string; | ||
imageUrl: string; | ||
name: string; | ||
|
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,16 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
import nc from 'next-connect'; | ||
import { auth } from '~config/firebase/admin'; | ||
|
||
export default nc().post(async (req: NextApiRequest, res: NextApiResponse) => { | ||
try { | ||
const parsedBody = JSON.parse(req.body); | ||
const user = await auth.getUserByEmail(parsedBody.email); | ||
|
||
await auth.setCustomUserClaims(user.uid, { isAdmin: true }); | ||
|
||
res.status(200).json({ message: `User ${user.email} has been made an admin` }); | ||
} catch (e) { | ||
res.status(500).json({ error: e }); | ||
} | ||
}); |
Oops, something went wrong.