Created
October 17, 2021 14:38
-
-
Save tomgobich/ddda208b9f1dbc3b07101a3fdfa2a869 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public async register({ request, response, auth, session }: HttpContextContract) { | |
| const validationSchema = schema.create({ | |
| username: schema.string({ trim: true }, [ | |
| rules.maxLength(50), | |
| rules.minLength(3), | |
| rules.unique({ table: 'users', column: 'username' }), | |
| rules.regex(/^[a-zA-Z0-9-_]+$/), | |
| rules.notIn(['admin', 'super', 'power', 'jagr', 'jagrco', '_jagr', '_jagrco', 'jagr_', 'jagrco_', 'jagr-co', 'moderator', 'public', 'dev', 'alpha', 'mail']) | |
| ]), | |
| email: schema.string({ trim: true }, [rules.unique({ table: 'users', column: 'email' })]), | |
| password: schema.string({}, [rules.minLength(8)]) | |
| }); | |
| const validationMessages = { | |
| 'required': "Please provide a {{ field }}", | |
| 'username.notIn': "This username is restricted and may be in use for other purposes", | |
| 'username.minLength': "Your username needs to be at least 3 characters long", | |
| 'username.maxLength': "Your username cannot exceed 50 characters long", | |
| 'username.regex': "Your username can only contain letters, numbers, _, and -", | |
| 'username.unique': "This username is already taken", | |
| 'email.unique': "An account with this email already exists", | |
| 'password.minLength': "Your password must be at least 8 characters long", | |
| }; | |
| const data = await request.validate({ schema: validationSchema, messages: validationMessages }); | |
| try { | |
| const user = new User(); | |
| user.username = data.username; | |
| user.email = data.email; | |
| user.password = data.password; | |
| user.role_id = Role.USER; | |
| await user.save(); | |
| await Profile.create({ user_id: user.id }); | |
| await auth.login(user); | |
| Event.emit('welcome', { user }) | |
| Logger.silly('NEW USER', `Welcome to Jagr: ${user.username}`); | |
| session.flash('success', `Welcome to Jagr, ${user.username}!`); | |
| return response.redirect('/'); | |
| } catch (error) { | |
| Logger.error('AuthController.register', error); | |
| session.flash('error', "An error occurred."); | |
| return response.redirect().back(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment