-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
57 lines (50 loc) · 1.24 KB
/
index.ts
File metadata and controls
57 lines (50 loc) · 1.24 KB
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
50
51
52
53
54
55
56
57
import * as admin from "firebase-admin";
import express from "express"
export const validateToken = (req: express.Request, res: express.Response, next: express.NextFunction) => {
const header = req.headers.authorization;
if (!header) {
res.status(401).send({
code: "authorization_header_missing",
description: "Authorization header is expected",
});
return;
}
const parts = header.split(" ");
if (parts[0].toLocaleLowerCase() != "bearer") {
res.status(401);
res.send({
code: "invalid_header",
description: 'Authorization header must start with "Bearer"',
});
return;
}
if (parts.length == 1) {
res.status(401);
res.send({
code: "invalid_header",
description: "Token not found",
});
}
if (parts.length > 2) {
res.status(401);
res.send({
code: "invalid_header",
description: 'Authorization header must be "Bearer TOKEN"',
});
return;
}
const token = parts[1];
admin.auth()
.verifyIdToken(token)
.then((user) => {
(req as any).user = user;
next();
})
.catch(() => {
res.status(401);
res.send({
code: "invalid_header",
description: "Validation of token failed.",
});
});
}