Skip to content

Commit

Permalink
integrate facebook
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenkims committed Dec 12, 2015
1 parent b1b170c commit d8093fb
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
37 changes: 37 additions & 0 deletions app.py
@@ -1,6 +1,9 @@
import json
import os

import flask
import jwt
import requests
from datetime import datetime, timedelta
from flask import Flask, jsonify, request
from flask.ext.sqlalchemy import SQLAlchemy
Expand All @@ -10,13 +13,15 @@

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
app.config['TOKEN_SECRET'] = 'very secret'
app.config['FACEBOOK_SECRET'] = os.environ.get('FACEBOOK_SECRET')

db = SQLAlchemy(app)


class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(100), nullable=False)
facebook_id = db.Column(db.String(100)) # facebook_id
password = db.Column(db.String(100))

def token(self):
Expand Down Expand Up @@ -89,6 +94,38 @@ def user_info():
return jsonify(error="never reach here..."), 500


@app.route('/auth/facebook', methods=['POST'])
def auth_facebook():
access_token_url = 'https://graph.facebook.com/v2.3/oauth/access_token'
graph_api_url = 'https://graph.facebook.com/v2.5/me?fields=id,email'

params = {
'client_id': request.json['clientId'],
'redirect_uri': request.json['redirectUri'],
'client_secret': app.config['FACEBOOK_SECRET'],
'code': request.json['code']
}

# Exchange authorization code for access token.
r = requests.get(access_token_url, params=params)
# use json.loads instad of urlparse.parse_qsl
access_token = json.loads(r.text)

# Step 2. Retrieve information about the current user.
r = requests.get(graph_api_url, params=access_token)
profile = json.loads(r.text)

# Step 3. Create a new account or return an existing one.
user = User.query.filter_by(facebook_id=profile['id']).first()
if user:
return jsonify(token=user.token())

u = User(facebook_id=profile['id'], email=profile['email'])
db.session.add(u)
db.session.commit()
return jsonify(token=u.token())


@app.route('/islive')
def islive():
return "it's live"
Expand Down
19 changes: 18 additions & 1 deletion static/main.js
@@ -1,6 +1,6 @@
var app = angular.module('DemoApp', ['ui.router', 'satellizer']);

app.config(function ($stateProvider, $urlRouterProvider) {
app.config(function ($stateProvider, $urlRouterProvider, $authProvider) {

$stateProvider
.state('home', {
Expand All @@ -21,6 +21,12 @@ app.config(function ($stateProvider, $urlRouterProvider) {

$urlRouterProvider.otherwise('/home');

$authProvider.facebook({
clientId: '413108255566242',
// by default, the redirect URI is http://localhost:5000
redirectUri: 'http://localhost:5000/static/index.html'
});

});

app.run(function ($rootScope, $state, $auth) {
Expand Down Expand Up @@ -63,6 +69,17 @@ app.controller('LoginSignupCtrl', function ($scope, $auth, $state) {
console.log("error response", response);
})
};

$scope.auth = function (provider) {
$auth.authenticate(provider)
.then(function (response) {
console.debug("success", response);
$state.go('secret');
})
.catch(function (response) {
console.debug("catch", response);
})
}
});

app.controller('SecretCtrl', function ($scope, $state, $auth, $http) {
Expand Down
3 changes: 2 additions & 1 deletion static/partials/login.tpl.html
Expand Up @@ -3,4 +3,5 @@
<input type="text" ng-model="email" placeholder="Email">
<input type="password" ng-model="password" placeholder="Password">
<button ng-click="signUp()">Sign up</button>
<button ng-click="login()">Login</button>
<button ng-click="login()">Login</button>
<button ng-click="auth('facebook')">Connect with Facebook</button>

0 comments on commit d8093fb

Please sign in to comment.