Beispiel #1
0
def oauth_callback(provider):
    if g.user is not None and g.user.is_authenticated:
        return redirect(url_for('index'))
    signin = OAuthSignIn.get_provider(provider)
    resp = signin.authorized_response()
    if resp is None:
        flash('Access denied: %s\n%s' % (
                request.args['error'],
                request.args['error_description']),
                 'error')
        return render_template('500.html', title='Error-500')
    signin.store_token(resp)
    user_name = signin.get_username()
    social_id = '$'.join([provider, user_name])
    user = User.query.filter_by(social_id=social_id).first()
    if user is None:
        nickname = user_name
        nickname = User.make_valid_nickname(nickname)
        nickname = User.make_unique_nickname(nickname)
        user = User(nickname=nickname, social_id=social_id)
        flash("Creating account: nickname=%s\nsocial_id=%s\n" % (
            nickname, social_id)
        )
        db.session.add(user)
        db.session.commit()
        db.session.add(user.follow(user))
        db.session.commit()
    remember_me=False
    if 'remember_me' in session:
        remember_me = session['remember_me']
        session.pop('remember_me', None)
    login_user(user, remember=remember_me)
    return redirect(request.args.get('next') or url_for('index'))
Beispiel #2
0
def oauth_authorize(provider):
    if not current_user.is_anonymous:
        return redirect(url_for('index'))
    signin = OAuthSignIn.get_provider(provider)
    return signin.authorize()