Example #1
0
 def dispatch_request(self, provider_name):
     if not current_user.is_anonymous:
         return redirect(url_for('index'))
     oauth = OAuthSignIn(provider_name=provider_name)
     logged_user = oauth.callback()
     if not logged_user:
         flash(_('Authentication failed.'))
         return redirect(url_for('index'))
     try:
         user = User.objects.get(email=logged_user['email'])
         user.avatar = logged_user['avatar']
         user.save()
     except DoesNotExist:
         user = User(**logged_user)
         user.save()
     login_user(user, remember=True)
     return redirect(request.args.get('next') or url_for('index'))
Example #2
0
def callback(provider):
    """"Callback from the OAuth provider."""
    if not current_user.is_anonymous():
        return redirect(url_for('main_page'))
    oauth = OAuthSignIn.get_provider(provider)
    username, email = oauth.callback()
    if email is None:
        # TODO: OAuth failed -> redirect somewhere else
        return redirect(url_for('main_page'))
    # Check if the user exists in the db, else create
    user = models.User.query.filter_by(email=email).first()
    if not user:
        user = models.User(email, username)
        db.session.add(user)
        db.session.commit()
    login_user(user, remember=True)
    user.authenticated = True
    db.session.add(user)
    db.session.commit()
    return redirect(url_for('dashboard'))
Example #3
0
 def dispatch_request(self, provider_name):
     if not current_user.is_anonymous:
         return redirect(url_for('index'))
     oauth = OAuthSignIn(provider_name=provider_name)
     return oauth.authorize()
Example #4
0
def oauth_authorize(provider):
    """Requests authorization to the OAuth provider."""
    oauth = OAuthSignIn.get_provider(provider)
    return oauth.authorize()