Define a view function to handle the login process. Decorate it with @oid.loginhandler. The handler should:
- Check if the user is already logged in using
g.user and redirect via oid.get_next_url() if so. - On
POST, extract the openid from the form and call oid.try_login(openid, ...). - Use
oid.get_next_url() to provide the next value for the template. - Use
oid.fetch_error() to display errors in the template.
When calling try_login, you can use ask_for (required) and ask_for_optional (optional) to request profile information like email, nickname, or fullname from the provider.
@app.route('/login', methods=['GET', 'POST'])
@oid.loginhandler
def login():
if g.user is not None:
return redirect(oid.get_next_url())
if request.method == 'POST':
openid = request.form.get('openid')
if openid:
return oid.try_login(openid, ask_for=['email', 'nickname'],
ask_for_optional=['fullname'])
return render_template('login.html', next=oid.get_next_url(),
error=oid.fetch_error())