Esempio n. 1
0
def login():
    if session.get('email', None):
        return fail('Already logged in')

    access_token = session.get('fb_token', None)
    if access_token is None:
        return fail('No facebook access token')

    r = requests.get('https://graph.facebook.com/v2.3/me',
                     params={
                         'fields': 'email',
                         'access_token': access_token,
                     })

    data = r.json()

    if 'error' in data:
        return fail(data['error']['message'])

    email = data.get('email', None)

    if not email:
        return fail('Bad response from facebook API')

    user = User.query.filter_by(email=email).first()

    if not user:
        return fail('No account')

    session['email'] = email
    return success(user)
Esempio n. 2
0
    def decorated_function(*args, **kwargs):
        user = g.get('user', None)
        if user is None:
            email = session.get('email', None)
            if not email:
                return fail('No email found')

            user = User.query.filter_by(email=email).first()

            if not user:
                return fail('No account with email found')

            g.user = user

        return f(*args, **kwargs)
Esempio n. 3
0
def oauth_callback():
    code = request.args.get('code', '')

    if not code:
        return abort(404)

    r = requests.get('https://graph.facebook.com/v2.3/oauth/access_token',
                     params={
                         'client_id': config['FACEBOOK']['APP_ID'],
                         'client_secret': config['FACEBOOK']['APP_SECRET'],
                         'redirect_uri': config['FACEBOOK']['REDIRECT_URI'],
                         'code': code,
                     })

    data = r.json()

    if 'error' in data:
        return fail(data['error']['message'])

    access_token = data.get('access_token', None)

    if not access_token:
        return error('Didn\'t receive access token?')

    session['fb_token'] = access_token

    return success(access_token)
Esempio n. 4
0
def new_connection():
    public_token = request.form.get('public_token', None)
    if public_token is None:
        return fail('Missing fields: public_token')

    client.exchange_token(public_token)

    return login_succeeded()
Esempio n. 5
0
def new_account():
    access_token = session.get('fb_token', None)
    if access_token is None:
        return fail('No login token')

    r = requests.get('https://graph.facebook.com/v2.3/me',
                     params={
                         'fields': 'email,first_name,last_name',
                         'access_token': access_token,
                     })

    data = r.json()

    if 'error' in data:
        return fail(data['error']['message'])

    email = data.get('email', None)
    first_name = data.get('first_name', None)
    last_name = data.get('last_name', None)

    if not email or not first_name or not last_name:
        return fail('Missing fields')

    user = User.query.filter_by(email=email).first()

    if user:
        return fail('User already exists')

    user = User(
        name='{} {}'.format(first_name, last_name),
        email=email,
    )

    db.session.add(user)
    db.session.commit()

    session['email'] = email
    return success(user)