payload 3.0.0-beta.65 → 3.0.0-beta.67
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auth/baseFields/accountLock.d.ts +1 -2
- package/dist/auth/baseFields/accountLock.d.ts.map +1 -1
- package/dist/auth/baseFields/accountLock.js +1 -1
- package/dist/auth/baseFields/accountLock.js.map +1 -1
- package/dist/auth/baseFields/apiKey.d.ts +1 -2
- package/dist/auth/baseFields/apiKey.d.ts.map +1 -1
- package/dist/auth/baseFields/apiKey.js +1 -2
- package/dist/auth/baseFields/apiKey.js.map +1 -1
- package/dist/auth/baseFields/auth.d.ts +1 -2
- package/dist/auth/baseFields/auth.d.ts.map +1 -1
- package/dist/auth/baseFields/auth.js +1 -2
- package/dist/auth/baseFields/auth.js.map +1 -1
- package/dist/auth/baseFields/email.d.ts +5 -0
- package/dist/auth/baseFields/email.d.ts.map +1 -0
- package/dist/auth/baseFields/email.js +25 -0
- package/dist/auth/baseFields/email.js.map +1 -0
- package/dist/auth/baseFields/username.d.ts +3 -0
- package/dist/auth/baseFields/username.d.ts.map +1 -0
- package/dist/auth/baseFields/username.js +25 -0
- package/dist/auth/baseFields/username.js.map +1 -0
- package/dist/auth/baseFields/verification.d.ts +1 -2
- package/dist/auth/baseFields/verification.d.ts.map +1 -1
- package/dist/auth/baseFields/verification.js +1 -1
- package/dist/auth/baseFields/verification.js.map +1 -1
- package/dist/auth/getAuthFields.d.ts +4 -0
- package/dist/auth/getAuthFields.d.ts.map +1 -0
- package/dist/auth/getAuthFields.js +35 -0
- package/dist/auth/getAuthFields.js.map +1 -0
- package/dist/auth/isLocked.d.ts.map +1 -1
- package/dist/auth/isLocked.js +4 -1
- package/dist/auth/isLocked.js.map +1 -1
- package/dist/auth/operations/forgotPassword.d.ts.map +1 -1
- package/dist/auth/operations/forgotPassword.js +25 -14
- package/dist/auth/operations/forgotPassword.js.map +1 -1
- package/dist/auth/operations/local/unlock.d.ts +5 -7
- package/dist/auth/operations/local/unlock.d.ts.map +1 -1
- package/dist/auth/operations/local/unlock.js.map +1 -1
- package/dist/auth/operations/login.d.ts.map +1 -1
- package/dist/auth/operations/login.js +70 -17
- package/dist/auth/operations/login.js.map +1 -1
- package/dist/auth/operations/resetPassword.d.ts +0 -1
- package/dist/auth/operations/resetPassword.d.ts.map +1 -1
- package/dist/auth/operations/resetPassword.js +0 -1
- package/dist/auth/operations/resetPassword.js.map +1 -1
- package/dist/auth/operations/unlock.d.ts +5 -7
- package/dist/auth/operations/unlock.d.ts.map +1 -1
- package/dist/auth/operations/unlock.js +22 -15
- package/dist/auth/operations/unlock.js.map +1 -1
- package/dist/auth/types.d.ts +7 -2
- package/dist/auth/types.d.ts.map +1 -1
- package/dist/auth/types.js.map +1 -1
- package/dist/collections/config/defaults.d.ts +2 -0
- package/dist/collections/config/defaults.d.ts.map +1 -1
- package/dist/collections/config/defaults.js +4 -0
- package/dist/collections/config/defaults.js.map +1 -1
- package/dist/collections/config/sanitize.d.ts.map +1 -1
- package/dist/collections/config/sanitize.js +6 -22
- package/dist/collections/config/sanitize.js.map +1 -1
- package/dist/collections/config/schema.d.ts.map +1 -1
- package/dist/collections/config/schema.js +4 -1
- package/dist/collections/config/schema.js.map +1 -1
- package/dist/collections/operations/create.d.ts.map +1 -1
- package/dist/collections/operations/create.js +1 -4
- package/dist/collections/operations/create.js.map +1 -1
- package/dist/fields/config/client.d.ts +1 -1
- package/dist/fields/config/client.d.ts.map +1 -1
- package/dist/fields/config/client.js +3 -1
- package/dist/fields/config/client.js.map +1 -1
- package/dist/fields/config/schema.d.ts.map +1 -1
- package/dist/fields/config/schema.js +3 -0
- package/dist/fields/config/schema.js.map +1 -1
- package/dist/fields/config/types.d.ts +9 -0
- package/dist/fields/config/types.d.ts.map +1 -1
- package/dist/fields/config/types.js.map +1 -1
- package/dist/fields/validations.d.ts +1 -0
- package/dist/fields/validations.d.ts.map +1 -1
- package/dist/fields/validations.js +13 -0
- package/dist/fields/validations.js.map +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/uploads/cropImage.d.ts +19 -6
- package/dist/uploads/cropImage.d.ts.map +1 -1
- package/dist/uploads/cropImage.js +4 -4
- package/dist/uploads/cropImage.js.map +1 -1
- package/dist/uploads/generateFileData.d.ts.map +1 -1
- package/dist/uploads/generateFileData.js +3 -1
- package/dist/uploads/generateFileData.js.map +1 -1
- package/dist/uploads/imageResizer.d.ts.map +1 -1
- package/dist/uploads/imageResizer.js +117 -101
- package/dist/uploads/imageResizer.js.map +1 -1
- package/dist/uploads/types.d.ts +16 -10
- package/dist/uploads/types.d.ts.map +1 -1
- package/dist/uploads/types.js.map +1 -1
- package/dist/utilities/configToJSONSchema.d.ts.map +1 -1
- package/dist/utilities/configToJSONSchema.js +103 -42
- package/dist/utilities/configToJSONSchema.js.map +1 -1
- package/dist/utilities/telemetry/conf/envPaths.d.ts +23 -0
- package/dist/utilities/telemetry/conf/envPaths.d.ts.map +1 -0
- package/dist/utilities/telemetry/conf/envPaths.js +70 -0
- package/dist/utilities/telemetry/conf/envPaths.js.map +1 -0
- package/dist/utilities/telemetry/conf/index.d.ts +84 -0
- package/dist/utilities/telemetry/conf/index.d.ts.map +1 -0
- package/dist/utilities/telemetry/conf/index.js +144 -0
- package/dist/utilities/telemetry/conf/index.js.map +1 -0
- package/dist/utilities/telemetry/index.d.ts.map +1 -1
- package/dist/utilities/telemetry/index.js +1 -1
- package/dist/utilities/telemetry/index.js.map +1 -1
- package/package.json +2 -3
- package/dist/auth/baseFields/loginField.d.ts +0 -4
- package/dist/auth/baseFields/loginField.d.ts.map +0 -1
- package/dist/auth/baseFields/loginField.js +0 -35
- package/dist/auth/baseFields/loginField.js.map +0 -1
|
@@ -33,10 +33,14 @@ export const loginOperation = async (incomingArgs)=>{
|
|
|
33
33
|
// Login
|
|
34
34
|
// /////////////////////////////////////
|
|
35
35
|
let user;
|
|
36
|
-
const loginWithUsername = collectionConfig?.auth?.loginWithUsername;
|
|
37
36
|
const { email: unsanitizedEmail, password } = data;
|
|
38
|
-
const
|
|
39
|
-
|
|
37
|
+
const loginWithUsername = collectionConfig.auth.loginWithUsername;
|
|
38
|
+
const sanitizedEmail = typeof unsanitizedEmail === 'string' ? unsanitizedEmail.toLowerCase().trim() : null;
|
|
39
|
+
const sanitizedUsername = 'username' in data && typeof data?.username === 'string' ? data.username.toLowerCase().trim() : null;
|
|
40
|
+
const canLoginWithUsername = Boolean(loginWithUsername);
|
|
41
|
+
const canLoginWithEmail = !loginWithUsername || loginWithUsername.allowEmailLogin;
|
|
42
|
+
// cannot login with email, did not provide username
|
|
43
|
+
if (!canLoginWithEmail && !sanitizedUsername) {
|
|
40
44
|
throw new ValidationError({
|
|
41
45
|
collection: collectionConfig.slug,
|
|
42
46
|
errors: [
|
|
@@ -47,7 +51,8 @@ export const loginOperation = async (incomingArgs)=>{
|
|
|
47
51
|
]
|
|
48
52
|
});
|
|
49
53
|
}
|
|
50
|
-
|
|
54
|
+
// cannot login with username, did not provide email
|
|
55
|
+
if (!canLoginWithUsername && !sanitizedEmail) {
|
|
51
56
|
throw new ValidationError({
|
|
52
57
|
collection: collectionConfig.slug,
|
|
53
58
|
errors: [
|
|
@@ -58,6 +63,23 @@ export const loginOperation = async (incomingArgs)=>{
|
|
|
58
63
|
]
|
|
59
64
|
});
|
|
60
65
|
}
|
|
66
|
+
// can login with either email or username, did not provide either
|
|
67
|
+
if (!sanitizedUsername && !sanitizedEmail) {
|
|
68
|
+
throw new ValidationError({
|
|
69
|
+
collection: collectionConfig.slug,
|
|
70
|
+
errors: [
|
|
71
|
+
{
|
|
72
|
+
field: 'email',
|
|
73
|
+
message: req.i18n.t('validation:required')
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
field: 'username',
|
|
77
|
+
message: req.i18n.t('validation:required')
|
|
78
|
+
}
|
|
79
|
+
]
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
// did not provide password for login
|
|
61
83
|
if (typeof password !== 'string' || password.trim() === '') {
|
|
62
84
|
throw new ValidationError({
|
|
63
85
|
collection: collectionConfig.slug,
|
|
@@ -69,24 +91,55 @@ export const loginOperation = async (incomingArgs)=>{
|
|
|
69
91
|
]
|
|
70
92
|
});
|
|
71
93
|
}
|
|
72
|
-
|
|
94
|
+
let whereConstraint = {};
|
|
95
|
+
const emailConstraint = {
|
|
96
|
+
email: {
|
|
97
|
+
equals: sanitizedEmail
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
const usernameConstraint = {
|
|
101
|
+
username: {
|
|
102
|
+
equals: sanitizedUsername
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
if (canLoginWithEmail && canLoginWithUsername && (sanitizedUsername || sanitizedEmail)) {
|
|
106
|
+
if (sanitizedUsername) {
|
|
107
|
+
whereConstraint = {
|
|
108
|
+
or: [
|
|
109
|
+
usernameConstraint,
|
|
110
|
+
{
|
|
111
|
+
email: {
|
|
112
|
+
equals: sanitizedUsername
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
]
|
|
116
|
+
};
|
|
117
|
+
} else {
|
|
118
|
+
whereConstraint = {
|
|
119
|
+
or: [
|
|
120
|
+
emailConstraint,
|
|
121
|
+
{
|
|
122
|
+
username: {
|
|
123
|
+
equals: sanitizedEmail
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
]
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
} else if (canLoginWithEmail && sanitizedEmail) {
|
|
130
|
+
whereConstraint = emailConstraint;
|
|
131
|
+
} else if (canLoginWithUsername && sanitizedUsername) {
|
|
132
|
+
whereConstraint = usernameConstraint;
|
|
133
|
+
}
|
|
73
134
|
user = await payload.db.findOne({
|
|
74
135
|
collection: collectionConfig.slug,
|
|
75
136
|
req,
|
|
76
|
-
where:
|
|
77
|
-
username: {
|
|
78
|
-
equals: username
|
|
79
|
-
}
|
|
80
|
-
} : {
|
|
81
|
-
email: {
|
|
82
|
-
equals: unsanitizedEmail.toLowerCase()
|
|
83
|
-
}
|
|
84
|
-
}
|
|
137
|
+
where: whereConstraint
|
|
85
138
|
});
|
|
86
139
|
if (!user || args.collection.config.auth.verify && user._verified === false) {
|
|
87
|
-
throw new AuthenticationError(req.t,
|
|
140
|
+
throw new AuthenticationError(req.t, Boolean(canLoginWithUsername && sanitizedUsername));
|
|
88
141
|
}
|
|
89
|
-
if (user && isLocked(user.lockUntil)) {
|
|
142
|
+
if (user && isLocked(new Date(user.lockUntil).getTime())) {
|
|
90
143
|
throw new LockedAuth(req.t);
|
|
91
144
|
}
|
|
92
145
|
const authResult = await authenticateLocalStrategy({
|
|
@@ -117,7 +170,7 @@ export const loginOperation = async (incomingArgs)=>{
|
|
|
117
170
|
}
|
|
118
171
|
const fieldsToSign = getFieldsToSign({
|
|
119
172
|
collectionConfig,
|
|
120
|
-
email,
|
|
173
|
+
email: sanitizedEmail,
|
|
121
174
|
user
|
|
122
175
|
});
|
|
123
176
|
// /////////////////////////////////////
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/auth/operations/login.ts"],"sourcesContent":["import jwt from 'jsonwebtoken'\n\nimport type {\n AuthOperationsFromCollectionSlug,\n Collection,\n DataFromCollectionSlug,\n} from '../../collections/config/types.js'\nimport type { CollectionSlug } from '../../index.js'\nimport type { PayloadRequest } from '../../types/index.js'\nimport type { User } from '../types.js'\n\nimport { buildAfterOperation } from '../../collections/operations/utils.js'\nimport { AuthenticationError, LockedAuth, ValidationError } from '../../errors/index.js'\nimport { afterRead } from '../../fields/hooks/afterRead/index.js'\nimport { commitTransaction } from '../../utilities/commitTransaction.js'\nimport { initTransaction } from '../../utilities/initTransaction.js'\nimport { killTransaction } from '../../utilities/killTransaction.js'\nimport sanitizeInternalFields from '../../utilities/sanitizeInternalFields.js'\nimport { getFieldsToSign } from '../getFieldsToSign.js'\nimport isLocked from '../isLocked.js'\nimport { authenticateLocalStrategy } from '../strategies/local/authenticate.js'\nimport { incrementLoginAttempts } from '../strategies/local/incrementLoginAttempts.js'\nimport { resetLoginAttempts } from '../strategies/local/resetLoginAttempts.js'\n\nexport type Result = {\n exp?: number\n token?: string\n user?: User\n}\n\nexport type Arguments<TSlug extends CollectionSlug> = {\n collection: Collection\n data: AuthOperationsFromCollectionSlug<TSlug>['login']\n depth?: number\n overrideAccess?: boolean\n req: PayloadRequest\n showHiddenFields?: boolean\n}\n\nexport const loginOperation = async <TSlug extends CollectionSlug>(\n incomingArgs: Arguments<TSlug>,\n): Promise<{ user: DataFromCollectionSlug<TSlug> } & Result> => {\n let args = incomingArgs\n\n try {\n const shouldCommit = await initTransaction(args.req)\n\n // /////////////////////////////////////\n // beforeOperation - Collection\n // /////////////////////////////////////\n\n await args.collection.config.hooks.beforeOperation.reduce(async (priorHook, hook) => {\n await priorHook\n\n args =\n (await hook({\n args,\n collection: args.collection?.config,\n context: args.req.context,\n operation: 'login',\n req: args.req,\n })) || args\n }, Promise.resolve())\n\n const {\n collection: { config: collectionConfig },\n data,\n depth,\n overrideAccess,\n req,\n req: {\n fallbackLocale,\n locale,\n payload,\n payload: { secret },\n },\n showHiddenFields,\n } = args\n\n // /////////////////////////////////////\n // Login\n // /////////////////////////////////////\n\n let user\n const loginWithUsername = collectionConfig?.auth?.loginWithUsername\n const { email: unsanitizedEmail, password } = data\n const username = 'username' in data && data.username\n\n if (loginWithUsername && !username) {\n throw new ValidationError({\n collection: collectionConfig.slug,\n errors: [{ field: 'username', message: req.i18n.t('validation:required') }],\n })\n }\n\n if (\n !loginWithUsername &&\n (typeof unsanitizedEmail !== 'string' || unsanitizedEmail.trim() === '')\n ) {\n throw new ValidationError({\n collection: collectionConfig.slug,\n errors: [{ field: 'email', message: req.i18n.t('validation:required') }],\n })\n }\n if (typeof password !== 'string' || password.trim() === '') {\n throw new ValidationError({\n collection: collectionConfig.slug,\n errors: [{ field: 'password', message: req.i18n.t('validation:required') }],\n })\n }\n\n const email = unsanitizedEmail ? unsanitizedEmail.toLowerCase().trim() : null\n\n user = await payload.db.findOne<any>({\n collection: collectionConfig.slug,\n req,\n where:\n loginWithUsername && username\n ? { username: { equals: username } }\n : { email: { equals: unsanitizedEmail.toLowerCase() } },\n })\n\n if (!user || (args.collection.config.auth.verify && user._verified === false)) {\n throw new AuthenticationError(req.t, loginWithUsername)\n }\n\n if (user && isLocked(user.lockUntil)) {\n throw new LockedAuth(req.t)\n }\n\n const authResult = await authenticateLocalStrategy({ doc: user, password })\n\n user = sanitizeInternalFields(user)\n\n const maxLoginAttemptsEnabled = args.collection.config.auth.maxLoginAttempts > 0\n\n if (!authResult) {\n if (maxLoginAttemptsEnabled) {\n await incrementLoginAttempts({\n collection: collectionConfig,\n doc: user,\n payload: req.payload,\n req,\n })\n }\n\n if (shouldCommit) await commitTransaction(req)\n\n throw new AuthenticationError(req.t)\n }\n\n if (maxLoginAttemptsEnabled) {\n await resetLoginAttempts({\n collection: collectionConfig,\n doc: user,\n payload: req.payload,\n req,\n })\n }\n\n const fieldsToSign = getFieldsToSign({\n collectionConfig,\n email,\n user,\n })\n\n // /////////////////////////////////////\n // beforeLogin - Collection\n // /////////////////////////////////////\n\n await collectionConfig.hooks.beforeLogin.reduce(async (priorHook, hook) => {\n await priorHook\n\n user =\n (await hook({\n collection: args.collection?.config,\n context: args.req.context,\n req: args.req,\n user,\n })) || user\n }, Promise.resolve())\n\n const token = jwt.sign(fieldsToSign, secret, {\n expiresIn: collectionConfig.auth.tokenExpiration,\n })\n\n req.user = user\n\n // /////////////////////////////////////\n // afterLogin - Collection\n // /////////////////////////////////////\n\n await collectionConfig.hooks.afterLogin.reduce(async (priorHook, hook) => {\n await priorHook\n\n user =\n (await hook({\n collection: args.collection?.config,\n context: args.req.context,\n req: args.req,\n token,\n user,\n })) || user\n }, Promise.resolve())\n\n // /////////////////////////////////////\n // afterRead - Fields\n // /////////////////////////////////////\n\n user = await afterRead({\n collection: collectionConfig,\n context: req.context,\n depth,\n doc: user,\n draft: undefined,\n fallbackLocale,\n global: null,\n locale,\n overrideAccess,\n req,\n showHiddenFields,\n })\n\n // /////////////////////////////////////\n // afterRead - Collection\n // /////////////////////////////////////\n\n await collectionConfig.hooks.afterRead.reduce(async (priorHook, hook) => {\n await priorHook\n\n user =\n (await hook({\n collection: args.collection?.config,\n context: req.context,\n doc: user,\n req,\n })) || user\n }, Promise.resolve())\n\n // /////////////////////////////////////\n // afterRead - Collection\n // /////////////////////////////////////\n\n await collectionConfig.hooks.afterRead.reduce(async (priorHook, hook) => {\n await priorHook\n\n user =\n (await hook({\n collection: args.collection?.config,\n context: req.context,\n doc: user,\n req,\n })) || user\n }, Promise.resolve())\n\n let result: { user: DataFromCollectionSlug<TSlug> } & Result = {\n exp: (jwt.decode(token) as jwt.JwtPayload).exp,\n token,\n user,\n }\n\n // /////////////////////////////////////\n // afterOperation - Collection\n // /////////////////////////////////////\n\n result = await buildAfterOperation({\n args,\n collection: args.collection?.config,\n operation: 'login',\n result,\n })\n\n // /////////////////////////////////////\n // Return results\n // /////////////////////////////////////\n\n if (shouldCommit) await commitTransaction(req)\n\n return result\n } catch (error: unknown) {\n await killTransaction(args.req)\n throw error\n }\n}\n"],"names":["jwt","buildAfterOperation","AuthenticationError","LockedAuth","ValidationError","afterRead","commitTransaction","initTransaction","killTransaction","sanitizeInternalFields","getFieldsToSign","isLocked","authenticateLocalStrategy","incrementLoginAttempts","resetLoginAttempts","loginOperation","incomingArgs","args","shouldCommit","req","collection","config","hooks","beforeOperation","reduce","priorHook","hook","context","operation","Promise","resolve","collectionConfig","data","depth","overrideAccess","fallbackLocale","locale","payload","secret","showHiddenFields","user","loginWithUsername","auth","email","unsanitizedEmail","password","username","slug","errors","field","message","i18n","t","trim","toLowerCase","db","findOne","where","equals","verify","_verified","lockUntil","authResult","doc","maxLoginAttemptsEnabled","maxLoginAttempts","fieldsToSign","beforeLogin","token","sign","expiresIn","tokenExpiration","afterLogin","draft","undefined","global","result","exp","decode","error"],"mappings":"AAAA,OAAOA,SAAS,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AA8B;QAC9B,wCAAwC;QAExCoC,SAAS,MAAM3E,oBAAoB;YACjCgB;YACAG,YAAYH,KAAKG,UAAU,EAAEC;YAC7BO,WAAW;YACXgD;QACF;QAEA,wCAAwC;QACxC,iBAAiB;QACjB,wCAAwC;QAExC,IAAI1D,cAAc,MAAMZ,kBAAkBa;QAE1C,OAAOyD;IACT,EAAE,OAAOG,OAAgB;QACvB,MAAMvE,gBAAgBS,KAAKE,GAAG;QAC9B,MAAM4D;IACR;AACF,EAAC"}
|
|
1
|
+
{"version":3,"sources":["../../../src/auth/operations/login.ts"],"sourcesContent":["import jwt from 'jsonwebtoken'\n\nimport type {\n AuthOperationsFromCollectionSlug,\n Collection,\n DataFromCollectionSlug,\n} from '../../collections/config/types.js'\nimport type { CollectionSlug } from '../../index.js'\nimport type { PayloadRequest, Where } from '../../types/index.js'\nimport type { User } from '../types.js'\n\nimport { buildAfterOperation } from '../../collections/operations/utils.js'\nimport { AuthenticationError, LockedAuth, ValidationError } from '../../errors/index.js'\nimport { afterRead } from '../../fields/hooks/afterRead/index.js'\nimport { commitTransaction } from '../../utilities/commitTransaction.js'\nimport { initTransaction } from '../../utilities/initTransaction.js'\nimport { killTransaction } from '../../utilities/killTransaction.js'\nimport sanitizeInternalFields from '../../utilities/sanitizeInternalFields.js'\nimport { getFieldsToSign } from '../getFieldsToSign.js'\nimport isLocked from '../isLocked.js'\nimport { authenticateLocalStrategy } from '../strategies/local/authenticate.js'\nimport { incrementLoginAttempts } from '../strategies/local/incrementLoginAttempts.js'\nimport { resetLoginAttempts } from '../strategies/local/resetLoginAttempts.js'\n\nexport type Result = {\n exp?: number\n token?: string\n user?: User\n}\n\nexport type Arguments<TSlug extends CollectionSlug> = {\n collection: Collection\n data: AuthOperationsFromCollectionSlug<TSlug>['login']\n depth?: number\n overrideAccess?: boolean\n req: PayloadRequest\n showHiddenFields?: boolean\n}\n\nexport const loginOperation = async <TSlug extends CollectionSlug>(\n incomingArgs: Arguments<TSlug>,\n): Promise<{ user: DataFromCollectionSlug<TSlug> } & Result> => {\n let args = incomingArgs\n\n try {\n const shouldCommit = await initTransaction(args.req)\n\n // /////////////////////////////////////\n // beforeOperation - Collection\n // /////////////////////////////////////\n\n await args.collection.config.hooks.beforeOperation.reduce(async (priorHook, hook) => {\n await priorHook\n\n args =\n (await hook({\n args,\n collection: args.collection?.config,\n context: args.req.context,\n operation: 'login',\n req: args.req,\n })) || args\n }, Promise.resolve())\n\n const {\n collection: { config: collectionConfig },\n data,\n depth,\n overrideAccess,\n req,\n req: {\n fallbackLocale,\n locale,\n payload,\n payload: { secret },\n },\n showHiddenFields,\n } = args\n\n // /////////////////////////////////////\n // Login\n // /////////////////////////////////////\n\n let user\n const { email: unsanitizedEmail, password } = data\n const loginWithUsername = collectionConfig.auth.loginWithUsername\n\n const sanitizedEmail =\n typeof unsanitizedEmail === 'string' ? unsanitizedEmail.toLowerCase().trim() : null\n const sanitizedUsername =\n 'username' in data && typeof data?.username === 'string'\n ? data.username.toLowerCase().trim()\n : null\n\n const canLoginWithUsername = Boolean(loginWithUsername)\n const canLoginWithEmail = !loginWithUsername || loginWithUsername.allowEmailLogin\n\n // cannot login with email, did not provide username\n if (!canLoginWithEmail && !sanitizedUsername) {\n throw new ValidationError({\n collection: collectionConfig.slug,\n errors: [{ field: 'username', message: req.i18n.t('validation:required') }],\n })\n }\n\n // cannot login with username, did not provide email\n if (!canLoginWithUsername && !sanitizedEmail) {\n throw new ValidationError({\n collection: collectionConfig.slug,\n errors: [{ field: 'email', message: req.i18n.t('validation:required') }],\n })\n }\n\n // can login with either email or username, did not provide either\n if (!sanitizedUsername && !sanitizedEmail) {\n throw new ValidationError({\n collection: collectionConfig.slug,\n errors: [\n { field: 'email', message: req.i18n.t('validation:required') },\n { field: 'username', message: req.i18n.t('validation:required') },\n ],\n })\n }\n\n // did not provide password for login\n if (typeof password !== 'string' || password.trim() === '') {\n throw new ValidationError({\n collection: collectionConfig.slug,\n errors: [{ field: 'password', message: req.i18n.t('validation:required') }],\n })\n }\n\n let whereConstraint: Where = {}\n const emailConstraint: Where = {\n email: {\n equals: sanitizedEmail,\n },\n }\n const usernameConstraint: Where = {\n username: {\n equals: sanitizedUsername,\n },\n }\n\n if (canLoginWithEmail && canLoginWithUsername && (sanitizedUsername || sanitizedEmail)) {\n if (sanitizedUsername) {\n whereConstraint = {\n or: [\n usernameConstraint,\n {\n email: {\n equals: sanitizedUsername,\n },\n },\n ],\n }\n } else {\n whereConstraint = {\n or: [\n emailConstraint,\n {\n username: {\n equals: sanitizedEmail,\n },\n },\n ],\n }\n }\n } else if (canLoginWithEmail && sanitizedEmail) {\n whereConstraint = emailConstraint\n } else if (canLoginWithUsername && sanitizedUsername) {\n whereConstraint = usernameConstraint\n }\n\n user = await payload.db.findOne<any>({\n collection: collectionConfig.slug,\n req,\n where: whereConstraint,\n })\n\n if (!user || (args.collection.config.auth.verify && user._verified === false)) {\n throw new AuthenticationError(req.t, Boolean(canLoginWithUsername && sanitizedUsername))\n }\n\n if (user && isLocked(new Date(user.lockUntil).getTime())) {\n throw new LockedAuth(req.t)\n }\n\n const authResult = await authenticateLocalStrategy({ doc: user, password })\n\n user = sanitizeInternalFields(user)\n\n const maxLoginAttemptsEnabled = args.collection.config.auth.maxLoginAttempts > 0\n\n if (!authResult) {\n if (maxLoginAttemptsEnabled) {\n await incrementLoginAttempts({\n collection: collectionConfig,\n doc: user,\n payload: req.payload,\n req,\n })\n }\n\n if (shouldCommit) await commitTransaction(req)\n\n throw new AuthenticationError(req.t)\n }\n\n if (maxLoginAttemptsEnabled) {\n await resetLoginAttempts({\n collection: collectionConfig,\n doc: user,\n payload: req.payload,\n req,\n })\n }\n\n const fieldsToSign = getFieldsToSign({\n collectionConfig,\n email: sanitizedEmail,\n user,\n })\n\n // /////////////////////////////////////\n // beforeLogin - Collection\n // /////////////////////////////////////\n\n await collectionConfig.hooks.beforeLogin.reduce(async (priorHook, hook) => {\n await priorHook\n\n user =\n (await hook({\n collection: args.collection?.config,\n context: args.req.context,\n req: args.req,\n user,\n })) || user\n }, Promise.resolve())\n\n const token = jwt.sign(fieldsToSign, secret, {\n expiresIn: collectionConfig.auth.tokenExpiration,\n })\n\n req.user = user\n\n // /////////////////////////////////////\n // afterLogin - Collection\n // /////////////////////////////////////\n\n await collectionConfig.hooks.afterLogin.reduce(async (priorHook, hook) => {\n await priorHook\n\n user =\n (await hook({\n collection: args.collection?.config,\n context: args.req.context,\n req: args.req,\n token,\n user,\n })) || user\n }, Promise.resolve())\n\n // /////////////////////////////////////\n // afterRead - Fields\n // /////////////////////////////////////\n\n user = await afterRead({\n collection: collectionConfig,\n context: req.context,\n depth,\n doc: user,\n draft: undefined,\n fallbackLocale,\n global: null,\n locale,\n overrideAccess,\n req,\n showHiddenFields,\n })\n\n // /////////////////////////////////////\n // afterRead - Collection\n // /////////////////////////////////////\n\n await collectionConfig.hooks.afterRead.reduce(async (priorHook, hook) => {\n await priorHook\n\n user =\n (await hook({\n collection: args.collection?.config,\n context: req.context,\n doc: user,\n req,\n })) || user\n }, Promise.resolve())\n\n // /////////////////////////////////////\n // afterRead - Collection\n // /////////////////////////////////////\n\n await collectionConfig.hooks.afterRead.reduce(async (priorHook, hook) => {\n await priorHook\n\n user =\n (await hook({\n collection: args.collection?.config,\n context: req.context,\n doc: user,\n req,\n })) || user\n }, Promise.resolve())\n\n let result: { user: DataFromCollectionSlug<TSlug> } & Result = {\n exp: (jwt.decode(token) as jwt.JwtPayload).exp,\n token,\n user,\n }\n\n // /////////////////////////////////////\n // afterOperation - Collection\n // /////////////////////////////////////\n\n result = await buildAfterOperation({\n args,\n collection: args.collection?.config,\n operation: 'login',\n result,\n })\n\n // /////////////////////////////////////\n // Return results\n // /////////////////////////////////////\n\n if (shouldCommit) await commitTransaction(req)\n\n return result\n } catch (error: unknown) {\n await killTransaction(args.req)\n throw error\n }\n}\n"],"names":["jwt","buildAfterOperation","AuthenticationError","LockedAuth","ValidationError","afterRead","commitTransaction","initTransaction","killTransaction","sanitizeInternalFields","getFieldsToSign","isLocked","authenticateLocalStrategy","incrementLoginAttempts","resetLoginAttempts","loginOperation","incomingArgs","args","shouldCommit","req","collection","config","hooks","beforeOperation","reduce","priorHook","hook","context","operation","Promise","resolve","collectionConfig","data","depth","overrideAccess","fallbackLocale","locale","payload","secret","showHiddenFields","user","email","unsanitizedEmail","password","loginWithUsername","auth","sanitizedEmail","toLowerCase","trim","sanitizedUsername","username","canLoginWithUsername","Boolean","canLoginWithEmail","allowEmailLogin","slug","errors","field","message","i18n","t","whereConstraint","emailConstraint","equals","usernameConstraint","or","db","findOne","where","verify","_verified","Date","lockUntil","getTime","authResult","doc","maxLoginAttemptsEnabled","maxLoginAttempts","fieldsToSign","beforeLogin","token","sign","expiresIn","tokenExpiration","afterLogin","draft","undefined","global","result","exp","decode","error"],"mappings":"AAAA,OAAOA,SAAS,eAAc;AAW9B,SAASC,mBAAmB,QAAQ,wCAAuC;AAC3E,SAASC,mBAAmB,EAAEC,UAAU,EAAEC,eAAe,QAAQ,wBAAuB;AACxF,SAASC,SAAS,QAAQ,wCAAuC;AACjE,SAASC,iBAAiB,QAAQ,uCAAsC;AACxE,SAASC,eAAe,QAAQ,qCAAoC;AACpE,SAASC,eAAe,QAAQ,qCAAoC;AACpE,OAAOC,4BAA4B,4CAA2C;AAC9E,SAASC,eAAe,QAAQ,wBAAuB;AACvD,OAAOC,cAAc,iBAAgB;AACrC,SAASC,yBAAyB,QAAQ,sCAAqC;AAC/E,SAASC,sBAAsB,QAAQ,gDAA+C;AACtF,SAASC,kBAAkB,QAAQ,4CAA2C;AAiB9E,OAAO,MAAMC,iBAAiB,OAC5BC;IAEA,IAAIC,OAAOD;IAEX,IAAI;QACF,MAAME,eAAe,MAAMX,gBAAgBU,KAAKE,GAAG;QAEnD,wCAAwC;QACxC,+BAA+B;QAC/B,wCAAwC;QAExC,MAAMF,KAAKG,UAAU,CAACC,MAAM,CAACC,KAAK,CAACC,eAAe,CAACC,MAAM,CAAC,OAAOC,WAAWC;YAC1E,M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C,MAAMd,KAAK;gBACVN,YAAYH,KAAKG,UAAU,EAAEC;gBAC7BM,SAASR,IAAIQ,OAAO;gBACpBgD,KAAKnC;gBACLrB;YACF,MAAOqB;QACX,GAAGX,QAAQC,OAAO;QAElB,IAAI0D,SAA2D;YAC7DC,KAAK,AAACzF,IAAI0F,MAAM,CAACV,OAA0BS,GAAG;YAC9CT;YACAxC;QACF;QAEA,wCAAwC;QACxC,8BAA8B;QAC9B,wCAAwC;QAExCgD,SAAS,MAAMvF,oBAAoB;YACjCgB;YACAG,YAAYH,KAAKG,UAAU,EAAEC;YAC7BO,WAAW;YACX4D;QACF;QAEA,wCAAwC;QACxC,iBAAiB;QACjB,wCAAwC;QAExC,IAAItE,cAAc,MAAMZ,kBAAkBa;QAE1C,OAAOqE;IACT,EAAE,OAAOG,OAAgB;QACvB,MAAMnF,gBAAgBS,KAAKE,GAAG;QAC9B,MAAMwE;IACR;AACF,EAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resetPassword.d.ts","sourceRoot":"","sources":["../../../src/auth/operations/resetPassword.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mCAAmC,CAAA;AACnE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAU1D,MAAM,MAAM,MAAM,GAAG;IACnB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAC9B,CAAA;AAED,MAAM,MAAM,SAAS,GAAG;IACtB,UAAU,EAAE,UAAU,CAAA;IACtB,IAAI,EAAE;QACJ,QAAQ,EAAE,MAAM,CAAA;QAChB,KAAK,EAAE,MAAM,CAAA;KACd,CAAA;IACD,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB,GAAG,EAAE,cAAc,CAAA;CACpB,CAAA;AAED,eAAO,MAAM,sBAAsB,SAAgB,SAAS,KAAG,OAAO,CAAC,MAAM,CA2F5E,CAAA
|
|
1
|
+
{"version":3,"file":"resetPassword.d.ts","sourceRoot":"","sources":["../../../src/auth/operations/resetPassword.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mCAAmC,CAAA;AACnE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAU1D,MAAM,MAAM,MAAM,GAAG;IACnB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAC9B,CAAA;AAED,MAAM,MAAM,SAAS,GAAG;IACtB,UAAU,EAAE,UAAU,CAAA;IACtB,IAAI,EAAE;QACJ,QAAQ,EAAE,MAAM,CAAA;QAChB,KAAK,EAAE,MAAM,CAAA;KACd,CAAA;IACD,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB,GAAG,EAAE,cAAc,CAAA;CACpB,CAAA;AAED,eAAO,MAAM,sBAAsB,SAAgB,SAAS,KAAG,OAAO,CAAC,MAAM,CA2F5E,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/auth/operations/resetPassword.ts"],"sourcesContent":["import httpStatus from 'http-status'\nimport jwt from 'jsonwebtoken'\n\nimport type { Collection } from '../../collections/config/types.js'\nimport type { PayloadRequest } from '../../types/index.js'\n\nimport { APIError } from '../../errors/index.js'\nimport { commitTransaction } from '../../utilities/commitTransaction.js'\nimport { initTransaction } from '../../utilities/initTransaction.js'\nimport { killTransaction } from '../../utilities/killTransaction.js'\nimport { getFieldsToSign } from '../getFieldsToSign.js'\nimport { authenticateLocalStrategy } from '../strategies/local/authenticate.js'\nimport { generatePasswordSaltHash } from '../strategies/local/generatePasswordSaltHash.js'\n\nexport type Result = {\n token?: string\n user: Record<string, unknown>\n}\n\nexport type Arguments = {\n collection: Collection\n data: {\n password: string\n token: string\n }\n depth?: number\n overrideAccess?: boolean\n req: PayloadRequest\n}\n\nexport const resetPasswordOperation = async (args: Arguments): Promise<Result> => {\n if (\n !Object.prototype.hasOwnProperty.call(args.data, 'token') ||\n !Object.prototype.hasOwnProperty.call(args.data, 'password')\n ) {\n throw new APIError('Missing required data.', httpStatus.BAD_REQUEST)\n }\n\n const {\n collection: { config: collectionConfig },\n data,\n depth,\n overrideAccess,\n req: {\n payload: { secret },\n payload,\n },\n req,\n } = args\n\n try {\n const shouldCommit = await initTransaction(req)\n\n // /////////////////////////////////////\n // Reset Password\n // /////////////////////////////////////\n\n const user = await payload.db.findOne<any>({\n collection: collectionConfig.slug,\n req,\n where: {\n resetPasswordExpiration: { greater_than: new Date() },\n resetPasswordToken: { equals: data.token },\n },\n })\n\n if (!user) throw new APIError('Token is either invalid or has expired.', httpStatus.FORBIDDEN)\n\n // TODO: replace this method\n const { hash, salt } = await generatePasswordSaltHash({\n collection: collectionConfig,\n password: data.password,\n })\n\n user.salt = salt\n user.hash = hash\n\n user.resetPasswordExpiration = new Date().toISOString()\n\n if (collectionConfig.auth.verify) {\n user._verified = true\n }\n\n const doc = await payload.db.updateOne({\n id: user.id,\n collection: collectionConfig.slug,\n data: user,\n req,\n })\n\n await authenticateLocalStrategy({ doc, password: data.password })\n\n const fieldsToSign = getFieldsToSign({\n collectionConfig,\n email: user.email,\n user,\n })\n\n const token = jwt.sign(fieldsToSign, secret, {\n expiresIn: collectionConfig.auth.tokenExpiration,\n })\n\n const fullUser = await payload.findByID({\n id: user.id,\n collection: collectionConfig.slug,\n depth,\n overrideAccess,\n req,\n })\n if (shouldCommit) await commitTransaction(req)\n\n const result = {\n token,\n user: fullUser,\n }\n\n return result\n } catch (error: unknown) {\n await killTransaction(req)\n throw error\n }\n}\n
|
|
1
|
+
{"version":3,"sources":["../../../src/auth/operations/resetPassword.ts"],"sourcesContent":["import httpStatus from 'http-status'\nimport jwt from 'jsonwebtoken'\n\nimport type { Collection } from '../../collections/config/types.js'\nimport type { PayloadRequest } from '../../types/index.js'\n\nimport { APIError } from '../../errors/index.js'\nimport { commitTransaction } from '../../utilities/commitTransaction.js'\nimport { initTransaction } from '../../utilities/initTransaction.js'\nimport { killTransaction } from '../../utilities/killTransaction.js'\nimport { getFieldsToSign } from '../getFieldsToSign.js'\nimport { authenticateLocalStrategy } from '../strategies/local/authenticate.js'\nimport { generatePasswordSaltHash } from '../strategies/local/generatePasswordSaltHash.js'\n\nexport type Result = {\n token?: string\n user: Record<string, unknown>\n}\n\nexport type Arguments = {\n collection: Collection\n data: {\n password: string\n token: string\n }\n depth?: number\n overrideAccess?: boolean\n req: PayloadRequest\n}\n\nexport const resetPasswordOperation = async (args: Arguments): Promise<Result> => {\n if (\n !Object.prototype.hasOwnProperty.call(args.data, 'token') ||\n !Object.prototype.hasOwnProperty.call(args.data, 'password')\n ) {\n throw new APIError('Missing required data.', httpStatus.BAD_REQUEST)\n }\n\n const {\n collection: { config: collectionConfig },\n data,\n depth,\n overrideAccess,\n req: {\n payload: { secret },\n payload,\n },\n req,\n } = args\n\n try {\n const shouldCommit = await initTransaction(req)\n\n // /////////////////////////////////////\n // Reset Password\n // /////////////////////////////////////\n\n const user = await payload.db.findOne<any>({\n collection: collectionConfig.slug,\n req,\n where: {\n resetPasswordExpiration: { greater_than: new Date() },\n resetPasswordToken: { equals: data.token },\n },\n })\n\n if (!user) throw new APIError('Token is either invalid or has expired.', httpStatus.FORBIDDEN)\n\n // TODO: replace this method\n const { hash, salt } = await generatePasswordSaltHash({\n collection: collectionConfig,\n password: data.password,\n })\n\n user.salt = salt\n user.hash = hash\n\n user.resetPasswordExpiration = new Date().toISOString()\n\n if (collectionConfig.auth.verify) {\n user._verified = true\n }\n\n const doc = await payload.db.updateOne({\n id: user.id,\n collection: collectionConfig.slug,\n data: user,\n req,\n })\n\n await authenticateLocalStrategy({ doc, password: data.password })\n\n const fieldsToSign = getFieldsToSign({\n collectionConfig,\n email: user.email,\n user,\n })\n\n const token = jwt.sign(fieldsToSign, secret, {\n expiresIn: collectionConfig.auth.tokenExpiration,\n })\n\n const fullUser = await payload.findByID({\n id: user.id,\n collection: collectionConfig.slug,\n depth,\n overrideAccess,\n req,\n })\n if (shouldCommit) await commitTransaction(req)\n\n const result = {\n token,\n user: fullUser,\n }\n\n return result\n } catch (error: unknown) {\n await killTransaction(req)\n throw error\n }\n}\n"],"names":["httpStatus","jwt","APIError","commitTransaction","initTransaction","killTransaction","getFieldsToSign","authenticateLocalStrategy","generatePasswordSaltHash","resetPasswordOperation","args","Object","prototype","hasOwnProperty","call","data","BAD_REQUEST","collection","config","collectionConfig","depth","overrideAccess","req","payload","secret","shouldCommit","user","db","findOne","slug","where","resetPasswordExpiration","greater_than","Date","resetPasswordToken","equals","token","FORBIDDEN","hash","salt","password","toISOString","auth","verify","_verified","doc","updateOne","id","fieldsToSign","email","sign","expiresIn","tokenExpiration","fullUser","findByID","result","error"],"mappings":"AAAA,OAAOA,gBAAgB,cAAa;AACpC,OAAOC,SAAS,eAAc;AAK9B,SAASC,QAAQ,QAAQ,wBAAuB;AAChD,SAASC,iBAAiB,QAAQ,uCAAsC;AACxE,SAASC,eAAe,QAAQ,qCAAoC;AACpE,SAASC,eAAe,QAAQ,qCAAoC;AACpE,SAASC,eAAe,QAAQ,wBAAuB;AACvD,SAASC,yBAAyB,QAAQ,sCAAqC;AAC/E,SAASC,wBAAwB,QAAQ,kDAAiD;AAkB1F,OAAO,MAAMC,yBAAyB,OAAOC;IAC3C,IACE,CAACC,OAAOC,SAAS,CAACC,cAAc,CAACC,IAAI,CAACJ,KAAKK,IAAI,EAAE,YACjD,CAACJ,OAAOC,SAAS,CAACC,cAAc,CAACC,IAAI,CAACJ,KAAKK,IAAI,EAAE,aACjD;QACA,MAAM,IAAIb,SAAS,0BAA0BF,WAAWgB,WAAW;IACrE;IAEA,MAAM,EACJC,YAAY,EAAEC,QAAQC,gBAAgB,EAAE,EACxCJ,IAAI,EACJK,KAAK,EACLC,cAA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B,QAAQ+B,QAAQ,CAAC;YACtCP,IAAIrB,KAAKqB,EAAE;YACX9B,YAAYE,iBAAiBU,IAAI;YACjCT;YACAC;YACAC;QACF;QACA,IAAIG,cAAc,MAAMtB,kBAAkBmB;QAE1C,MAAMiC,SAAS;YACbnB;YACAV,MAAM2B;QACR;QAEA,OAAOE;IACT,EAAE,OAAOC,OAAgB;QACvB,MAAMnD,gBAAgBiB;QACtB,MAAMkC;IACR;AACF,EAAC"}
|
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
import type { Collection } from '../../collections/config/types.js';
|
|
1
|
+
import type { AuthOperationsFromCollectionSlug, Collection } from '../../collections/config/types.js';
|
|
2
|
+
import type { CollectionSlug } from '../../index.js';
|
|
2
3
|
import type { PayloadRequest } from '../../types/index.js';
|
|
3
|
-
export type
|
|
4
|
+
export type Arguments<TSlug extends CollectionSlug> = {
|
|
4
5
|
collection: Collection;
|
|
5
|
-
data:
|
|
6
|
-
email: string;
|
|
7
|
-
};
|
|
6
|
+
data: AuthOperationsFromCollectionSlug<TSlug>['unlock'];
|
|
8
7
|
overrideAccess?: boolean;
|
|
9
8
|
req: PayloadRequest;
|
|
10
9
|
};
|
|
11
|
-
export declare const unlockOperation: (args:
|
|
12
|
-
export default unlockOperation;
|
|
10
|
+
export declare const unlockOperation: <TSlug extends CollectionSlug>(args: Arguments<TSlug>) => Promise<boolean>;
|
|
13
11
|
//# sourceMappingURL=unlock.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unlock.d.ts","sourceRoot":"","sources":["../../../src/auth/operations/unlock.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"unlock.d.ts","sourceRoot":"","sources":["../../../src/auth/operations/unlock.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,gCAAgC,EAChC,UAAU,EACX,MAAM,mCAAmC,CAAA;AAC1C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AACpD,OAAO,KAAK,EAAE,cAAc,EAAS,MAAM,sBAAsB,CAAA;AASjE,MAAM,MAAM,SAAS,CAAC,KAAK,SAAS,cAAc,IAAI;IACpD,UAAU,EAAE,UAAU,CAAA;IACtB,IAAI,EAAE,gCAAgC,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,CAAA;IACvD,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB,GAAG,EAAE,cAAc,CAAA;CACpB,CAAA;AAED,eAAO,MAAM,eAAe,GAAU,KAAK,SAAS,cAAc,QAC1D,SAAS,CAAC,KAAK,CAAC,KACrB,OAAO,CAAC,OAAO,CAsFjB,CAAA"}
|
|
@@ -6,10 +6,15 @@ import { killTransaction } from '../../utilities/killTransaction.js';
|
|
|
6
6
|
import executeAccess from '../executeAccess.js';
|
|
7
7
|
import { resetLoginAttempts } from '../strategies/local/resetLoginAttempts.js';
|
|
8
8
|
export const unlockOperation = async (args)=>{
|
|
9
|
-
if (!Object.prototype.hasOwnProperty.call(args.data, 'email')) {
|
|
10
|
-
throw new APIError('Missing email.', httpStatus.BAD_REQUEST);
|
|
11
|
-
}
|
|
12
9
|
const { collection: { config: collectionConfig }, overrideAccess, req: { locale }, req } = args;
|
|
10
|
+
const loginWithUsername = collectionConfig.auth.loginWithUsername;
|
|
11
|
+
const canLoginWithUsername = Boolean(loginWithUsername);
|
|
12
|
+
const canLoginWithEmail = !loginWithUsername || loginWithUsername.allowEmailLogin;
|
|
13
|
+
const sanitizedEmail = canLoginWithEmail && (args.data?.email || '').toLowerCase().trim();
|
|
14
|
+
const sanitizedUsername = canLoginWithUsername && 'username' in args.data && typeof args.data.username === 'string' && args.data.username.toLowerCase().trim() || null;
|
|
15
|
+
if (!sanitizedEmail && !sanitizedUsername) {
|
|
16
|
+
throw new APIError(`Missing ${collectionConfig.auth.loginWithUsername ? 'username' : 'email'}.`, httpStatus.BAD_REQUEST);
|
|
17
|
+
}
|
|
13
18
|
try {
|
|
14
19
|
const shouldCommit = await initTransaction(req);
|
|
15
20
|
// /////////////////////////////////////
|
|
@@ -20,25 +25,28 @@ export const unlockOperation = async (args)=>{
|
|
|
20
25
|
req
|
|
21
26
|
}, collectionConfig.access.unlock);
|
|
22
27
|
}
|
|
23
|
-
const options = {
|
|
24
|
-
...args
|
|
25
|
-
};
|
|
26
|
-
const { data } = options;
|
|
27
28
|
// /////////////////////////////////////
|
|
28
29
|
// Unlock
|
|
29
30
|
// /////////////////////////////////////
|
|
30
|
-
|
|
31
|
-
|
|
31
|
+
let whereConstraint = {};
|
|
32
|
+
if (canLoginWithEmail && sanitizedEmail) {
|
|
33
|
+
whereConstraint = {
|
|
34
|
+
email: {
|
|
35
|
+
equals: sanitizedEmail
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
} else if (canLoginWithUsername && sanitizedUsername) {
|
|
39
|
+
whereConstraint = {
|
|
40
|
+
username: {
|
|
41
|
+
equals: sanitizedUsername
|
|
42
|
+
}
|
|
43
|
+
};
|
|
32
44
|
}
|
|
33
45
|
const user = await req.payload.db.findOne({
|
|
34
46
|
collection: collectionConfig.slug,
|
|
35
47
|
locale,
|
|
36
48
|
req,
|
|
37
|
-
where:
|
|
38
|
-
email: {
|
|
39
|
-
equals: data.email.toLowerCase()
|
|
40
|
-
}
|
|
41
|
-
}
|
|
49
|
+
where: whereConstraint
|
|
42
50
|
});
|
|
43
51
|
let result;
|
|
44
52
|
if (user) {
|
|
@@ -59,6 +67,5 @@ export const unlockOperation = async (args)=>{
|
|
|
59
67
|
throw error;
|
|
60
68
|
}
|
|
61
69
|
};
|
|
62
|
-
export default unlockOperation;
|
|
63
70
|
|
|
64
71
|
//# sourceMappingURL=unlock.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/auth/operations/unlock.ts"],"sourcesContent":["import httpStatus from 'http-status'\n\nimport type {
|
|
1
|
+
{"version":3,"sources":["../../../src/auth/operations/unlock.ts"],"sourcesContent":["import httpStatus from 'http-status'\n\nimport type {\n AuthOperationsFromCollectionSlug,\n Collection,\n} from '../../collections/config/types.js'\nimport type { CollectionSlug } from '../../index.js'\nimport type { PayloadRequest, Where } from '../../types/index.js'\n\nimport { APIError } from '../../errors/index.js'\nimport { commitTransaction } from '../../utilities/commitTransaction.js'\nimport { initTransaction } from '../../utilities/initTransaction.js'\nimport { killTransaction } from '../../utilities/killTransaction.js'\nimport executeAccess from '../executeAccess.js'\nimport { resetLoginAttempts } from '../strategies/local/resetLoginAttempts.js'\n\nexport type Arguments<TSlug extends CollectionSlug> = {\n collection: Collection\n data: AuthOperationsFromCollectionSlug<TSlug>['unlock']\n overrideAccess?: boolean\n req: PayloadRequest\n}\n\nexport const unlockOperation = async <TSlug extends CollectionSlug>(\n args: Arguments<TSlug>,\n): Promise<boolean> => {\n const {\n collection: { config: collectionConfig },\n overrideAccess,\n req: { locale },\n req,\n } = args\n\n const loginWithUsername = collectionConfig.auth.loginWithUsername\n const canLoginWithUsername = Boolean(loginWithUsername)\n const canLoginWithEmail = !loginWithUsername || loginWithUsername.allowEmailLogin\n\n const sanitizedEmail = canLoginWithEmail && (args.data?.email || '').toLowerCase().trim()\n const sanitizedUsername =\n (canLoginWithUsername &&\n 'username' in args.data &&\n typeof args.data.username === 'string' &&\n args.data.username.toLowerCase().trim()) ||\n null\n\n if (!sanitizedEmail && !sanitizedUsername) {\n throw new APIError(\n `Missing ${collectionConfig.auth.loginWithUsername ? 'username' : 'email'}.`,\n httpStatus.BAD_REQUEST,\n )\n }\n\n try {\n const shouldCommit = await initTransaction(req)\n\n // /////////////////////////////////////\n // Access\n // /////////////////////////////////////\n\n if (!overrideAccess) {\n await executeAccess({ req }, collectionConfig.access.unlock)\n }\n\n // /////////////////////////////////////\n // Unlock\n // /////////////////////////////////////\n\n let whereConstraint: Where = {}\n\n if (canLoginWithEmail && sanitizedEmail) {\n whereConstraint = {\n email: {\n equals: sanitizedEmail,\n },\n }\n } else if (canLoginWithUsername && sanitizedUsername) {\n whereConstraint = {\n username: {\n equals: sanitizedUsername,\n },\n }\n }\n\n const user = await req.payload.db.findOne({\n collection: collectionConfig.slug,\n locale,\n req,\n where: whereConstraint,\n })\n\n let result\n\n if (user) {\n await resetLoginAttempts({\n collection: collectionConfig,\n doc: user,\n payload: req.payload,\n req,\n })\n result = true\n } else {\n result = null\n }\n\n if (shouldCommit) await commitTransaction(req)\n\n return result\n } catch (error: unknown) {\n await killTransaction(req)\n throw error\n }\n}\n"],"names":["httpStatus","APIError","commitTransaction","initTransaction","killTransaction","executeAccess","resetLoginAttempts","unlockOperation","args","collection","config","collectionConfig","overrideAccess","req","locale","loginWithUsername","auth","canLoginWithUsername","Boolean","canLoginWithEmail","allowEmailLogin","sanitizedEmail","data","email","toLowerCase","trim","sanitizedUsername","username","BAD_REQUEST","shouldCommit","access","unlock","whereConstraint","equals","user","payload","db","findOne","slug","where","result","doc","error"],"mappings":"AAAA,OAAOA,gBAAgB,cAAa;AASpC,SAASC,QAAQ,QAAQ,wBAAuB;AAChD,SAASC,iBAAiB,QAAQ,uCAAsC;AACxE,SAASC,eAAe,QAAQ,qCAAoC;AACpE,SAASC,eAAe,QAAQ,qCAAoC;AACpE,OAAOC,mBAAmB,sBAAqB;AAC/C,SAASC,kBAAkB,QAAQ,4CAA2C;AAS9E,OAAO,MAAMC,kBAAkB,OAC7BC;IAEA,MAAM,EACJC,YAAY,EAAEC,QAAQC,gBAAgB,EAAE,EACxCC,cAAc,EACdC,KAAK,EAAEC,MAAM,EAAE,EACfD,GAAG,EACJ,GAAGL;IAEJ,MAAMO,oBAAoBJ,iBAAiBK,IAAI,CAACD,iBAAiB;IACjE,MAAME,uBAAuBC,QAAQH;IACrC,MAAMI,oBAAoB,CAACJ,qBAAqBA,kBAAkBK,eAAe;IAEjF,MAAMC,iBAAiBF,qBAAqB,AAACX,CAAAA,KAAKc,IAAI,EAAEC,SAAS,EAAC,EAAGC,WAAW,GAAGC,IAAI;IACvF,MAAMC,oBACJ,AAACT,wBACC,cAAcT,KAAKc,I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 +100,10 @@ export type AuthStrategy = {
|
|
|
100
100
|
authenticate: AuthStrategyFunction;
|
|
101
101
|
name: string;
|
|
102
102
|
};
|
|
103
|
+
export type LoginWithUsernameOptions = {
|
|
104
|
+
allowEmailLogin?: boolean;
|
|
105
|
+
requireEmail?: boolean;
|
|
106
|
+
};
|
|
103
107
|
export interface IncomingAuthType {
|
|
104
108
|
cookies?: {
|
|
105
109
|
domain?: string;
|
|
@@ -113,7 +117,7 @@ export interface IncomingAuthType {
|
|
|
113
117
|
generateEmailSubject?: GenerateForgotPasswordEmailSubject;
|
|
114
118
|
};
|
|
115
119
|
lockTime?: number;
|
|
116
|
-
loginWithUsername?: boolean;
|
|
120
|
+
loginWithUsername?: LoginWithUsernameOptions | boolean;
|
|
117
121
|
maxLoginAttempts?: number;
|
|
118
122
|
removeTokenFromResponses?: true;
|
|
119
123
|
strategies?: AuthStrategy[];
|
|
@@ -128,11 +132,12 @@ export type VerifyConfig = {
|
|
|
128
132
|
generateEmailHTML?: GenerateVerifyEmailHTML;
|
|
129
133
|
generateEmailSubject?: GenerateVerifyEmailSubject;
|
|
130
134
|
};
|
|
131
|
-
export interface Auth extends Omit<DeepRequired<IncomingAuthType>, 'forgotPassword' | 'verify'> {
|
|
135
|
+
export interface Auth extends Omit<DeepRequired<IncomingAuthType>, 'forgotPassword' | 'loginWithUsername' | 'verify'> {
|
|
132
136
|
forgotPassword?: {
|
|
133
137
|
generateEmailHTML?: GenerateForgotPasswordEmailHTML;
|
|
134
138
|
generateEmailSubject?: GenerateForgotPasswordEmailSubject;
|
|
135
139
|
};
|
|
140
|
+
loginWithUsername: LoginWithUsernameOptions | false;
|
|
136
141
|
verify?: VerifyConfig | boolean;
|
|
137
142
|
}
|
|
138
143
|
export declare function hasWhereAccessResult(result: Where | boolean): result is Where;
|
package/dist/auth/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/auth/types.ts"],"names":[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
|
package/dist/auth/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/auth/types.ts"],"sourcesContent":["import type { DeepRequired } from 'ts-essentials'\n\nimport type { Payload } from '../index.js'\nimport type { PayloadRequest, Where } from '../types/index.js'\n\nexport type Permission = {\n permission: boolean\n where?: Record<string, unknown>\n}\n\nexport type FieldPermissions = {\n blocks?: {\n [blockSlug: string]: {\n fields: {\n [fieldName: string]: FieldPermissions\n }\n }\n }\n create: {\n permission: boolean\n }\n fields?: {\n [fieldName: string]: FieldPermissions\n }\n read: {\n permission: boolean\n }\n update: {\n permission: boolean\n }\n}\n\nexport type CollectionPermission = {\n create: Permission\n delete: Permission\n fields: {\n [fieldName: string]: FieldPermissions\n }\n read: Permission\n readVersions?: Permission\n update: Permission\n}\n\nexport type GlobalPermission = {\n fields: {\n [fieldName: string]: FieldPermissions\n }\n read: Permission\n readVersions?: Permission\n update: Permission\n}\n\nexport type DocumentPermissions = CollectionPermission | GlobalPermission\nexport type Permissions = {\n canAccessAdmin: boolean\n collections: {\n [collectionSlug: string]: CollectionPermission\n }\n globals?: {\n [globalSlug: string]: GlobalPermission\n }\n}\n\nexport type User = {\n [key: string]: any // This NEEDS to be an any, otherwise it breaks the Omit for ClientUser below\n collection: string\n email?: string\n id: number | string\n username?: string\n}\n\n/**\n * `collection` is not available one the client. It's only available on the server (req.user)\n * On the client, you can access the collection via config.admin.user. Config can be accessed using the useConfig() hook\n */\nexport type ClientUser = Omit<User, 'collection'>\n\ntype GenerateVerifyEmailHTML<TUser = any> = (args: {\n req: PayloadRequest\n token: string\n user: TUser\n}) => Promise<string> | string\n\ntype GenerateVerifyEmailSubject<TUser = any> = (args: {\n req: PayloadRequest\n token: string\n user: TUser\n}) => Promise<string> | string\n\ntype GenerateForgotPasswordEmailHTML<TUser = any> = (args?: {\n req?: PayloadRequest\n token?: string\n user?: TUser\n}) => Promise<string> | string\n\ntype GenerateForgotPasswordEmailSubject<TUser = any> = (args?: {\n req?: PayloadRequest\n token?: string\n user?: TUser\n}) => Promise<string> | string\n\nexport type AuthStrategyFunctionArgs = {\n headers: Request['headers']\n isGraphQL?: boolean\n payload: Payload\n}\n\nexport type AuthStrategyResult = {\n responseHeaders?: Headers\n user: User | null\n}\n\nexport type AuthStrategyFunction = (\n args: AuthStrategyFunctionArgs,\n) => AuthStrategyResult | Promise<AuthStrategyResult>\nexport type AuthStrategy = {\n authenticate: AuthStrategyFunction\n name: string\n}\n\nexport interface IncomingAuthType {\n cookies?: {\n domain?: string\n sameSite?: 'Lax' | 'None' | 'Strict' | boolean\n secure?: boolean\n }\n depth?: number\n disableLocalStrategy?: true\n forgotPassword?: {\n generateEmailHTML?: GenerateForgotPasswordEmailHTML\n generateEmailSubject?: GenerateForgotPasswordEmailSubject\n }\n lockTime?: number\n loginWithUsername?: boolean\n maxLoginAttempts?: number\n removeTokenFromResponses?: true\n strategies?: AuthStrategy[]\n tokenExpiration?: number\n useAPIKey?: boolean\n verify?:\n | {\n generateEmailHTML?: GenerateVerifyEmailHTML\n generateEmailSubject?: GenerateVerifyEmailSubject\n }\n | boolean\n}\n\nexport type VerifyConfig = {\n generateEmailHTML?: GenerateVerifyEmailHTML\n generateEmailSubject?: GenerateVerifyEmailSubject\n}\n\nexport interface Auth
|
|
1
|
+
{"version":3,"sources":["../../src/auth/types.ts"],"sourcesContent":["import type { DeepRequired } from 'ts-essentials'\n\nimport type { Payload } from '../index.js'\nimport type { PayloadRequest, Where } from '../types/index.js'\n\nexport type Permission = {\n permission: boolean\n where?: Record<string, unknown>\n}\n\nexport type FieldPermissions = {\n blocks?: {\n [blockSlug: string]: {\n fields: {\n [fieldName: string]: FieldPermissions\n }\n }\n }\n create: {\n permission: boolean\n }\n fields?: {\n [fieldName: string]: FieldPermissions\n }\n read: {\n permission: boolean\n }\n update: {\n permission: boolean\n }\n}\n\nexport type CollectionPermission = {\n create: Permission\n delete: Permission\n fields: {\n [fieldName: string]: FieldPermissions\n }\n read: Permission\n readVersions?: Permission\n update: Permission\n}\n\nexport type GlobalPermission = {\n fields: {\n [fieldName: string]: FieldPermissions\n }\n read: Permission\n readVersions?: Permission\n update: Permission\n}\n\nexport type DocumentPermissions = CollectionPermission | GlobalPermission\nexport type Permissions = {\n canAccessAdmin: boolean\n collections: {\n [collectionSlug: string]: CollectionPermission\n }\n globals?: {\n [globalSlug: string]: GlobalPermission\n }\n}\n\nexport type User = {\n [key: string]: any // This NEEDS to be an any, otherwise it breaks the Omit for ClientUser below\n collection: string\n email?: string\n id: number | string\n username?: string\n}\n\n/**\n * `collection` is not available one the client. It's only available on the server (req.user)\n * On the client, you can access the collection via config.admin.user. Config can be accessed using the useConfig() hook\n */\nexport type ClientUser = Omit<User, 'collection'>\n\ntype GenerateVerifyEmailHTML<TUser = any> = (args: {\n req: PayloadRequest\n token: string\n user: TUser\n}) => Promise<string> | string\n\ntype GenerateVerifyEmailSubject<TUser = any> = (args: {\n req: PayloadRequest\n token: string\n user: TUser\n}) => Promise<string> | string\n\ntype GenerateForgotPasswordEmailHTML<TUser = any> = (args?: {\n req?: PayloadRequest\n token?: string\n user?: TUser\n}) => Promise<string> | string\n\ntype GenerateForgotPasswordEmailSubject<TUser = any> = (args?: {\n req?: PayloadRequest\n token?: string\n user?: TUser\n}) => Promise<string> | string\n\nexport type AuthStrategyFunctionArgs = {\n headers: Request['headers']\n isGraphQL?: boolean\n payload: Payload\n}\n\nexport type AuthStrategyResult = {\n responseHeaders?: Headers\n user: User | null\n}\n\nexport type AuthStrategyFunction = (\n args: AuthStrategyFunctionArgs,\n) => AuthStrategyResult | Promise<AuthStrategyResult>\nexport type AuthStrategy = {\n authenticate: AuthStrategyFunction\n name: string\n}\n\nexport type LoginWithUsernameOptions = {\n allowEmailLogin?: boolean\n requireEmail?: boolean\n}\n\nexport interface IncomingAuthType {\n cookies?: {\n domain?: string\n sameSite?: 'Lax' | 'None' | 'Strict' | boolean\n secure?: boolean\n }\n depth?: number\n disableLocalStrategy?: true\n forgotPassword?: {\n generateEmailHTML?: GenerateForgotPasswordEmailHTML\n generateEmailSubject?: GenerateForgotPasswordEmailSubject\n }\n lockTime?: number\n loginWithUsername?: LoginWithUsernameOptions | boolean\n maxLoginAttempts?: number\n removeTokenFromResponses?: true\n strategies?: AuthStrategy[]\n tokenExpiration?: number\n useAPIKey?: boolean\n verify?:\n | {\n generateEmailHTML?: GenerateVerifyEmailHTML\n generateEmailSubject?: GenerateVerifyEmailSubject\n }\n | boolean\n}\n\nexport type VerifyConfig = {\n generateEmailHTML?: GenerateVerifyEmailHTML\n generateEmailSubject?: GenerateVerifyEmailSubject\n}\n\nexport interface Auth\n extends Omit<DeepRequired<IncomingAuthType>, 'forgotPassword' | 'loginWithUsername' | 'verify'> {\n forgotPassword?: {\n generateEmailHTML?: GenerateForgotPasswordEmailHTML\n generateEmailSubject?: GenerateForgotPasswordEmailSubject\n }\n loginWithUsername: LoginWithUsernameOptions | false\n verify?: VerifyConfig | boolean\n}\n\nexport function hasWhereAccessResult(result: Where | boolean): result is Where {\n return result && typeof result === 'object'\n}\n"],"names":["hasWhereAccessResult","result"],"mappings":"AAuKA,OAAO,SAASA,qBAAqBC,MAAuB;IAC1D,OAAOA,UAAU,OAAOA,WAAW;AACrC"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { LoginWithUsernameOptions } from '../../auth/types.js';
|
|
1
2
|
export declare const defaults: {
|
|
2
3
|
access: {
|
|
3
4
|
create: ({ req: { user } }: {
|
|
@@ -66,4 +67,5 @@ export declare const authDefaults: {
|
|
|
66
67
|
tokenExpiration: number;
|
|
67
68
|
verify: boolean;
|
|
68
69
|
};
|
|
70
|
+
export declare const loginWithUsernameDefaults: LoginWithUsernameOptions;
|
|
69
71
|
//# sourceMappingURL=defaults.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"defaults.d.ts","sourceRoot":"","sources":["../../../src/collections/config/defaults.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"defaults.d.ts","sourceRoot":"","sources":["../../../src/collections/config/defaults.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,qBAAqB,CAAA;AAInE,eAAO,MAAM,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA6CpB,CAAA;AAED,eAAO,MAAM,YAAY;;;;;;;;;;;CAWxB,CAAA;AAED,eAAO,MAAM,yBAAyB,EAAE,wBAGvC,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/collections/config/defaults.ts"],"sourcesContent":["import defaultAccess from '../../auth/defaultAccess.js'\n\nexport const defaults = {\n access: {\n create: defaultAccess,\n delete: defaultAccess,\n read: defaultAccess,\n unlock: defaultAccess,\n update: defaultAccess,\n },\n admin: {\n components: {},\n custom: {},\n enableRichTextLink: true,\n enableRichTextRelationship: true,\n pagination: {\n defaultLimit: 10,\n limits: [5, 10, 25, 50, 100],\n },\n useAsTitle: 'id',\n },\n auth: false,\n custom: {},\n endpoints: [],\n fields: [],\n hooks: {\n afterChange: [],\n afterDelete: [],\n afterForgotPassword: [],\n afterLogin: [],\n afterLogout: [],\n afterMe: [],\n afterOperation: [],\n afterRead: [],\n afterRefresh: [],\n beforeChange: [],\n beforeDelete: [],\n beforeLogin: [],\n beforeOperation: [],\n beforeRead: [],\n beforeValidate: [],\n me: [],\n refresh: [],\n },\n timestamps: true,\n upload: false,\n versions: false,\n}\n\nexport const authDefaults = {\n cookies: {\n sameSite: 'Lax',\n secure: false,\n },\n forgotPassword: {},\n lockTime: 600000, // 10 minutes\n loginWithUsername: false,\n maxLoginAttempts: 5,\n tokenExpiration: 7200,\n verify: false,\n}\n"],"names":["defaultAccess","defaults","access","create","delete","read","unlock","update","admin","components","custom","enableRichTextLink","enableRichTextRelationship","pagination","defaultLimit","limits","useAsTitle","auth","endpoints","fields","hooks","afterChange","afterDelete","afterForgotPassword","afterLogin","afterLogout","afterMe","afterOperation","afterRead","afterRefresh","beforeChange","beforeDelete","beforeLogin","beforeOperation","beforeRead","beforeValidate","me","refresh","timestamps","upload","versions","authDefaults","cookies","sameSite","secure","forgotPassword","lockTime","loginWithUsername","maxLoginAttempts","tokenExpiration","verify"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../../src/collections/config/defaults.ts"],"sourcesContent":["import type { LoginWithUsernameOptions } from '../../auth/types.js'\n\nimport defaultAccess from '../../auth/defaultAccess.js'\n\nexport const defaults = {\n access: {\n create: defaultAccess,\n delete: defaultAccess,\n read: defaultAccess,\n unlock: defaultAccess,\n update: defaultAccess,\n },\n admin: {\n components: {},\n custom: {},\n enableRichTextLink: true,\n enableRichTextRelationship: true,\n pagination: {\n defaultLimit: 10,\n limits: [5, 10, 25, 50, 100],\n },\n useAsTitle: 'id',\n },\n auth: false,\n custom: {},\n endpoints: [],\n fields: [],\n hooks: {\n afterChange: [],\n afterDelete: [],\n afterForgotPassword: [],\n afterLogin: [],\n afterLogout: [],\n afterMe: [],\n afterOperation: [],\n afterRead: [],\n afterRefresh: [],\n beforeChange: [],\n beforeDelete: [],\n beforeLogin: [],\n beforeOperation: [],\n beforeRead: [],\n beforeValidate: [],\n me: [],\n refresh: [],\n },\n timestamps: true,\n upload: false,\n versions: false,\n}\n\nexport const authDefaults = {\n cookies: {\n sameSite: 'Lax',\n secure: false,\n },\n forgotPassword: {},\n lockTime: 600000, // 10 minutes\n loginWithUsername: false,\n maxLoginAttempts: 5,\n tokenExpiration: 7200,\n verify: false,\n}\n\nexport const loginWithUsernameDefaults: LoginWithUsernameOptions = {\n allowEmailLogin: false,\n requireEmail: false,\n}\n"],"names":["defaultAccess","defaults","access","create","delete","read","unlock","update","admin","components","custom","enableRichTextLink","enableRichTextRelationship","pagination","defaultLimit","limits","useAsTitle","auth","endpoints","fields","hooks","afterChange","afterDelete","afterForgotPassword","afterLogin","afterLogout","afterMe","afterOperation","afterRead","afterRefresh","beforeChange","beforeDelete","beforeLogin","beforeOperation","beforeRead","beforeValidate","me","refresh","timestamps","upload","versions","authDefaults","cookies","sameSite","secure","forgotPassword","lockTime","loginWithUsername","maxLoginAttempts","tokenExpiration","verify","loginWithUsernameDefaults","allowEmailLogin","requireEmail"],"mappings":"AAEA,OAAOA,mBAAmB,8BAA6B;AAEvD,OAAO,MAAMC,WAAW;IACtBC,QAAQ;QACNC,QAAQH;QACRI,QAAQJ;QACRK,MAAML;QACNM,QAAQN;QACRO,QAAQP;IACV;IACAQ,OAAO;QACLC,YAAY,CAAC;QACbC,QAAQ,CAAC;QACTC,oBAAoB;QACpBC,4BAA4B;QAC5BC,YAAY;YACVC,cAAc;YACdC,QAAQ;gBAAC;gBAAG;gBAAI;gBAAI;gBAAI;aAAI;QAC9B;QACAC,YAAY;IACd;IACAC,MAAM;IACNP,QAAQ,CAAC;IACTQ,WAAW,EAAE;IACbC,QAAQ,EAAE;IACVC,OAAO;QACLC,aAAa,EAAE;QACfC,aAAa,EAAE;QACfC,qBAAqB,EAAE;QACvBC,YAAY,EAAE;QACdC,aAAa,EAAE;QACfC,SAAS,EAAE;QACXC,gBAAgB,EAAE;QAClBC,WAAW,EAAE;QACbC,cAAc,EAAE;QAChBC,cAAc,EAAE;QAChBC,cAAc,EAAE;QAChBC,aAAa,EAAE;QACfC,iBAAiB,EAAE;QACnBC,YAAY,EAAE;QACdC,gBAAgB,EAAE;QAClBC,IAAI,EAAE;QACNC,SAAS,EAAE;IACb;IACAC,YAAY;IACZC,QAAQ;IACRC,UAAU;AACZ,EAAC;AAED,OAAO,MAAMC,eAAe;IAC1BC,SAAS;QACPC,UAAU;QACVC,QAAQ;IACV;IACAC,gBAAgB,CAAC;IACjBC,UAAU;IACVC,mBAAmB;IACnBC,kBAAkB;IAClBC,iBAAiB;IACjBC,QAAQ;AACV,EAAC;AAED,OAAO,MAAMC,4BAAsD;IACjEC,iBAAiB;IACjBC,cAAc;AAChB,EAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sanitize.d.ts","sourceRoot":"","sources":["../../../src/collections/config/sanitize.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AACpE,OAAO,KAAK,EAAE,gBAAgB,EAAE,yBAAyB,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"sanitize.d.ts","sourceRoot":"","sources":["../../../src/collections/config/sanitize.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AACpE,OAAO,KAAK,EAAE,gBAAgB,EAAE,yBAAyB,EAAE,MAAM,YAAY,CAAA;AAc7E,eAAO,MAAM,kBAAkB,WACrB,MAAM,cACF,gBAAgB,iCAKG,KAAK,CAAC,CAAC,MAAM,EAAE,eAAe,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC,KAC/E,OAAO,CAAC,yBAAyB,CA0InC,CAAA"}
|
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
import merge from 'deepmerge';
|
|
2
|
-
import
|
|
3
|
-
import baseAPIKeyFields from '../../auth/baseFields/apiKey.js';
|
|
4
|
-
import baseAuthFields from '../../auth/baseFields/auth.js';
|
|
5
|
-
import baseLoginField from '../../auth/baseFields/loginField.js';
|
|
6
|
-
import baseVerificationFields from '../../auth/baseFields/verification.js';
|
|
2
|
+
import { getBaseAuthFields } from '../../auth/getAuthFields.js';
|
|
7
3
|
import { TimestampsRequired } from '../../errors/TimestampsRequired.js';
|
|
8
4
|
import { sanitizeFields } from '../../fields/config/sanitize.js';
|
|
9
5
|
import { fieldAffectsData } from '../../fields/config/types.js';
|
|
@@ -13,7 +9,7 @@ import { formatLabels } from '../../utilities/formatLabels.js';
|
|
|
13
9
|
import { isPlainObject } from '../../utilities/isPlainObject.js';
|
|
14
10
|
import baseVersionFields from '../../versions/baseFields.js';
|
|
15
11
|
import { versionDefaults } from '../../versions/defaults.js';
|
|
16
|
-
import { authDefaults, defaults } from './defaults.js';
|
|
12
|
+
import { authDefaults, defaults, loginWithUsernameDefaults } from './defaults.js';
|
|
17
13
|
export const sanitizeCollection = async (config, collection, /**
|
|
18
14
|
* If this property is set, RichText fields won't be sanitized immediately. Instead, they will be added to this array as promises
|
|
19
15
|
* so that you can sanitize them together, after the config has been sanitized.
|
|
@@ -112,28 +108,16 @@ export const sanitizeCollection = async (config, collection, /**
|
|
|
112
108
|
sanitized.auth = merge(authDefaults, typeof sanitized.auth === 'object' ? sanitized.auth : {}, {
|
|
113
109
|
isMergeableObject: isPlainObject
|
|
114
110
|
});
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
authFields = authFields.concat(baseAPIKeyFields);
|
|
118
|
-
}
|
|
119
|
-
if (!sanitized.auth.disableLocalStrategy) {
|
|
120
|
-
const loginField = sanitized.auth.loginWithUsername ? 'username' : 'email';
|
|
121
|
-
authFields = authFields.concat(baseLoginField(loginField));
|
|
122
|
-
authFields = authFields.concat(baseAuthFields);
|
|
123
|
-
if (sanitized.auth.verify) {
|
|
124
|
-
if (sanitized.auth.verify === true) sanitized.auth.verify = {};
|
|
125
|
-
authFields = authFields.concat(baseVerificationFields);
|
|
126
|
-
}
|
|
127
|
-
if (sanitized.auth.maxLoginAttempts > 0) {
|
|
128
|
-
authFields = authFields.concat(baseAccountLockFields);
|
|
129
|
-
}
|
|
111
|
+
if (!sanitized.auth.disableLocalStrategy && sanitized.auth.verify === true) {
|
|
112
|
+
sanitized.auth.verify = {};
|
|
130
113
|
}
|
|
131
114
|
// disable duplicate for auth enabled collections by default
|
|
132
115
|
sanitized.disableDuplicate = sanitized.disableDuplicate || true;
|
|
133
116
|
if (!sanitized.auth.strategies) {
|
|
134
117
|
sanitized.auth.strategies = [];
|
|
135
118
|
}
|
|
136
|
-
sanitized.
|
|
119
|
+
sanitized.auth.loginWithUsername = sanitized.auth.loginWithUsername ? merge(loginWithUsernameDefaults, typeof sanitized.auth.loginWithUsername === 'boolean' ? {} : sanitized.auth.loginWithUsername) : false;
|
|
120
|
+
sanitized.fields = mergeBaseFields(sanitized.fields, getBaseAuthFields(sanitized.auth));
|
|
137
121
|
}
|
|
138
122
|
return sanitized;
|
|
139
123
|
};
|