-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlogin.ts
More file actions
138 lines (115 loc) · 3.03 KB
/
login.ts
File metadata and controls
138 lines (115 loc) · 3.03 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import type { PayloadRequest, TypedUser } from 'payload'
import {
APIError,
checkLoginPermission,
getFieldsToSign,
incrementLoginAttempts,
jwtSign,
ValidationError,
} from 'payload'
import type { AuthCollectionSlug, FindUserType } from '../types.js'
import { encrypt } from '../utilities/encrypt.js'
import { findUser } from '../utilities/findUser.js'
import { getLoginOptions } from '../utilities/getLoginOptions.js'
type BaseArgs = {
collection: AuthCollectionSlug
otp: string
req: PayloadRequest
}
type Args = BaseArgs & FindUserType
export const loginWithOTP = async ({ type, collection, otp, req, value }: Args) => {
const { context, payload } = req
const collectionConfig = payload.collections[collection].config
const matchedUser = await findUser({
type,
collection,
otp: encrypt({ payload, value: otp }),
payload,
value,
})
const maxLoginAttemptsEnabled = collectionConfig.auth.maxLoginAttempts > 0
if (!matchedUser) {
if (maxLoginAttemptsEnabled) {
await incrementLoginAttempts({
collection: collectionConfig,
doc: matchedUser,
payload: req.payload,
req,
})
}
throw new ValidationError({
collection: collectionConfig.slug,
errors: [
{
message: 'Failed logging in with one-time password.',
path: 'otp',
},
],
})
}
let user: TypedUser = {
...matchedUser,
collection,
}
const { canLoginWithUsername } = getLoginOptions(collectionConfig.auth?.loginWithUsername)
checkLoginPermission({
collection: collectionConfig,
loggingInWithUsername: Boolean(canLoginWithUsername && type === 'username'),
req,
user,
})
if (collectionConfig.hooks?.beforeLogin?.length) {
for (const hook of collectionConfig.hooks.beforeLogin) {
user =
(await hook({
collection: collectionConfig,
context,
req,
user,
})) || user
}
}
req.user = user
const { exp, token } = await jwtSign({
fieldsToSign: getFieldsToSign({ collectionConfig, email: user.emai || '', user }),
secret: payload.secret,
tokenExpiration: collectionConfig.auth.tokenExpiration,
})
if (collectionConfig.hooks?.afterLogin?.length) {
for (const hook of collectionConfig.hooks.afterLogin) {
user =
(await hook({
collection: collectionConfig,
context,
req,
token,
user,
})) || user
}
}
const dataToUpdate: Record<string, unknown> = {
_otp: null,
_otpExpiration: null,
}
if (maxLoginAttemptsEnabled) {
dataToUpdate.lockUntil = null
dataToUpdate.loginAttempts = 0
}
const userData = await payload.db.findOne({
collection,
joins: false,
where: { id: { equals: user.id } },
})
if (!userData) {
throw new APIError(`User with ID=${user.id} was not found.`)
}
await payload.db.updateOne({
id: user.id,
collection,
data: {
...userData,
dataToUpdate,
},
})
return { exp, token, user }
}