Example #1
0
def authorized():
    """
    Authorization return for OAuth. Populates session data on login. Creates or retrieves user based on google_id and
    syncs user data with data provided by google.
    """
    resp = google.authorized_response()
    if resp is None:
        return 'Access denied: reason=%s error=%s' % (
            request.args['error_reason'],
            request.args['error_description']
        )
    session['google_token'] = (resp['access_token'], '')
    user_info = google.get('userinfo')
    session['google_id'] = user_info.data['id']
    session['name'] = user_info.data['name']
    session['email'] = user_info.data['email']
    session['picture'] = user_info.data['picture']
    # Get or create user and add the user_id to the session
    session['user_id'] = get_user_id(session['google_id']) or create_user(session)[0].id
    # Sync user data from google
    user = get_user(session['user_id'])
    user.name = session['name']
    user.email = session['email']
    user.picture = session['picture']
    db.session.add(user)
    db.session.commit()

    flash('Signed in as %s' % session['name'], 'notice')
    return redirect(url_for('category_list'))
Example #2
0
def context_check_login():
    """
    Context processor to check if a user is logged in.
    :return: dict containing boolean signifying if there is a logged in user and instance of the user
    """
    logged_in = check_login(session)
    user = None

    if logged_in:
        user = get_user(session.get('user_id'))

    return dict(logged_in=logged_in, user=user)