Skip to content

Commit

Permalink
feat(authentication-jwt): add signup route
Browse files Browse the repository at this point in the history
  • Loading branch information
HrithikMittal authored and jannyHou committed Jul 24, 2020
1 parent 85a6164 commit d481157
Showing 1 changed file with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import {
UserService,
} from '@loopback/authentication';
import {inject} from '@loopback/core';
import {model, property} from '@loopback/repository';
import {get, post, requestBody} from '@loopback/rest';
import {SecurityBindings, securityId, UserProfile} from '@loopback/security';
import {genSalt, hash} from 'bcryptjs';
import {TokenServiceBindings, User, UserServiceBindings} from '../../../';
import {UserRepository} from '../../../repositories';
import {Credentials} from '../../../services/user.service';

const CredentialsSchema = {
Expand All @@ -29,6 +32,15 @@ const CredentialsSchema = {
},
};

@model()
export class NewUserRequest extends User {
@property({
type: 'string',
required: true,
})
password: string;
}

export const CredentialsRequestBody = {
description: 'The input of login function',
required: true,
Expand All @@ -45,8 +57,43 @@ export class UserController {
public userService: UserService<User, Credentials>,
@inject(SecurityBindings.USER, {optional: true})
private user: UserProfile,
@inject(UserServiceBindings.USER_REPOSITORY)
public userRepository: UserRepository,
) {}

@post('/users/signup', {
responses: {
'200': {
description: 'User model instance',
content: {
'application/json': {
schema: {
'x-ts-type': User,
},
},
},
},
},
})
async signUp(
@requestBody({
content: {
'application/json': {
schema: CredentialsSchema,
},
},
})
newUserRequest: NewUserRequest,
): Promise<User> {
const password = await hash(newUserRequest.password, await genSalt());
delete newUserRequest.password;
const savedUser = await this.userRepository.create(newUserRequest);

await this.userRepository.userCredentials(savedUser.id).create({password});

return savedUser;
}

@post('/users/login', {
responses: {
'200': {
Expand Down

0 comments on commit d481157

Please sign in to comment.