forked from mate-academy/qa_cypress_e2e_settings_pom_local
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
44 lines (40 loc) · 1.01 KB
/
auth.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
const jwt = require('express-jwt')
const secret = require('./front/config').secret
function getTokenFromHeader(authorization) {
if (
(authorization && authorization.split(' ')[0] === 'Token') ||
(authorization && authorization.split(' ')[0] === 'Bearer')
) {
return authorization.split(' ')[1]
}
return null
}
function getTokenFromRequest(req) {
let ret = getTokenFromHeader(req.headers.authorization)
if (ret) return ret
// If one day we want to allow API GET requests with the cookie.
// Does not work for Next.js routes.
//if (
// req.method === 'GET' ||
// req.method === 'HEAD' ||
// req.method === 'OPTIONS'
//) {
// ret = front.getCookieFromReq(req, 'auth')
// if (ret)
// return ret
//}
return null
}
module.exports = {
required: jwt({
secret: secret,
userProperty: 'payload',
getToken: getTokenFromRequest,
}),
optional: jwt({
secret: secret,
userProperty: 'payload',
credentialsRequired: false,
getToken: getTokenFromRequest,
}),
}