generated from ctc-uci/npo-backend-template
-
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.
Protected routes have token refresh method, all CRUD methods work wit…
…h the user database and firebase console (#37) * protected routes have method to refresh token, all CRUD Methods work with the database and firebase console. * took out console logs * removed test code --------- Co-authored-by: subinqkim <[email protected]> Co-authored-by: michellelin1 <[email protected]>
- Loading branch information
1 parent
39cccb0
commit 1e5863b
Showing
9 changed files
with
1,067 additions
and
7 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
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,9 @@ | ||
const admin = require('firebase-admin'); | ||
|
||
require('dotenv').config(); | ||
|
||
const credentials = require('./firebase-adminsdk.json'); | ||
|
||
admin.initializeApp({ credential: admin.credential.cert(credentials) }); | ||
|
||
module.exports = admin; |
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,41 @@ | ||
const express = require('express'); | ||
|
||
const authRouter = express(); | ||
const admin = require('../firebase'); | ||
|
||
authRouter.use(express.json()); | ||
|
||
// This method makes a call to Firebase that will verify the access token attached to the request's cookies | ||
// This method is used to make sure that only users who have appropriate access tokens can access backend routes. | ||
const verifyToken = async (req, res, next) => { | ||
try { | ||
const { | ||
cookies: { accessToken }, | ||
} = req; | ||
if (!accessToken) { | ||
return res.status(400).send('@verifyToken no access token'); | ||
} | ||
const decodedToken = await admin.auth().verifyIdToken(accessToken); | ||
if (!decodedToken) { | ||
return res.status(400).send('Empty token from firebase'); | ||
} | ||
return next(); | ||
} catch (err) { | ||
return res.status(400).send('@verifyToken no access token'); | ||
} | ||
}; | ||
|
||
// This method makes a call to firebase that will verify the access token attached to the request's cookies | ||
// This method is used to make sure that only users who have appropriate access tokens can access frontend routes. | ||
authRouter.get('/verifyToken/:accessToken', async (req, res) => { | ||
try { | ||
const { accessToken } = req.params; | ||
const decodedToken = await admin.auth().verifyIdToken(accessToken); | ||
return res.status(200).send(decodedToken.uid); | ||
} catch (err) { | ||
console.log('err', err); | ||
return res.status(400).send('@verifyToken no access token'); | ||
} | ||
}); | ||
|
||
module.exports = { verifyToken, authRouter }; |
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
Oops, something went wrong.