Skip to content

Instantly share code, notes, and snippets.

@freekrai
Last active March 31, 2016 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freekrai/6f5825388b04f77420ac to your computer and use it in GitHub Desktop.
Save freekrai/6f5825388b04f77420ac to your computer and use it in GitHub Desktop.
social login with angular and hello.js

How to use Hello.js and Angular to enable client-side social login.

  1. bower install to install bower dependencies.
  2. Setup a Facebook app
(function () {
var app = angular.module('helloApp', ['ui.router','ngHello','angular-storage']);
app.config( function($stateProvider, $urlRouterProvider, helloProvider) {
helloProvider.init({
facebook: 'YOUR_FACEBOOK_APP_ID'
}, {redirect_uri: 'redirect.html'});
$urlRouterProvider.otherwise("/");
$stateProvider.state('login', {
url: "/",
templateUrl: "home.html",
controller: "LoginController"
}).state('test', {
url: "/test",
templateUrl: "test.html",
controller: "TestController"
}).state('home', {
url: "/login",
template: "home.html"
});
});
app.run(function($rootScope, store, hello, $timeout) {
$rootScope.user = null;
$rootScope.$on('$locationChangeStart', function() {
var user = store.get('user');
if (user) {
$rootScope.user = user;
} else {
self.location.href = "#/";
}
});
});
app.controller('TestController', function ($scope, $rootScope, hello, $timeout) {
$scope.whoami = "";
if( $rootScope.user === null ){
self.location.href = "#/";
}else{
$scope.whoami = "Hey " + $rootScope.user.name;
}
});
app.controller('LoginController', function ($scope, $rootScope, hello, store, $timeout) {
$scope.whoami = "";
if( $rootScope.user !== null ){
$scope.whoami = "Hey " + $rootScope.user.name;
}
$scope.login = function () {
hello('facebook').login();
};
$scope.logout = function () {
hello('facebook').logout().then(function() {
$timeout(function() {
store.remove('user');
$scope.whoami = "";
$rootScope.user = {};
});
}, function(e) {
alert('Signed out error: ' + e.error.message);
});
};
hello.on("auth.login", function (auth) {
hello(auth.network).api('/me').then(function( user ) {
var displayName = user.name.split(' ');
user.first_name = displayName[0].toLowerCase();
user.last_name = displayName[displayName.length - 1].toLowerCase();
user.fbid = user.id;
$scope.user = user;
$timeout(function() {
$rootScope.user = user;
store.set('user', user );
$scope.whoami = "Hey " + $rootScope.user.name;
});
});
});
});
})();
{
"name": "helloAngular",
"authors": [
"Roger Stringer <roger@flybase.io>"
],
"description": "",
"main": "",
"moduleType": [],
"license": "MIT",
"homepage": "",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
],
"dependencies": {
"angular-ui-router": "~0.2.15",
"ng-hello": "*"
}
}
<h1>hi</h1>
<div id="profile_facebook">{{whoami}}</div>
<a ng-click="login()" ng-show="!whoami">Login</a>
<a ng-click="logout()" ng-hide="!whoami">Logout</a>
<a href="#/test">Test</a>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title>Hello Flying Angular</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.min.js"></script>
<script type="text/javascript" src="https://cdn.flybase.io/libs/angular-storage.js"></script>
<script type="text/javascript" src="https://cdn.flybase.io/libs/hello.all.js"></script>
<script type="text/javascript" src="https://cdn.flybase.io/libs/ng-hello.js"></script>
<script type="text/javascript" src="app.js"></script>
</head>
<body ng-app="helloApp">
<div ui-view></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Hello, redirecting...</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
</head>
<body>
<div class="loading"><span>&bull;</span><span>&bull;</span><span>&bull;</span></div>
<h2>Please close this window to continue.</h2>
<script>
window.onbeforeunload = function(){
document.getElementsByTagName('h2')[0].innerHTML="Redirecting, please wait";
}
</script>
<script type="text/javascript" src="https://cdn.flybase.io/libs/hello.all.js"></script>
</body>
</html>
<h1>I am.....</h1>
<div id="profile_facebook">{{whoami}}</div>
<a href="#/">Back</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment