YellWOWPages/yellow/auth.py
2022-03-13 15:33:21 +02:00

28 lines
741 B
Python

import peewee
from quart import session, redirect, url_for
from yellow.factory import openid
from yellow.models import User
@openid.after_token()
async def handle_user_login(resp: dict):
access_token = resp["access_token"]
openid.verify_token(access_token)
user = await openid.user_info(access_token)
username = user['preferred_username']
uid = user['sub']
try:
user = User.select().where(User.id == uid).get()
except peewee.DoesNotExist:
user = None
if not user:
# create new user if it does not exist yet
user = User.create(id=uid, username=username)
# user is now logged in
session['user'] = user.to_json()
return redirect(url_for('bp_routes.dashboard'))