-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
78 lines (70 loc) · 2.37 KB
/
index.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const createFirebaseAuth = ({
ignoredUrls,
serviceAccount,
firebase,
checkEmailVerified = false,
checkEmailVerifiedIgnoredUrls
}) => {
if (!serviceAccount && !firebase) {
/* eslint-disable no-console */
console.log(
'*********************************************************************************'
);
console.log(
'Please provide the Firebase serviceAccount object or an initialized firebase app!'
);
console.log(
'*********************************************************************************'
);
/* eslint-enable no-console */
}
// If the user has passed an initialized firebase app, use that
// or initialize one using the serviceAccount object.
const firebaseAdmin = firebase || require.main.require('firebase-admin');
if (!firebase) {
firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.cert(serviceAccount),
databaseURL: `https://${process.env.FIREBASE_DATABASE_NAME}.firebaseio.com`
});
}
return (req, res, next) => {
if (ignoredUrls && ignoredUrls.includes(req.path)) {
next(); // If the url is in `ignoredUrls`, skip the authorization.
} else {
const authorizationHeader = req.header('Authorization');
// Send an error if the authorization header is missing
if (!authorizationHeader) {
res.status(401);
return res.send({ error: 'Missing authorization header!' });
}
const idToken = authorizationHeader.split(' ').pop();
// Authenticate user
firebaseAdmin
.auth()
.verifyIdToken(idToken)
.then(user => {
// If checkEmailVerified is true, deny the request if the user's email is not verified
// Skip if the url is in checkEmailVerifiedIgnoredUrls
if (
checkEmailVerified &&
(checkEmailVerifiedIgnoredUrls &&
!checkEmailVerifiedIgnoredUrls.includes(req.originalUrl)) &&
!user.email_verified
) {
res.status(401);
return res.send({ error: 'You are not authorized!' });
}
res.locals.user = user; // Set the user object to locals
return next();
})
.catch(error => {
res.status(401);
res.send({ error: 'You are not authorized!' });
next(error);
});
}
};
};
module.exports = {
createFirebaseAuth
};