def oauth_callback(provider): if not current_user.is_anonymous: return redirect(url_for('index')) oauth = OAuthSignIn.get_provider(provider) username, email, picture, source = oauth.callback() if email is None: # I need a valid email address for my user identification flash('Authentication failed.') return redirect(url_for('index')) # Look if the user already exists user = session.query(User).filter_by(email=email).first() if not user: # Create the user. Try and use their name returned by Google, # but if it is not set, split the email address at the @. nickname = username if nickname is None or nickname == "": nickname = email.split('@')[0] print "nickname: ", nickname # We can do more work here to ensure a unique nickname, if you # require that. user = User(username=nickname, email=email, picture=picture, user_source=source) session.add(user) session.commit() # Log in the user, by default remembering them for their next visit # unless they log out. user.last_login_date = datetime.utcnow() session.add(user) session.commit() login_user(user, remember=False) return redirect(url_for('index'))