Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- app.get('/auth/google', passport.authenticate('google', { scope : ['profile', 'email'] }));
- app.get('/auth/google/callback',
- passport.authenticate('google', {
- successRedirect: '/#/profile',
- failureRedirect: '/login'
- }));
- let
- GoogleStrategy = require('passport-google-oauth').OAuth2Strategy,
- FacebookStrategy = require('passport-facebook').Strategy,
- Promise = require('bluebird'),
- config = require('./auth.js');
- module.exports = function(passport, sequelize) {
- const
- User = sequelize.import("../model/user"),
- Google = sequelize.import("../model/google");
- User.hasOne(Google, {foreignKey: 'idUser'});
- Google.belongsTo(User, {foreignKey: 'idUser'});
- function findGoogleUser(idUser) {
- return new Promise(function(resolve, reject){
- User.findOne({
- where: {
- id: idUser
- },
- attributes: ['nameFirst', 'nameLast', 'email', 'desc', 'img', 'username'],
- include: [{
- model: Google,
- attributes: ['idGoogle', 'token', 'displayName', 'gmail']
- }]
- })
- .then(function(user) {
- resolve(user);
- })
- .catch(function(err) {
- reject(err);
- });
- });
- }
- function findGoogleUserByGoogleId(idGoogle) {
- return new Promise(function(res, resolve, reject){
- Google.findOne({
- where: { idGoogle: idGoogle }
- }).then(function(google) {
- if(google) {
- findGoogleUser(google.idUser).then(function(user) {
- res.redirect('/#/profile');
- resolve(user);
- });
- } else {
- reject();
- }
- });
- });
- }
- // used to serialize the user for the session
- passport.serializeUser(function(user, done) {
- console.log('serializeUser ------------------------------');
- var converted = JSON.parse(JSON.stringify(user));
- console.dir(converted);
- done(null, converted.google.idGoogle);
- });
- // used to deserialize the user
- passport.deserializeUser(function(id, done) {
- console.log('deserializeUser ------------------------------');
- console.dir(id);
- findGoogleUserByGoogleId(id).then(function(user) {
- if(user) return done(null, user);
- throw Error('No user found!');
- });
- });
- passport.use(new GoogleStrategy({
- clientID: "config.auth.google.clientID",
- clientSecret: "config.auth.google.clientSecret",
- callbackURL: "config.auth.google.callbackURL"
- },
- function(token, refreshToken, profile, done) {
- // User.findOne won't fire until we have all our data back from Google
- process.nextTick(function() {
- Google.findOne({
- where: { idGoogle: profile.id }
- }).then(function(google) {
- if(google) {
- findGoogleUser(google.idUser).then(function(user) {
- done(null, user);
- });
- } else {
- User.create({
- nameLast: profile.name.familyName,
- nameFirst: profile.name.givenName,
- username: profile.displayName,
- email: profile.emails[0].value
- })
- .then(function(user) {
- Google.create({
- idUser: user.id,
- idGoogle: profile.id,
- token: token,
- refreshToken: refreshToken,
- displayName: profile.displayName,
- gmail: profile.emails[0].value
- })
- .then(function(google) {
- findGoogleUser(google.idUser).then(function(user) {
- });
- }).catch(function(err) {
- throw err;
- });
- });
- }
- });
- });
- }));
- };
- 'use strict';
- angular.module('thesis.login', ['ngRoute', 'ngStorage'])
- .controller('LoginController', ['$scope', '$location', '$window', 'UserService', '$cookies', '$rootScope', '$auth', 'toastr','$localStorage','$sessionStorage',
- function AdminUserCtrl($scope, $location, $window, UserService, $cookies, $rootScope, $auth, toastr, $localStorage, $sessionStorage) {
- //Admin User Controller (login, logout)
- $scope.login = function login() {
- var email = $scope.user.email;
- var password = $scope.user.password;
- if (email !== undefined && password !== undefined) {
- UserService.logIn(email, password).success(function(data) {
- // $rootScope.$broadcast('userLoggedIn');
- toastr.info('Welcome Back!');
- $cookies.get('id');
- $location.path("/profile");
- }).error(function(status, data) {
- });
- }
- };
- $scope.logout = function logout() {
- UserService.logOut().success(function(data) {
- $cookies.remove('name');
- $cookies.remove('id');
- // $rootScope.$broadcast('userLoggedOut');
- toastr.info('You have been logged out');
- $location.path("/login");
- });
- };
- $scope.$storage = $localStorage;
- $scope.profile = function() {
- if($cookies.get('id')) {
- $location.path("/profile");
- }
- $location.path("/login");
- };
- $scope.checkin = function() {
- if($cookies.get('id')) {
- $location.path("/foursquare");
- }
- $location.path("/login");
- };
- $scope.authenticate = function(provider) {
- $auth.UserService.authenticate(provider)
- .then(function() {
- toastr.success('You have successfully signed in with ' + provider + '!');
- $location.path('/profile');
- })
- .catch(function(error) {
- if (error.error) {
- console.log(error);
- // Popup error - invalid redirect_uri, pressed cancel button, etc.
- toastr.error(error.error);
- } else if (error.data) {
- // HTTP response error from server
- toastr.error(error.data.message, error.status);
- } else {
- toastr.error(error);
- }
- });
- };
- }
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement