Added password validator and facebook routes
This commit is contained in:
13
app.js
13
app.js
@ -52,6 +52,19 @@ app.get('/account', passportConf.ensureAuthenticated, user.account);
|
||||
app.get('/admin', passportConf.ensureAuthenticated, passportConf.ensureAdmin(), user.admin);
|
||||
app.get('/partials/:name', home.partials);
|
||||
|
||||
// Redirect the user to Facebook for authentication. When complete,
|
||||
// Facebook will redirect the user back to the application at
|
||||
app.get('/auth/facebook', passport.authenticate('facebook'));
|
||||
// Facebook will redirect the user to this URL after approval. Finish the
|
||||
// authentication process by attempting to obtain an access token. If
|
||||
// access was granted, the user will be logged in. Otherwise,
|
||||
// authentication has failed.
|
||||
app.get('/auth/facebook/callback', passport.authenticate('facebook', {
|
||||
successRedirect: '/',
|
||||
failureRedirect: '/login'
|
||||
}));
|
||||
|
||||
|
||||
app.get('*', home.index);
|
||||
|
||||
|
||||
|
@ -41,8 +41,11 @@ passport.use(new FacebookStrategy({
|
||||
function (accessToken, refreshToken, profile, done) {
|
||||
User.findOne({ $where: profile.provider + '==' + profile.id }, function(err, existingUser) {
|
||||
if (existingUser) {
|
||||
console.log('already exists')
|
||||
done(null, existingUser);
|
||||
} else {
|
||||
console.log('making new user');
|
||||
console.log(profile)
|
||||
var user = new User({
|
||||
firstName: profile.first_name,
|
||||
lastName: profile.last_name,
|
||||
@ -50,6 +53,7 @@ passport.use(new FacebookStrategy({
|
||||
});
|
||||
user[profile.provider] = profile.id;
|
||||
user.save(function(err) {
|
||||
if (err) throw err;
|
||||
done(null, user);
|
||||
});
|
||||
}
|
||||
|
@ -64,6 +64,7 @@ exports.postSignup = function(req, res) {
|
||||
|
||||
user.save(function(err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
if (err.code === 11000) {
|
||||
req.flash('messages', 'User already exists');
|
||||
return res.redirect('/signup');
|
||||
|
@ -9,7 +9,7 @@ block content
|
||||
a(href='/signup') Sign Up
|
||||
.row
|
||||
.col-xs-6.col-sm-6.col-md-6
|
||||
a.btn.btn-lg.btn-primary.btn-block(href='#')
|
||||
a.btn.btn-lg.btn-primary.btn-block(href='/auth/facebook')
|
||||
i.fa.fa-facebook-square
|
||||
| Facebook
|
||||
.col-xs-6.col-sm-6.col-md-6
|
||||
|
Reference in New Issue
Block a user