Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function validateCredentials(credentials: Credentials) {
// Validate Email
if (!isemail.validate(credentials.email)) {
throw new HttpErrors.UnprocessableEntity('invalid email');
}
// Validate Password Length
if (!credentials.password || credentials.password.length < 8) {
throw new HttpErrors.UnprocessableEntity(
'password must be minimum 8 characters',
);
}
}
export function validateCredentials(credentials: Credentials) {
// Validate Email
if (!isemail.validate(credentials.email)) {
throw new HttpErrors.UnprocessableEntity('invalid email');
}
// Validate Password Length
if (!credentials.password || credentials.password.length < 8) {
throw new HttpErrors.UnprocessableEntity(
'password must be minimum 8 characters',
);
}
}
export function validateCredentials(credentials: Credentials) {
// Validate Email
if (!isemail.validate(credentials.email)) {
throw new HttpErrors.UnprocessableEntity('invalid email');
}
// Validate Password Length
if (credentials.password.length < 8) {
throw new HttpErrors.UnprocessableEntity(
'password must be minimum 8 characters',
);
}
}
export function validateCredentials(credentials: Credentials) {
// Validate Email
if (!isemail.validate(credentials.email)) {
throw new HttpErrors.UnprocessableEntity('invalid email');
}
// Validate Password Length
if (credentials.password.length < 8) {
throw new HttpErrors.UnprocessableEntity(
'password must be minimum 8 characters',
);
}
}
async create(entity: DataObject, options?: Options): Promise {
const user = await super.create(entity, options);
try {
// Add temporary password for first time
const password = await bcrypt.hash(
process.env.USER_TEMP_PASSWORD,
this.saltRounds,
);
const creds = new UserCredentials({
authProvider: 'internal',
password: password,
});
await this.credentials(user.id).create(creds);
} catch (err) {
throw new HttpErrors.UnprocessableEntity('Error while hashing password');
}
return user;
}
it('validateCredentials() fails with invalid password', () => {
const expectedError = new HttpErrors.UnprocessableEntity(
'password must be minimum 8 characters',
);
const credentials = {email: 'dom@example.com', password: 'p4ss'};
expect(() => validateCredentials(credentials)).to.throw(expectedError);
});
it('validateCredentials() fails with invalid email', () => {
const expectedError = new HttpErrors.UnprocessableEntity('invalid email');
const credentials = {email: 'domdomdom', password: 'p4ssw0rd'};
expect(() => validateCredentials(credentials)).to.throw(expectedError);
});
async loginWithClientUser(
@requestBody() req: LoginRequest,
): Promise {
if (!this.client || !this.user) {
throw new HttpErrors.Unauthorized(AuthErrorKeys.ClientInvalid);
} else if (!this.client.userIds || this.client.userIds.length === 0) {
throw new HttpErrors.UnprocessableEntity(AuthErrorKeys.ClientUserMissing);
} else if (!req.client_secret) {
throw new HttpErrors.BadRequest(AuthErrorKeys.ClientSecretMissing);
}
try {
const payload: ClientAuthCode = {
clientId: this.client.clientId,
user: this.user,
};
return await this.createJWT(payload, this.client);
} catch (error) {
throw new HttpErrors.InternalServerError(
AuthErrorKeys.InvalidCredentials,
);
}
}