-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebaseTokenValidation.ts
49 lines (40 loc) · 1.44 KB
/
firebaseTokenValidation.ts
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { NextFunction, Request, Response } from "express";
import admin from 'firebase-admin';
const serviceAccount = require("./serviceAccountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://xxxxxxxx.firebaseio.com"
});
async function validateFirebaseToken(request: Request, response: Response, next: NextFunction) {
let userIdToken = request.headers['authorization'];
if (typeof userIdToken == 'undefined' || userIdToken == null) {
response.status(403).send({
error: "AUTHORIZATION_TOKEN_VALIDATION_ERROR"
})
} else {
const idTokenSplitArray = userIdToken.split(' ');
if (idTokenSplitArray.length == 2) {
userIdToken = idTokenSplitArray[1];
} else {
console.log("Authorization token error");
response.status(403).send({
error: "AUTHORIZATION_TOKEN_VALIDATION_ERROR"
})
}
try {
const decodedToken = await admin.auth().verifyIdToken(userIdToken);
console.log(decodedToken);
next();
} catch (exception) {
console.log(exception);
console.log("Token authorization failed");
response.status(403).send({
error: "AUTHORIZATION_TOKEN_VALIDATION_ERROR",
exception: exception
})
}
}
}
export {
validateFirebaseToken
}