-
Notifications
You must be signed in to change notification settings - Fork 59
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 #13 from iamAravindks/auth-middleware
Auth middleware added
- Loading branch information
Showing
8 changed files
with
2,185 additions
and
149 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,27 @@ | ||
// const firebase = require("firebase-admin"); | ||
import { Request, Response, NextFunction } from 'express'; | ||
import admin from './firebase'; | ||
|
||
declare module 'express-serve-static-core' { | ||
interface Request { | ||
currentUser: admin.auth.DecodedIdToken; | ||
} | ||
} | ||
const auth = async (req: Request, res: Response, next: NextFunction): Promise<void> => { | ||
const headerToken = req.headers?.authorization; | ||
if (headerToken?.startsWith('Bearer ')) { | ||
const idToken = headerToken.split('Bearer ')[1]; | ||
|
||
try { | ||
const decodedToken = await admin.auth().verifyIdToken(idToken); | ||
req.currentUser = decodedToken; | ||
next(); | ||
} catch (err) { | ||
res.status(401).json({ msg: err.message }); | ||
} | ||
} else { | ||
res.status(401).json({ msg: 'Token does not exist ' }); | ||
} | ||
}; | ||
|
||
export default 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import admin from 'firebase-admin'; | ||
import { firebaseAccountCredentials } from '../../../config'; | ||
|
||
const serviceAccount = firebaseAccountCredentials as admin.ServiceAccount; | ||
admin.initializeApp({ | ||
credential: admin.credential.cert(serviceAccount), | ||
}); | ||
|
||
export default 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
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