Last active
December 16, 2015 09:25
-
-
Save anil826/cedfe988cbc4f712d9aa to your computer and use it in GitHub Desktop.
Oauth for angularJs using satellizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
'use strict'; | |
var app = angular.module('app', ['ngRoute', 'ngStorage', 'ui.bootstrap.modal', 'autocomplete', 'timer', 'satellizer']); | |
app.config(function ($authProvider) { | |
$authProvider.google({ | |
//clientId: 'xxxxxxxxxxxxxxxxx.apps.googleusercontent.com' //development | |
clientId: 'xcccccccccccccccccxxxxxx.apps.googleusercontent.com' //production | |
}); | |
}); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('enbakeCRM').controller('commonCtrl', [ | |
'$scope', '$location', '$localStorage', '$auth', 'apiUrl', 'oauthGoogle','$route', function ($scope, $location, $localStorage, $auth, apiUrl, oauthGoogle,$route) { | |
$scope.link = function (provider) { | |
var data, res; | |
$auth.link(provider) | |
.then(function (response) { | |
console.log(response.data.user); | |
res = oauthGoogle.googleUser(response.data); | |
console.log(res) | |
// console.log(res.responseJSON.user.email) | |
if (res.status === 200) { | |
// $localStorage.email = res.responseJSON.user.email; | |
// $localStorage.token = res.responseJSON.user.authentication_token; | |
// $localStorage.role_name = res.responseJSON.user.role_name; | |
// $localStorage.id = res.responseJSON.user.id; | |
if (res.responseJSON.user.role_name === "admin") { | |
$localStorage.display_name = 'Admin'; | |
} else { | |
if (res.responseJSON.user.role_name === "user") { | |
$localStorage.display_name = 'User'; | |
} else { | |
$localStorage.display_name = "Super Admin"; | |
} | |
} | |
return $location.path('/scrum'); | |
} else { | |
alert("Sorry this user does not exist in our system."); | |
return $location.path('/'); | |
// return $scope.errorMessage = "Invalid credentials."; | |
} | |
}) | |
.catch(function (response) { | |
console.log('----------error--------') | |
console.log(response); | |
}); | |
}; | |
} | |
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html ng-app="app"> | |
<head> | |
<script type="text/javascript" src="bower_components/satellizer.js"></script> | |
<script type="text/javascript" src="bower_components/toastr.js"></script> | |
<!--<script src="//cdn.jsdelivr.net/satellizer/0.10.1/satellizer.min.js"></script>--> | |
</head> | |
<body ng-controller="commonCtrl"> | |
<div class="row"> | |
<div class="col-md-4 center-block"> | |
<a ng-click="link('google')" class="btn btn-danger" href="#"> | |
Google Login</a><span class="fa-stack fa-lg"><i class="fa fa-google-plus-square fa-stack-2x"></i> | |
</span> | |
</div> | |
</div> | |
<div id="main"> | |
<!-- angular templating --> | |
<!-- this is where content will be injected --> | |
<div ng-view></div> | |
</div> | |
</body> | |
</html> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
'use strict'; | |
angular.module('app').factory('oauthGoogle', [ | |
'$http', '$q', '$localStorage','apiUrl', function ($http, $q, $localStorage,apiUrl) { | |
// var data; | |
var data, deffered, userAPI; | |
deffered = $q.defer(); | |
userAPI = {}; | |
data = []; | |
console.log('----------------oauthGoogle--------------') | |
return { | |
googleUser: function (data) { | |
return $.ajax({ | |
url: apiUrl+'/users/log_in', | |
type: 'POST', | |
data: data.user, | |
dataType: 'JSON', | |
async: false | |
}).success(function(d) { | |
data = d; | |
console.log('--------------data---------------------') | |
console.log(data.status); | |
return data; | |
console.log('---------------backend success-------------------') | |
}); | |
} | |
}; | |
userAPI.data = function() { | |
return data; | |
}; | |
return userAPI; | |
} | |
]); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// set up ====================================================================== | |
var path = require('path'); | |
var express = require('express'); | |
var app = express(); // create our app w/ express | |
var port = process.env.PORT || 8080; // set the port | |
var bodyParser = require('body-parser'); | |
var async = require('async'); | |
var bcrypt = require('bcryptjs'); | |
var logger = require('morgan'); | |
var jwt = require('jwt-simple') | |
var mongoose = require('mongoose'); | |
var request = require('request'); // pull information from HTML POST (express4) | |
var methodOverride = require('method-override'); // simulate DELETE and PUT (express4) | |
app.listen(port); | |
console.log("App listening on port 8080"); | |
//require('./app/app.js')(app); | |
app.use(bodyParser.urlencoded({'extended': 'true'})); // parse application/x-www-form-urlencoded | |
app.use(bodyParser.json()); // parse application/json | |
app.use(bodyParser.json({type: 'application/vnd.api+json'})); // parse application/vnd.api+json as json | |
app.use(methodOverride('X-HTTP-Method-Override')); // override with the X-HTTP-Method-Override header in the request | |
//app.all('*', function(req, res) { | |
// res.sendfile('index.html'); // load the single view file (angular will handle the page changes on the front-end) | |
//}); | |
// | |
var userSchema = new mongoose.Schema({ | |
email: {type: String, unique: true, lowercase: true}, | |
password: {type: String, select: false}, | |
displayName: String, | |
picture: String, | |
bitbucket: String, | |
facebook: String, | |
foursquare: String, | |
google: String, | |
github: String, | |
instagram: String, | |
linkedin: String, | |
live: String, | |
yahoo: String, | |
twitter: String, | |
twitch: String | |
}); | |
userSchema.pre('save', function (next) { | |
var user = this; | |
if (!user.isModified('password')) { | |
return next(); | |
} | |
bcrypt.genSalt(10, function (err, salt) { | |
bcrypt.hash(user.password, salt, function (err, hash) { | |
user.password = hash; | |
next(); | |
}); | |
}); | |
}); | |
var User = mongoose.model('User', userSchema); | |
mongoose.connect('localhost'); | |
mongoose.connection.on('error', function (err) { | |
console.log('Error: Could not connect to MongoDB. Did you forget to run `mongod`?'.red); | |
}); | |
app.use(express.static(__dirname + '/')); | |
/* | |
|-------------------------------------------------------------------------- | |
| Login with Google | |
|-------------------------------------------------------------------------- | |
// */ | |
app.post('/auth/google', function (req, res) { | |
var accessTokenUrl = 'https://accounts.google.com/o/oauth2/token'; | |
var peopleApiUrl = 'https://www.googleapis.com/plus/v1/people/me/openIdConnect'; | |
var params = { | |
code: req.body.code, | |
client_id: 'xxxxxxxxxxxxx.apps.googleusercontent.com', | |
client_secret: 'xxxxxxxccxxxxxxxxxx', | |
redirect_uri: req.body.redirectUri, | |
grant_type: 'authorization_code' | |
}; | |
// Step 1. Exchange authorization code for access token. | |
request.post(accessTokenUrl, {json: true, form: params}, function (err, response, token) { | |
var accessToken = token.access_token; | |
var headers = {Authorization: 'Bearer ' + accessToken}; | |
// Step 2. Retrieve profile information about the current user. | |
request.get({url: peopleApiUrl, headers: headers, json: true}, function (err, response, profile) { | |
if (profile.error) { | |
return res.status(500).send({message: profile.error.message}); | |
} else { | |
console.log(profile.email) | |
var user = new User(); | |
user.google = profile.sub; | |
user.picture = profile.picture.replace('sz=50', 'sz=200'); | |
user.displayName = profile.name; | |
user.email = profile.email; | |
user.save(); | |
res.send({user: user}); | |
} | |
//Step 3a. Link user accounts. | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment