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 method to refresh token, all CRUD Methods work …
…with the database and firebase console.
- Loading branch information
Showing
10 changed files
with
1,089 additions
and
6 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,11 @@ | ||
const admin = require('firebase-admin'); | ||
|
||
require('dotenv').config(); | ||
|
||
console.log('Current working directory:', process.cwd()); | ||
|
||
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,42 @@ | ||
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; | ||
console.log('accessToken3urju3jrwj!', accessToken); | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const express = require('express'); | ||
const { keysToCamel } = require('../common/utils'); | ||
const { db } = require('../server/db'); | ||
|
||
const testRouter = express(); | ||
|
||
testRouter.get('/', async (req, res) => { | ||
try { | ||
const allUsers = await db.query(`SELECT * FROM users;`); | ||
res.status(200).json(keysToCamel(allUsers)); | ||
} catch (err) { | ||
res.status(500).send(err.message); | ||
} | ||
}); | ||
|
||
module.exports = testRouter; |
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.