def _create_user(first_name: str, last_name: str) -> User:
    user = User()
    user.first_name = first_name
    user.last_name = last_name
    user.username = f"{first_name}_{last_name}"
    user.email = f"{first_name}_{last_name}@southwestern.edu"
    user.profile_url = f"https://cdn.google.com/{first_name}_{last_name}.png"
    return user
Esempio n. 2
0
async def get_or_create_user(flow: google_auth_oauthlib.flow.Flow, info,
                             Authorize: AuthJWT) -> UserCreate:
    google_creds = flow.credentials

    user = await User.get_or_none(email=info["email"]).first()

    if user is None:
        user = User()
        user.username = info["email"].split("@")[0]
        user.first_name = info["given_name"]
        user.last_name = info["family_name"]
        user.email = info["email"]

    user.profile_url = info["picture"]
    await user.save()
    creds, bo = await Credentials.get_or_create(user=user)
    creds.json_field = google_creds.to_json()
    await creds.save()

    return UserCreate(
        user=await User_Pydnatic.from_tortoise_orm(user),
        access_token=Authorize.create_access_token(subject=user.email),
        refresh_token=Authorize.create_refresh_token(subject=user.email),
    )