Page Contents

Home > @loopback/authentication > UserService > verifyCredentials

UserService.verifyCredentials() method

Verify the identity of a user, construct a corresponding user profile using the user information and return the user profile.

Signature:

verifyCredentials(credentials: C): Promise<U>;

Parameters

Parameter Type Description
credentials C Credentials for basic auth or configurations for 3rd party. Example see the

Returns:

Promise<U>

Example

A pseudo code for basic authentication:

verifyCredentials(credentials: C): Promise<U> {
  // the id field shouldn't be hardcoded
  user = await UserRepo.find(credentials.id);
  matched = await passwordService.compare(user.password, credentials.password);
  if (matched) return user;
  // throw a JS error, agnostic of the client type
  throw new Error('authentication failed');
};

A pseudo code for 3rd party authentication:

type UserInfo = {
  accessToken: string;
  refreshToken: string;
  userProfile: string;
};
verifyCredentials(credentials: C): Promise<U> {
  try {
    userInfo: UserInfo = await getUserInfoFromFB(credentials);
  } catch (e) {
    // throw a JS error, agnostic of the client type
    throw e;
  }
};