Created
December 2, 2014 12:07
-
-
Save anonymous/2e80a1ddacc4f2d7aa88 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
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