Skip to content

Instantly share code, notes, and snippets.

@robksawyer
Last active November 26, 2022 09:05
Show Gist options
  • Save robksawyer/c5549bce5452a88d5143e652935e4d9b to your computer and use it in GitHub Desktop.
Save robksawyer/c5549bce5452a88d5143e652935e4d9b to your computer and use it in GitHub Desktop.
A pretty basic registration form for KeystoneJS.
// @file signup.jade
// @path /templates/views/user/signup.jade
// @description Form that user sees when signing up.
//
extends ../../layouts/default
block intro
.container
h1= 'Sign up for Little B.O.M'
block content
.container
if user
p You are already logged in.
else
p Fill form in order to sign up.
form.contact-form(method="post").row.col-sm-8.col-md-6
input(type='hidden', name='action', value='user.create')
.form-group
label Name
.row
.col-sm-6.col-md-6
input.form-control.input-box(type='text', name='first', value=formData.first, placeholder='First Name')
.col-sm-6.col-md-6
input.form-control.input-box(type='text', name='last', value=formData.last, placeholder='Last Name')
.form-group
label Email
input.form-control.input-box(type='email', name='email', value=formData.email, placeholder='Email')
.form-group
label Password
.row
.col-sm-6.col-md-6
input.form-control.input-box(type='password', name='password', value=formData.password, placeholder='Password')
.col-sm-6.col-md-6
input.form-control.input-box(type='password', name='password_confirm', value=formData.password_confirm, placeholder='Retype password')
button(type='submit').btn.btn-success Sign up
// @file signup.js
// @path /routes/views/signup.js
// @description Handles the post request when the user tries to sign up.
// @url https://github.com/keystonejs/generator-keystone/issues/10
//
var keystone = require('keystone');
var User = keystone.list('User');
exports = module.exports = function (req, res) {
var view = new keystone.View(req, res);
var locals = res.locals;
// Set locals
locals.section = 'signup';
locals.filters = {
};
locals.data = {
};
console.log(req);
locals.formData = req.body || {};
view.on('post', { action: 'user.create' }, function(next) {
// if (!keystone.security.csrf.validate(req)) {
// req.flash('error', 'There was an error with your request, please try again.');
// return renderView();
// }
if(locals.formData.password !== locals.formData.password_confirm){
req.flash('error', 'The passwords do not match.');
next();
}
var newUser = new User.model({
name: {
first: locals.formData.first,
last: locals.formData.last
},
email: locals.formData.email,
password: locals.formData.password
//Add some user defaults here.
});
newUser.isAdmin = false;
newUser.save(function(err, result) {
if (err) {
locals.data.validationErrors = err.errors;
} else {
req.flash('success', 'Account created. Please sign in.');
//auto-signin can be easily implemented using the keystone.session.signin() API.
return res.redirect('/keystone/signin');
}
next();
});
});
// Render the view
view.render('user/signup');
};
@ariffira
Copy link

does anything need to add or change in User model?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment