Skip to content

Instantly share code, notes, and snippets.

@robertjd
Forked from anonymous/gist:2e80a1ddacc4f2d7aa88
Last active August 29, 2015 14:10
Show Gist options
  • Save robertjd/e09dcdf282587fad3a53 to your computer and use it in GitHub Desktop.
Save robertjd/e09dcdf282587fad3a53 to your computer and use it in GitHub Desktop.
var express = require('express');
var router = express.Router();
var config = require('config');
var passport = require('passport');
var stormpath = require('stormpath');
// Register a new user to Stormpath.
router.post('/register', function(req, res, next) {
// var username = req.body.username;
// var password = req.body.password;
var givenName = req.body.givenName,
surname = req.body.surname,
email = req.body.email,
gender = req.body.gender,
birth_month = req.body.birth_month,
birth_date = req.body.birth_date,
birth_year = req.body.birth_year,
password = req.body.password;
// Grab user fields.
if (!givenName || !surname || !email || !gender || !birth_date || !birth_month || !birth_year || !password) {
return res.send({
error: 'All fields are required.'
});
}
// Initialize our Stormpath client.
var apiKey = new stormpath.ApiKey(
config.get('stormpath.apiKey'),
config.get('stormpath.apiSecret')
);
var spClient = new stormpath.Client({
apiKey: apiKey
});
// Grab our app, then attempt to create this user's account.
var app = spClient.getApplication(config.get('stormpath.applicationUrl'), function(err, app) {
if (err) throw err;
app.createAccount({
givenName: givenName,
surname: surname,
email: email,
username: email, // setting username equal to the email
password: password,
gender: gender,
birth_month: birth_month,
birth_date: birth_date,
birth_year: birth_year
}, function(err, createdAccount) {
if (err) {
console.log('err')
return res.send({
error: err.userMessage
});
} else {
console.log('auth')
passport.authenticate('stormpath')(req, res, function(err, user) {
console.log(err);
console.log('user');
console.log(user);
if(err) return next(err);
return res.send({success: true});
});
}
});
});
});
// Authenticate a user.
router.post(
'/login',
passport.authenticate(
'stormpath', {
successRedirect: '/dashboard',
failureRedirect: '/login',
failureFlash: 'Invalid email or password.',
}
)
);
// Logout the user, then redirect to the home page.
router.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
module.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment