Skip to content
This repository was archived by the owner on Dec 12, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 47 additions & 22 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,44 @@ var passport = require('passport');
var stormpath = require('stormpath');


function getApplication(callback) {
// Initialize our Stormpath client.
var apiKey = new stormpath.ApiKey(
process.env['STORMPATH_API_KEY_ID'],
process.env['STORMPATH_API_KEY_SECRET']
);

var spClient = new stormpath.Client({ apiKey: apiKey });

// get the application and perform the callback
spClient.getApplication(process.env['STORMPATH_APP_HREF'], callback);
}

// Password reset
function sendPasswordResetEmail(req, res) {
getApplication(function(err, app) {
if (err) throw err;
app.sendPasswordResetEmail(req.body.username, function onEmailSent(err, token) {
if (err) throw err;
console.log('reset email sent to ' + token.email);
req.flash('error', 'reset email sent');
res.redirect('/login');
});
})
}

// Authenticate a user.
function authenticate(req, res, next) {
passport.authenticate(
'stormpath',
{
successRedirect: '/dashboard',
failureRedirect: '/login',
failureFlash: 'Invalid email or password.',
}
)(req, res, next);
}

// Render the home page.
router.get('/', function(req, res) {
res.render('index', { title: 'Home', user: req.user });
Expand All @@ -15,7 +53,6 @@ router.get('/register', function(req, res) {
res.render('register', { title: 'Register', error: req.flash('error')[0] });
});


// Register a new user to Stormpath.
router.post('/register', function(req, res) {

Expand All @@ -27,15 +64,8 @@ router.post('/register', function(req, res) {
return res.render('register', { title: 'Register', error: 'Email and password required.' });
}

// Initialize our Stormpath client.
var apiKey = new stormpath.ApiKey(
process.env['STORMPATH_API_KEY_ID'],
process.env['STORMPATH_API_KEY_SECRET']
);
var spClient = new stormpath.Client({ apiKey: apiKey });

// Grab our app, then attempt to create this user's account.
var app = spClient.getApplication(process.env['STORMPATH_APP_HREF'], function(err, app) {
getApplication(function(err, app) {
if (err) throw err;

app.createAccount({
Expand Down Expand Up @@ -71,19 +101,14 @@ router.get('/logout', function(req, res) {
res.redirect('/');
});


// Authenticate a user.
router.post(
'/login',
passport.authenticate(
'stormpath',
{
successRedirect: '/dashboard',
failureRedirect: '/login',
failureFlash: 'Invalid email or password.',
}
)
);
// Login form submission
router.post('/login', function(req, res, next) {
if (req.body.forgot) {
sendPasswordResetEmail(req, res)
} else {
authenticate(req, res, next);
}
});


// Render the dashboard page.
Expand Down
1 change: 1 addition & 0 deletions views/login.jade
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ block body
.form-group
.col-lg-10.col-lg-offset-4
button.btn.btn-primary(type='submit') Login
button.btn.btn-link(type='submit', name='forgot', value='true') Forgot Password