Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.48 KB | None | 0 0
  1. app.get('/auth/google', passport.authenticate('google', { scope : ['profile', 'email'] }));
  2.  
  3. app.get('/auth/google/callback',
  4. passport.authenticate('google', {
  5. successRedirect: '/#/profile',
  6. failureRedirect: '/login'
  7. }));
  8.  
  9. let
  10. GoogleStrategy = require('passport-google-oauth').OAuth2Strategy,
  11. FacebookStrategy = require('passport-facebook').Strategy,
  12. Promise = require('bluebird'),
  13. config = require('./auth.js');
  14.  
  15. module.exports = function(passport, sequelize) {
  16. const
  17. User = sequelize.import("../model/user"),
  18. Google = sequelize.import("../model/google");
  19.  
  20. User.hasOne(Google, {foreignKey: 'idUser'});
  21. Google.belongsTo(User, {foreignKey: 'idUser'});
  22.  
  23. function findGoogleUser(idUser) {
  24. return new Promise(function(resolve, reject){
  25. User.findOne({
  26. where: {
  27. id: idUser
  28. },
  29. attributes: ['nameFirst', 'nameLast', 'email', 'desc', 'img', 'username'],
  30. include: [{
  31. model: Google,
  32. attributes: ['idGoogle', 'token', 'displayName', 'gmail']
  33. }]
  34. })
  35. .then(function(user) {
  36. resolve(user);
  37. })
  38. .catch(function(err) {
  39. reject(err);
  40. });
  41. });
  42. }
  43.  
  44. function findGoogleUserByGoogleId(idGoogle) {
  45. return new Promise(function(res, resolve, reject){
  46. Google.findOne({
  47. where: { idGoogle: idGoogle }
  48. }).then(function(google) {
  49. if(google) {
  50. findGoogleUser(google.idUser).then(function(user) {
  51. res.redirect('/#/profile');
  52. resolve(user);
  53. });
  54. } else {
  55. reject();
  56. }
  57. });
  58. });
  59. }
  60.  
  61. // used to serialize the user for the session
  62. passport.serializeUser(function(user, done) {
  63. console.log('serializeUser ------------------------------');
  64. var converted = JSON.parse(JSON.stringify(user));
  65. console.dir(converted);
  66. done(null, converted.google.idGoogle);
  67. });
  68.  
  69. // used to deserialize the user
  70. passport.deserializeUser(function(id, done) {
  71. console.log('deserializeUser ------------------------------');
  72. console.dir(id);
  73. findGoogleUserByGoogleId(id).then(function(user) {
  74. if(user) return done(null, user);
  75. throw Error('No user found!');
  76. });
  77. });
  78. passport.use(new GoogleStrategy({
  79. clientID: "config.auth.google.clientID",
  80. clientSecret: "config.auth.google.clientSecret",
  81. callbackURL: "config.auth.google.callbackURL"
  82. },
  83. function(token, refreshToken, profile, done) {
  84.  
  85. // User.findOne won't fire until we have all our data back from Google
  86. process.nextTick(function() {
  87.  
  88. Google.findOne({
  89. where: { idGoogle: profile.id }
  90. }).then(function(google) {
  91. if(google) {
  92. findGoogleUser(google.idUser).then(function(user) {
  93. done(null, user);
  94. });
  95. } else {
  96. User.create({
  97. nameLast: profile.name.familyName,
  98. nameFirst: profile.name.givenName,
  99. username: profile.displayName,
  100. email: profile.emails[0].value
  101. })
  102. .then(function(user) {
  103. Google.create({
  104. idUser: user.id,
  105. idGoogle: profile.id,
  106. token: token,
  107. refreshToken: refreshToken,
  108. displayName: profile.displayName,
  109. gmail: profile.emails[0].value
  110. })
  111. .then(function(google) {
  112. findGoogleUser(google.idUser).then(function(user) {
  113. });
  114. }).catch(function(err) {
  115. throw err;
  116. });
  117. });
  118. }
  119. });
  120. });
  121. }));
  122. };
  123.  
  124. 'use strict';
  125.  
  126. angular.module('thesis.login', ['ngRoute', 'ngStorage'])
  127.  
  128. .controller('LoginController', ['$scope', '$location', '$window', 'UserService', '$cookies', '$rootScope', '$auth', 'toastr','$localStorage','$sessionStorage',
  129. function AdminUserCtrl($scope, $location, $window, UserService, $cookies, $rootScope, $auth, toastr, $localStorage, $sessionStorage) {
  130. //Admin User Controller (login, logout)
  131. $scope.login = function login() {
  132. var email = $scope.user.email;
  133. var password = $scope.user.password;
  134. if (email !== undefined && password !== undefined) {
  135. UserService.logIn(email, password).success(function(data) {
  136. // $rootScope.$broadcast('userLoggedIn');
  137. toastr.info('Welcome Back!');
  138. $cookies.get('id');
  139. $location.path("/profile");
  140.  
  141. }).error(function(status, data) {
  142.  
  143. });
  144. }
  145.  
  146. };
  147. $scope.logout = function logout() {
  148. UserService.logOut().success(function(data) {
  149. $cookies.remove('name');
  150. $cookies.remove('id');
  151. // $rootScope.$broadcast('userLoggedOut');
  152. toastr.info('You have been logged out');
  153. $location.path("/login");
  154. });
  155. };
  156. $scope.$storage = $localStorage;
  157. $scope.profile = function() {
  158. if($cookies.get('id')) {
  159. $location.path("/profile");
  160. }
  161. $location.path("/login");
  162. };
  163.  
  164. $scope.checkin = function() {
  165. if($cookies.get('id')) {
  166. $location.path("/foursquare");
  167. }
  168. $location.path("/login");
  169. };
  170. $scope.authenticate = function(provider) {
  171. $auth.UserService.authenticate(provider)
  172. .then(function() {
  173. toastr.success('You have successfully signed in with ' + provider + '!');
  174. $location.path('/profile');
  175. })
  176. .catch(function(error) {
  177. if (error.error) {
  178. console.log(error);
  179. // Popup error - invalid redirect_uri, pressed cancel button, etc.
  180. toastr.error(error.error);
  181. } else if (error.data) {
  182. // HTTP response error from server
  183. toastr.error(error.data.message, error.status);
  184. } else {
  185. toastr.error(error);
  186. }
  187. });
  188. };
  189. }
  190. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement