Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(authentication-jwt) Add signup / create user #5763

Merged
merged 1 commit into from
Jul 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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