1
1
import jwt from 'jsonwebtoken'
2
2
3
- import type { Where } from '../../types/index.js'
3
+ import type { Payload , Where } from '../../types/index.js'
4
4
import type { AuthStrategyFunction , User } from '../index.js'
5
5
6
6
import { extractJWT } from '../extractJWT.js'
@@ -10,6 +10,62 @@ type JWTToken = {
10
10
id : string
11
11
}
12
12
13
+ async function autoLogin ( {
14
+ isGraphQL,
15
+ payload,
16
+ } : {
17
+ isGraphQL : boolean
18
+ payload : Payload
19
+ } ) : Promise < {
20
+ user : User | null
21
+ } > {
22
+ if (
23
+ typeof payload ?. config ?. admin ?. autoLogin !== 'object' ||
24
+ payload . config . admin ?. autoLogin . prefillOnly ||
25
+ ! payload ?. config ?. admin ?. autoLogin ||
26
+ ( ! payload . config . admin ?. autoLogin . email && ! payload . config . admin ?. autoLogin . username )
27
+ ) {
28
+ return { user : null }
29
+ }
30
+
31
+ const collection = payload . collections [ payload . config . admin . user ]
32
+
33
+ const where : Where = {
34
+ or : [ ] ,
35
+ }
36
+ if ( payload . config . admin ?. autoLogin . email ) {
37
+ where . or . push ( {
38
+ email : {
39
+ equals : payload . config . admin ?. autoLogin . email ,
40
+ } ,
41
+ } )
42
+ } else if ( payload . config . admin ?. autoLogin . username ) {
43
+ where . or . push ( {
44
+ username : {
45
+ equals : payload . config . admin ?. autoLogin . username ,
46
+ } ,
47
+ } )
48
+ }
49
+
50
+ const user = (
51
+ await payload . find ( {
52
+ collection : collection . config . slug ,
53
+ depth : isGraphQL ? 0 : collection . config . auth . depth ,
54
+ where,
55
+ } )
56
+ ) . docs [ 0 ]
57
+
58
+ if ( ! user ) {
59
+ return { user : null }
60
+ }
61
+ user . collection = collection . config . slug
62
+ user . _strategy = 'local-jwt'
63
+
64
+ return {
65
+ user : user as User ,
66
+ }
67
+ }
68
+
13
69
export const JWTAuthentication : AuthStrategyFunction = async ( {
14
70
headers,
15
71
isGraphQL = false ,
@@ -18,43 +74,11 @@ export const JWTAuthentication: AuthStrategyFunction = async ({
18
74
try {
19
75
const token = extractJWT ( { headers, payload } )
20
76
21
- if (
22
- ! token &&
23
- typeof payload ?. config ?. admin ?. autoLogin === 'object' &&
24
- ! payload . config . admin ?. autoLogin . prefillOnly &&
25
- headers . get ( 'DisableAutologin' ) !== 'true'
26
- ) {
27
- const collection = payload . collections [ payload . config . admin . user ]
28
-
29
- const where : Where = {
30
- or : [ ] ,
31
- }
32
- if ( payload . config . admin ?. autoLogin . email ) {
33
- where . or . push ( {
34
- email : {
35
- equals : payload . config . admin ?. autoLogin . email ,
36
- } ,
37
- } )
38
- } else if ( payload . config . admin ?. autoLogin . username ) {
39
- where . or . push ( {
40
- username : {
41
- equals : payload . config . admin ?. autoLogin . username ,
42
- } ,
43
- } )
44
- }
45
-
46
- const user = (
47
- await payload . find ( {
48
- collection : collection . config . slug ,
49
- depth : isGraphQL ? 0 : collection . config . auth . depth ,
50
- where,
51
- } )
52
- ) . docs [ 0 ]
53
- user . collection = collection . config . slug
54
- user . _strategy = 'local-jwt'
55
- return {
56
- user : user as User ,
77
+ if ( ! token ) {
78
+ if ( headers . get ( 'DisableAutologin' ) !== 'true' ) {
79
+ return await autoLogin ( { isGraphQL, payload } )
57
80
}
81
+ return { user : null }
58
82
}
59
83
60
84
const decodedPayload = jwt . verify ( token , payload . secret ) as JWTToken & jwt . JwtPayload
@@ -74,9 +98,15 @@ export const JWTAuthentication: AuthStrategyFunction = async ({
74
98
user : user as User ,
75
99
}
76
100
} else {
101
+ if ( headers . get ( 'DisableAutologin' ) !== 'true' ) {
102
+ return await autoLogin ( { isGraphQL, payload } )
103
+ }
77
104
return { user : null }
78
105
}
79
106
} catch ( error ) {
107
+ if ( headers . get ( 'DisableAutologin' ) !== 'true' ) {
108
+ return await autoLogin ( { isGraphQL, payload } )
109
+ }
80
110
return { user : null }
81
111
}
82
112
}
0 commit comments