🚨 Announcing Vendure v2 Beta

UserService

UserService

Contains methods relating to User entities.

Signature

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

Members

constructor

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

getUserById

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

getUserByEmailAddress

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

createCustomerUser

async method
type:
(ctx: RequestContext, identifier: string, password?: string) => Promise<User | PasswordValidationError>
Creates a new User with the special customer Role and using the NativeAuthenticationStrategy.

addNativeAuthenticationMethod

async method
type:
(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

async method
type:
(ctx: RequestContext, identifier: string, password: string) => Promise<User>
Creates a new verified User using the NativeAuthenticationStrategy.

softDelete

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

setVerificationToken

async method
type:
(ctx: RequestContext, user: User) => Promise<User>
Sets the NativeAuthenticationMethod verificationToken as part of the User email verification flow.

verifyUserByToken

async method
type:
(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

async method
type:
(ctx: RequestContext, emailAddress: string) => Promise<User | undefined>
Sets the NativeAuthenticationMethod passwordResetToken as part of the User password reset flow.

resetPasswordByToken

async method
type:
(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.

changeNativeIdentifier

async method
type:
(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

async method
type:
(ctx: RequestContext, user: User) => Promise<User>
Sets the NativeAuthenticationMethod identifierChangeToken as part of the User email address change flow.

changeIdentifierByToken

async method
type:
(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

async method
type:
(ctx: RequestContext, userId: ID, currentPassword: string, newPassword: string) => Promise<boolean | InvalidCredentialsError | PasswordValidationError>
Updates the password for a User with the NativeAuthenticationMethod.