Skip to content

Commit

Permalink
Fix passport strategies
Browse files Browse the repository at this point in the history
Refers to [#114228015]
- Add user id to user profile
- Refactor local, facebook, google authentication strategies
- Refactor login and signup middlewares
  • Loading branch information
andela-rekemezie committed Mar 14, 2016
1 parent 936e221 commit 467e1d3
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 115 deletions.
9 changes: 5 additions & 4 deletions app/scripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,12 @@
}
})
.state('authSuccess', {
url: '/auth/success/{token}',
url: '/auth/success/{token}/{id}',
controller: ['$stateParams', 'Auth', '$state',
function($stateParams, Auth, $state) {
Auth.setToken($stateParams.token);
var loc = $state.href('userProfile', {}, {absolute:true});
var loc = $state.href('userProfile',
{id : $stateParams.id}, {absolute:true});
window.location.href = loc;
}
]
Expand All @@ -181,13 +182,13 @@
})

.state('userProfile', {
url: '/user/profile',
url: '/user/{id}/profile',
authenticate: true,
controller: 'UserProfileCtrl',
templateUrl: 'views/user-profile.html'
})
.state('userProfile.edit', {
url: '/{id}/edit',
url: '/edit',
views: {
'inner-view@userProfile': {
controller: 'ProfileCtrl',
Expand Down
8 changes: 3 additions & 5 deletions app/scripts/controllers/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
Users.login($scope.user, function(err, res) {
if (!err) {
Auth.setToken(res.token);
var user = {};
user.name = res.name;
user.img_url = res.img_url;
$rootScope.currentUser = user;
$rootScope.currentUser = res;
var goTo = $rootScope.intendedState || 'welcome';
$state.go(goTo);
} else {
Expand Down Expand Up @@ -42,10 +39,11 @@
email: $scope.user.emailSignup,
password: $scope.user.passwordSignup
};

Users.save(user, function(res) {
Auth.setToken(res.token);
$rootScope.currentUser = res;
$state.go('profile', {
$state.go('userProfile', {
id: $rootScope.currentUser.id
});
}, function(err) {
Expand Down
4 changes: 3 additions & 1 deletion app/scripts/controllers/user-profile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ angular.module('vvida.controllers')
}];

$scope.ownerMenu = [{
link: 'userProfile.edit',
link: 'userProfile.edit({id:' +
$rootScope.currentUser.id +
'})',
title: 'Edit Your Profile',
icon: 'fa fa-pencil'
}];
Expand Down
1 change: 1 addition & 0 deletions app/views/login.jade
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
md-button.md-raised.md-warn(class="third-party-button" aria-label="Google" ng-click="google()")
i(class="fa fa-google-plus")
| Google

md-tab(label="sign Up")
md-content.md-padding
.custom-signup
Expand Down
3 changes: 2 additions & 1 deletion server/config/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ var config = {
module.exports = {
'development': config,
'test': config,
'production': config
'production': config,
'staging': config
};
3 changes: 0 additions & 3 deletions server/config/passport.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,5 @@ module.exports = {
clientID: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
callbackURL: process.env.GOOGLE_CALLBACK_URL
// returnURL: 'http://localhost:3000/auth/google/callback',
// realm: 'http://localhost:3000/',
// passReqToCallback: true
}
};
21 changes: 11 additions & 10 deletions server/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
passport.authenticate('login', function(err, user) {
if (user) {
user.password = null;
res.json(user);
res.json(stripUser(user));
} else {
res.status(401).json({
error: 'Authentication failed.',
Expand All @@ -37,18 +37,20 @@

signup: function(req, res, next) {
passport.authenticate('signup', function(err, user) {
// check for errors, if exist send a response with error

// check for errors
if (err) {
res.status(500).json({
error: err.message || err.errors[0].message || err
});
}
// If passport doesn't the user object, signup failed
if (!user) {
// If passport doesn't return the user object, signup failed
else if (!user) {
res.status(409).json({
error: 'Signup failed. User already exists.'
});
} else {
delete user.password;
return res.json(stripUser(user));
}
// else signup succesful
})(req, res, next);
Expand All @@ -70,7 +72,6 @@
err: err
});
});

},

// Middleware to get all users
Expand Down Expand Up @@ -154,11 +155,11 @@
message: 'Successfully logged out.'
});
}).catch(function(err) {
res.status(500).json({
error: 'Failed to logout user.',
err: err
});
res.status(500).json({
error: 'Failed to logout user.',
err: err
});
});
},

getItems: function(req, res) {
Expand Down
2 changes: 1 addition & 1 deletion server/routes/auth/facebook.auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ module.exports = function(app, passport) {
failureRedirect: '/sign-up'
}), function(req, res) {
// Successful authentication, redirect home.
res.redirect('/auth/success/' + req.user.dataValues.token);
res.redirect('/auth/success/' + req.user.token + '/' + req.user.id);
});
};
2 changes: 1 addition & 1 deletion server/routes/auth/google.auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ module.exports = function(app, passport) {
failureRedirect: '/sign-up'
}), function(req, res) {
// Successful authentication, redirect home.
res.redirect('/auth/success/' + req.user.dataValues.token);
res.redirect('/auth/success/' + req.user.token + '/' + req.user.id);
});
};
49 changes: 25 additions & 24 deletions server/services/auth/facebook.auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ module.exports = function(app, passport, config) {
var FacebookStrategy = config.strategy.Facebook,
jwt = require('jsonwebtoken'),
ucfirst = require('../ucfirst'),
Users = app.get('models').Users;
Users = app.get('models').Users,
user;

passport.use(new FacebookStrategy(config.auth.FACEBOOK,
function(accessToken, refreshToken, profile, done) {
Expand All @@ -17,7 +18,7 @@ module.exports = function(app, passport, config) {
facebook_auth_id: profile.id
};

if(profile.emails[0] && profile.emails[0].value) {
if (profile.emails[0] && profile.emails[0].value) {
$where = {
$or: [{
facebook_auth_id: profile.id,
Expand All @@ -26,42 +27,44 @@ module.exports = function(app, passport, config) {
}]
};
}
// check if the user exists in out database
// check if the user exists in database
Users.findOne({
where: $where,
attributes: ['id', 'name', 'img_url', 'facebook_auth_id']
}).then(function(user) {
// If the user does not exist create one
if (!user) {
Users.build({
email: profile.emails[0].value,
role: 'user',
username: profile.username,
name: profile.displayName,
facebook_auth_id: profile.id,
img_url: profile.photos[0].value,
facebook_auth_token: accessToken,
gender: ucfirst(profile.gender)
})
email: profile.emails[0].value,
role: 'user',
username: profile.username,
name: profile.displayName,
facebook_auth_id: profile.id,
img_url: profile.photos[0].value,
facebook_auth_token: accessToken,
gender: ucfirst(profile.gender)
})
.save()
.then(function(user) {
.then(function(newUser) {
user = newUser.dataValues;

user.token = null;
var token = jwt.sign({id: user.id}, config.superSecret, {
var token = jwt.sign({ id: user.id }, config.superSecret, {
expiresIn: '8760h'
});

user.token = token;
Users.update(user, {
where: {
email: user.email
id: user.id
}
}).then(function(ok, err) {
if (err) {
return done(err, null);
}

user.password = undefined;
done(null, user);
return done(null, user);
});
})
.catch(function(err) {
Expand All @@ -71,12 +74,12 @@ module.exports = function(app, passport, config) {
});
} else {
user.token = null;
var token = jwt.sign({id: user.id}, config.superSecret, {
var token = jwt.sign({ id: user.id }, config.superSecret, {
expiresIn: '8760h'
});

user.token = token;
Users.update({token: user.token}, {
Users.update({ token: user.token }, {
where: {
id: user.id
}
Expand All @@ -90,13 +93,11 @@ module.exports = function(app, passport, config) {
});
}
}).catch(function(err) {
if (err) {
return done(err);
}
});

if (err) {
return done(err);
}
});
});
}
));

};
Loading

0 comments on commit 467e1d3

Please sign in to comment.