import { Context, HttpResponseRedirect, Post, Session, ValidateBody } from '@foal/core';
import { isCommon } from '@foal/password';
import { User } from '../entities';
export class SignupController {
@Post()
@ValidateBody({
additionalProperties: false,
properties: {
email: { type: 'string', format: 'email' },
password: { type: 'string' }
},
required: [ 'email', 'password' ],
type: 'object',
})
async signup(ctx: Context<User, Session>) {
if (await isCommon(ctx.request.body.password)) {
ctx.session.set('error', 'Password too common.', { flash: true });
return new HttpResponseRedirect('/signup');
}
let user = await User.findOne({ email: ctx.request.body.email });
if (user) {
ctx.session.set('error', 'Email already taken.', { flash: true });
return new HttpResponseRedirect('/signup');
}
user = new User();
user.email = ctx.request.body.email;
await user.setPassword(ctx.request.body.password);
await user.save();
ctx.session.setUser(user);
return new HttpResponseRedirect('/');
}
}