-
Notifications
You must be signed in to change notification settings - Fork 0
/
authenticateToken.js
34 lines (31 loc) · 1.34 KB
/
authenticateToken.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const admin = require('firebase-admin');
const serviceAccountKey = {
"type": "service_account",
"project_id": "psychic-fortnight",
"private_key_id": "f41ff9c419c25ed0e9114693ad48c02b0f47e6d2",
"private_key": process.env.FIREBASE_SERVICE_KEY.replace(/\\n/g, '\n'),
"client_email": "firebase-adminsdk-r3ycf@psychic-fortnight.iam.gserviceaccount.com",
"client_id": "108101675999541675334",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-r3ycf%40psychic-fortnight.iam.gserviceaccount.com"
}
admin.initializeApp({
credential: admin.credential.cert(serviceAccountKey),
databaseURL: 'https://psychic-fortnight-default-rtdb.firebaseio.com',
});
async function decodeIDToken(req, res, next) {
const header = req.headers?.authorization;
if (header !== 'Bearer null' && req.headers?.authorization?.startsWith('Bearer ')) {
const idToken = req.headers.authorization.split('Bearer ')[1];
try {
const decodedToken = await admin.auth().verifyIdToken(idToken);
req['currentUser'] = decodedToken;
} catch (err) {
console.log(err);
}
}
next();
}
module.exports = decodeIDToken;