UNPKG

13 kBTypeScriptView Raw
1import type { Application, Id, NullableId, Paginated, Params, Query } from '@feathersjs/feathers';
2export interface User {
3 isVerified: boolean;
4 verifyToken: string;
5 verifyShortToken: string;
6 verifyExpires: Date | number;
7 verifyChanges: VerifyChanges;
8 resetToken: string;
9 resetShortToken: string;
10 resetExpires: Date | number;
11 resetAttempts: number;
12 password: string;
13 [key: string]: any;
14 [key: number]: any;
15}
16export type ArrayOrPaginated<T> = T[] | Paginated<T>;
17export type UsersArrayOrPaginated = ArrayOrPaginated<User>;
18export type NotifierOptions = Record<string, any>;
19export type VerifyChanges = Record<string, any>;
20export interface Tokens {
21 resetToken?: string;
22 resetShortToken?: string;
23 verifyShortToken?: string;
24 verifyToken?: string;
25}
26export type IdentifyUser = Query;
27export type Notifier = (type: NotificationType, user: Partial<User>, notifierOptions?: NotifierOptions) => any;
28export type SanitizeUserForClient = (user: Partial<User>) => SanitizedUser;
29export type SanitizedUser = Partial<User>;
30export type NotificationType = 'resendVerifySignup' | 'verifySignup' | 'verifySignupSetPassword' | 'sendResetPwd' | 'resetPwd' | 'passwordChange' | 'identityChange';
31export type AuthenticationManagementAction = 'checkUnique' | 'resendVerifySignup' | 'verifySignupLong' | 'verifySignupShort' | 'verifySignupSetPasswordLong' | 'verifySignupSetPasswordShort' | 'sendResetPwd' | 'resetPwdLong' | 'resetPwdShort' | 'passwordChange' | 'identityChange' | 'options';
32export type ActionPathMap<T> = {
33 [key in Exclude<AuthenticationManagementAction, 'options'>]: T;
34};
35export type GetUserDataCheckProps = Array<'isNotVerified' | 'isNotVerifiedOrHasVerifyChanges' | 'isVerified' | 'verifyNotExpired' | 'resetNotExpired'>;
36export interface AuthenticationManagementServiceOptions {
37 /** The path of the service for user items.
38 * @default "/users" */
39 service: string;
40 /** If `false` (default) it is impossible to reset passwords even if e-mail is not verified.
41 * @default false */
42 skipIsVerifiedCheck: boolean;
43 /** The notifier function handles the sending of any notification depending on the action.
44 */
45 notifier: Notifier;
46 /** Half the length of the long token. Default is 15, giving tokens of 30 characters length.
47 * @default 15 */
48 longTokenLen: number;
49 /** Length of short token (e.g. for sms).
50 * @default 6 */
51 shortTokenLen: number;
52 /** If `true` short tokens contain only digits. Otherwise also characters.
53 * @default true */
54 shortTokenDigits: boolean;
55 /** Lifetime for password reset tokens in ms. Default is 2*60*60*1000 = 7200000 (2 hours).
56 * @default 7200000 */
57 resetDelay: number;
58 /** Lifetime for e-mail verification tokens in ms. Default is 5*24*60*60*1000 = 432000000 (5 days).
59 * @default 432000000
60 */
61 delay: number;
62 /** Amount of times a user can submit an invalid token before the current token gets removed from the database. Default is 0.
63 * @default 0 */
64 resetAttempts: number;
65 /** Use the same reset token if the user resets password twice in a short period. In this case token is not hashed in the database. Default is false.
66 * @default false */
67 reuseResetToken: boolean;
68 /** Property names in the user item which uniquely identify the user, e.g. `['username', 'email', 'cellphone']`. The default is `['email']`. Only these properties may be changed with verification by the service. At least one of these properties must be provided whenever a short token is used, as the short token alone is too susceptible to brute force attack.
69 * @default ['email']
70 */
71 identifyUserProps: string[];
72 /** Used for sanitization reasions. By default, the user object is in the response e. g. of a password reset request. To reply with empty object use `() => ({})`.
73 * Deletes the following properties by default: `['password', 'verifyExpires', 'verifyToken', 'verifyShortToken', 'verifyChanges', 'resetExpires', 'resetToken', 'resetShortToken']`
74 */
75 sanitizeUserForClient: (user: User) => Partial<User>;
76 /** Property name of the password field on your `'/users'` service
77 * @default 'password' */
78 passwordField: string;
79 /** Should we skip hashing password for `passwordField` ? If `true`, password won't be hashed by feathers-authentication-management when patching the user. This must be set to `true` if you are hashing your password field using resolvers. */
80 skipPasswordHash: boolean;
81 /** Pass params from f-a-m service to `/users` service */
82 passParams: (params: any) => Params | Promise<Params>;
83}
84export type AuthenticationManagementSetupOptions = AuthenticationManagementServiceOptions & {
85 path: string;
86};
87export type VerifySignupLongServiceOptions = Pick<AuthenticationManagementServiceOptions, 'service' | 'notifier' | 'sanitizeUserForClient' | 'passParams'>;
88export type VerifySignupOptions = VerifySignupLongServiceOptions & {
89 app: Application;
90};
91export type VerifySignupShortServiceOptions = VerifySignupLongServiceOptions & {
92 identifyUserProps: string[];
93};
94export type VerifySignupWithShortTokenOptions = VerifySignupShortServiceOptions & {
95 app: Application;
96};
97export type VerifySignupSetPasswordLongServiceOptions = Pick<AuthenticationManagementServiceOptions, 'service' | 'sanitizeUserForClient' | 'notifier' | 'passwordField' | 'skipPasswordHash' | 'passParams'>;
98export type VerifySignupSetPasswordOptions = VerifySignupSetPasswordLongServiceOptions & {
99 app: Application;
100};
101export type PasswordChangeServiceOptions = Pick<AuthenticationManagementServiceOptions, 'service' | 'identifyUserProps' | 'notifier' | 'sanitizeUserForClient' | 'passwordField' | 'skipPasswordHash' | 'passParams'>;
102export type PasswordChangeOptions = PasswordChangeServiceOptions & {
103 app: Application;
104};
105export type VerifySignupSetPasswordShortServiceOptions = VerifySignupSetPasswordLongServiceOptions & Pick<AuthenticationManagementServiceOptions, 'identifyUserProps'>;
106export type VerifySignupSetPasswordWithShortTokenOptions = VerifySignupSetPasswordShortServiceOptions & {
107 app: Application;
108};
109export type ResetPasswordServiceOptions = Pick<AuthenticationManagementServiceOptions, 'service' | 'skipIsVerifiedCheck' | 'reuseResetToken' | 'notifier' | 'sanitizeUserForClient' | 'passwordField' | 'skipPasswordHash' | 'passParams'>;
110export type ResetPasswordOptions = ResetPasswordServiceOptions & {
111 app: Application;
112};
113export type ResetPwdWithShortServiceOptions = ResetPasswordServiceOptions & {
114 identifyUserProps: string[];
115};
116export type ResetPwdWithShortTokenOptions = ResetPwdWithShortServiceOptions & {
117 app: Application;
118};
119export type ResendVerifySignupServiceOptions = Pick<AuthenticationManagementServiceOptions, 'service' | 'identifyUserProps' | 'delay' | 'longTokenLen' | 'shortTokenLen' | 'shortTokenDigits' | 'notifier' | 'sanitizeUserForClient' | 'passParams'>;
120export type ResendVerifySignupOptions = ResendVerifySignupServiceOptions & {
121 app: Application;
122};
123export type IdentityChangeServiceOptions = Pick<AuthenticationManagementServiceOptions, 'service' | 'identifyUserProps' | 'delay' | 'longTokenLen' | 'shortTokenLen' | 'shortTokenDigits' | 'notifier' | 'sanitizeUserForClient' | 'passwordField' | 'passParams'>;
124export type IdentityChangeOptions = IdentityChangeServiceOptions & {
125 app: Application;
126};
127export type CheckUniqueServiceOptions = Pick<AuthenticationManagementServiceOptions, 'service' | 'passParams'>;
128export type CheckUniqueOptions = CheckUniqueServiceOptions & {
129 app: Application;
130};
131export type SendResetPwdServiceOptions = Pick<AuthenticationManagementServiceOptions, 'service' | 'identifyUserProps' | 'skipIsVerifiedCheck' | 'reuseResetToken' | 'resetDelay' | 'sanitizeUserForClient' | 'resetAttempts' | 'shortTokenLen' | 'longTokenLen' | 'shortTokenDigits' | 'notifier' | 'passwordField' | 'passParams'>;
132export type SendResetPwdOptions = SendResetPwdServiceOptions & {
133 app: Application;
134};
135export interface AuthenticationManagementClient {
136 checkUnique: (identifyUser: IdentifyUser, ownId?: NullableId, ifErrMsg?: boolean) => Promise<void>;
137 resendVerifySignup: (identifyUser: IdentifyUser, notifierOptions: NotifierOptions) => Promise<void>;
138 verifySignupLong: (verifyToken: string) => Promise<void>;
139 verifySignupShort: (verifyToken: string, identifyUser: IdentifyUser) => Promise<void>;
140 sendResetPwd: (IdentifyUser: IdentifyUser, notifierOptions: NotifierOptions) => Promise<void>;
141 resetPwdLong: (resetToken: string, password: string) => Promise<void>;
142 resetPwdShort: (resetShortToken: string, identifyUser: IdentifyUser, password: string) => Promise<void>;
143 passwordChange: (oldPassword: string, password: string, identifyUser: IdentifyUser) => Promise<void>;
144 identityChange: (password: string, changesIdentifyUser: NotifierOptions, identifyUser: IdentifyUser) => Promise<void>;
145 authenticate: (email: string, password: string, cb?: (err: Error | null, user?: Partial<User>) => void) => Promise<any>;
146}
147export interface WithNotifierOptions {
148 notifierOptions?: NotifierOptions;
149}
150export type AuthenticationManagementData = DataCheckUniqueWithAction | DataIdentityChangeWithAction | DataOptions | DataPasswordChangeWithAction | DataResendVerifySignupWithAction | DataResetPwdLongWithAction | DataResetPwdShortWithAction | DataSendResetPwdWithAction | DataVerifySignupLongWithAction | DataVerifySignupSetPasswordLongWithAction | DataVerifySignupSetPasswordShortWithAction | DataVerifySignupShortWithAction;
151export interface DataCheckUnique {
152 user: IdentifyUser;
153 ownId?: Id;
154 meta?: {
155 noErrMsg: boolean;
156 };
157}
158export interface DataCheckUniqueWithAction {
159 action: 'checkUnique';
160 value: IdentifyUser;
161 ownId?: Id;
162 meta?: {
163 noErrMsg: boolean;
164 };
165}
166export interface DataIdentityChange extends WithNotifierOptions {
167 changes: Record<string, any>;
168 password: string;
169 user: IdentifyUser;
170}
171export interface DataIdentityChangeWithAction extends WithNotifierOptions {
172 action: 'identityChange';
173 value: {
174 changes: Record<string, any>;
175 password: string;
176 user: IdentifyUser;
177 };
178}
179export interface DataPasswordChange extends WithNotifierOptions {
180 oldPassword: string;
181 password: string;
182 user: IdentifyUser;
183}
184export interface DataPasswordChangeWithAction extends WithNotifierOptions {
185 action: 'passwordChange';
186 value: {
187 oldPassword: string;
188 password: string;
189 user: IdentifyUser;
190 };
191}
192export interface DataResendVerifySignup extends WithNotifierOptions {
193 user: IdentifyUser;
194}
195export interface DataResendVerifySignupWithAction extends WithNotifierOptions {
196 action: 'resendVerifySignup';
197 value: IdentifyUser;
198}
199export interface DataResetPwdLong extends WithNotifierOptions {
200 password: string;
201 token: string;
202}
203export interface DataResetPwdLongWithAction extends WithNotifierOptions {
204 action: 'resetPwdLong';
205 value: {
206 password: string;
207 token: string;
208 };
209}
210export interface DataResetPwdShort extends WithNotifierOptions {
211 password: string;
212 token: string;
213 user: IdentifyUser;
214}
215export interface DataResetPwdShortWithAction extends WithNotifierOptions {
216 action: 'resetPwdShort';
217 value: {
218 password: string;
219 token: string;
220 user: IdentifyUser;
221 };
222}
223export interface DataSendResetPwd extends WithNotifierOptions {
224 user: IdentifyUser;
225}
226export interface DataSendResetPwdWithAction extends WithNotifierOptions {
227 action: 'sendResetPwd';
228 value: IdentifyUser;
229}
230export interface DataVerifySignupLong extends WithNotifierOptions {
231 token: string;
232}
233export interface DataVerifySignupLongWithAction extends WithNotifierOptions {
234 action: 'verifySignupLong';
235 value: string;
236}
237export interface DataVerifySignupSetPasswordLong extends WithNotifierOptions {
238 password: string;
239 token: string;
240}
241export interface DataVerifySignupSetPasswordLongWithAction extends WithNotifierOptions {
242 action: 'verifySignupSetPasswordLong';
243 value: {
244 password: string;
245 token: string;
246 };
247}
248export interface DataVerifySignupSetPasswordShort extends WithNotifierOptions {
249 password: string;
250 token: string;
251 user: IdentifyUser;
252}
253export interface DataVerifySignupSetPasswordShortWithAction extends WithNotifierOptions {
254 action: 'verifySignupSetPasswordShort';
255 value: {
256 password: string;
257 token: string;
258 user: IdentifyUser;
259 };
260}
261export interface DataVerifySignupShort extends WithNotifierOptions {
262 token: string;
263 user: IdentifyUser;
264}
265export interface DataVerifySignupShortWithAction extends WithNotifierOptions {
266 action: 'verifySignupShort';
267 value: {
268 token: string;
269 user: IdentifyUser;
270 };
271}
272export interface DataOptions {
273 action: 'options';
274}
275export interface ClientOptions {
276 path: string;
277}