Skip to main content

UserService

UserService

Contains methods relating to User entities.

Signature
class UserService {
constructor(connection: TransactionalConnection, configService: ConfigService, roleService: RoleService, passwordCipher: PasswordCipher, verificationTokenGenerator: VerificationTokenGenerator, moduleRef: ModuleRef)
getUserById(ctx: RequestContext, userId: ID) => Promise<User | undefined>;
getUserByEmailAddress(ctx: RequestContext, emailAddress: string, userType?: 'administrator' | 'customer') => Promise<User | undefined>;
createCustomerUser(ctx: RequestContext, identifier: string, password?: string) => Promise<User | PasswordValidationError>;
addNativeAuthenticationMethod(ctx: RequestContext, user: User, identifier: string, password?: string) => Promise<User | PasswordValidationError>;
createAdminUser(ctx: RequestContext, identifier: string, password: string) => Promise<User>;
softDelete(ctx: RequestContext, userId: ID) => ;
setVerificationToken(ctx: RequestContext, user: User) => Promise<User>;
verifyUserByToken(ctx: RequestContext, verificationToken: string, password?: string) => Promise<ErrorResultUnion<VerifyCustomerAccountResult, User>>;
setPasswordResetToken(ctx: RequestContext, emailAddress: string) => Promise<User | undefined>;
resetPasswordByToken(ctx: RequestContext, passwordResetToken: string, password: string) => Promise<
User | PasswordResetTokenExpiredError | PasswordResetTokenInvalidError | PasswordValidationError
>;
changeUserAndNativeIdentifier(ctx: RequestContext, userId: ID, newIdentifier: string) => ;
setIdentifierChangeToken(ctx: RequestContext, user: User) => Promise<User>;
changeIdentifierByToken(ctx: RequestContext, token: string) => Promise<
| { user: User; oldIdentifier: string }
| IdentifierChangeTokenInvalidError
| IdentifierChangeTokenExpiredError
>;
updatePassword(ctx: RequestContext, userId: ID, currentPassword: string, newPassword: string) => Promise<boolean | InvalidCredentialsError | PasswordValidationError>;
}

constructor

method
(connection: TransactionalConnection, configService: ConfigService, roleService: RoleService, passwordCipher: PasswordCipher, verificationTokenGenerator: VerificationTokenGenerator, moduleRef: ModuleRef) => UserService

getUserById

method
(ctx: RequestContext, userId: ID) => Promise<User | undefined>

getUserByEmailAddress

method
(ctx: RequestContext, emailAddress: string, userType?: 'administrator' | 'customer') => Promise<User | undefined>

createCustomerUser

method
(ctx: RequestContext, identifier: string, password?: string) => Promise<User | PasswordValidationError>

Creates a new User with the special customer Role and using the NativeAuthenticationStrategy.

addNativeAuthenticationMethod

method
(ctx: RequestContext, user: User, identifier: string, password?: string) => Promise<User | PasswordValidationError>

Adds a new NativeAuthenticationMethod to the User. If the AuthOptions requireVerification is set to true (as is the default), the User will be marked as unverified until the email verification flow is completed.

createAdminUser

method
(ctx: RequestContext, identifier: string, password: string) => Promise<User>

Creates a new verified User using the NativeAuthenticationStrategy.

softDelete

method
(ctx: RequestContext, userId: ID) =>

setVerificationToken

method
(ctx: RequestContext, user: User) => Promise<User>

Sets the NativeAuthenticationMethod verificationToken as part of the User email verification flow.

verifyUserByToken

method
(ctx: RequestContext, verificationToken: string, password?: string) => Promise<ErrorResultUnion<VerifyCustomerAccountResult, User>>

Verifies a verificationToken by looking for a User which has previously had it set using the setVerificationToken() method, and checks that the token is valid and has not expired.

If valid, the User will be set to verified: true.

setPasswordResetToken

method
(ctx: RequestContext, emailAddress: string) => Promise<User | undefined>

Sets the NativeAuthenticationMethod passwordResetToken as part of the User password reset flow.

resetPasswordByToken

method
(ctx: RequestContext, passwordResetToken: string, password: string) => Promise< User | PasswordResetTokenExpiredError | PasswordResetTokenInvalidError | PasswordValidationError >

Verifies a passwordResetToken by looking for a User which has previously had it set using the setPasswordResetToken() method, and checks that the token is valid and has not expired.

If valid, the User's credentials will be updated with the new password.

changeUserAndNativeIdentifier

method
(ctx: RequestContext, userId: ID, newIdentifier: string) =>

Changes the User identifier without an email verification step, so this should be only used when an Administrator is setting a new email address.

setIdentifierChangeToken

method
(ctx: RequestContext, user: User) => Promise<User>

Sets the NativeAuthenticationMethod identifierChangeToken as part of the User email address change flow.

changeIdentifierByToken

method
(ctx: RequestContext, token: string) => Promise< | { user: User; oldIdentifier: string } | IdentifierChangeTokenInvalidError | IdentifierChangeTokenExpiredError >

Changes the User identifier as part of the storefront flow used by Customers to set a new email address, with the token previously set using the setIdentifierChangeToken() method.

updatePassword

method
(ctx: RequestContext, userId: ID, currentPassword: string, newPassword: string) => Promise<boolean | InvalidCredentialsError | PasswordValidationError>

Updates the password for a User with the NativeAuthenticationMethod.