Handle the callback after a successful OAuth authentication. If the user already exists with this OAuth account, the token is updated. If a user with the same e-mail already exists and `associate_by_email` is True, the OAuth account is associated to this user.
(
self: "BaseUserManager[models.UOAP, models.ID]",
oauth_name: str,
access_token: str,
account_id: str,
account_email: str,
expires_at: int | None = None,
refresh_token: str | None = None,
request: Request | None = None,
*,
associate_by_email: bool = False,
is_verified_by_default: bool = False,
)
| 147 | return created_user |
| 148 | |
| 149 | async def oauth_callback( |
| 150 | self: "BaseUserManager[models.UOAP, models.ID]", |
| 151 | oauth_name: str, |
| 152 | access_token: str, |
| 153 | account_id: str, |
| 154 | account_email: str, |
| 155 | expires_at: int | None = None, |
| 156 | refresh_token: str | None = None, |
| 157 | request: Request | None = None, |
| 158 | *, |
| 159 | associate_by_email: bool = False, |
| 160 | is_verified_by_default: bool = False, |
| 161 | ) -> models.UOAP: |
| 162 | """ |
| 163 | Handle the callback after a successful OAuth authentication. |
| 164 | |
| 165 | If the user already exists with this OAuth account, the token is updated. |
| 166 | |
| 167 | If a user with the same e-mail already exists and `associate_by_email` is True, |
| 168 | the OAuth account is associated to this user. |
| 169 | Otherwise, the `UserNotExists` exception is raised. |
| 170 | |
| 171 | If the user does not exist, it is created and the on_after_register handler |
| 172 | is triggered. |
| 173 | |
| 174 | :param oauth_name: Name of the OAuth client. |
| 175 | :param access_token: Valid access token for the service provider. |
| 176 | :param account_id: Id of the account on the external OAuth service. |
| 177 | :param account_email: E-mail of the user on the service provider. |
| 178 | :param expires_at: Optional timestamp at which the access token expires. |
| 179 | :param refresh_token: Optional refresh token to get a |
| 180 | fresh access token from the service provider. |
| 181 | :param request: Optional FastAPI request that |
| 182 | triggered the operation, defaults to None |
| 183 | :param associate_by_email: If True, any existing user with the same |
| 184 | e-mail address will be associated to this user. Defaults to False. |
| 185 | :param is_verified_by_default: If True, the `is_verified` flag will be |
| 186 | set to `True` on newly created user. Make sure the OAuth Provider you're |
| 187 | using does verify the email address before enabling this flag. |
| 188 | Defaults to False. |
| 189 | :return: A user of type models.UOAP. |
| 190 | """ |
| 191 | oauth_account_dict = { |
| 192 | "oauth_name": oauth_name, |
| 193 | "access_token": access_token, |
| 194 | "account_id": account_id, |
| 195 | "account_email": account_email, |
| 196 | "expires_at": expires_at, |
| 197 | "refresh_token": refresh_token, |
| 198 | } |
| 199 | |
| 200 | try: |
| 201 | user = await self.get_by_oauth_account(oauth_name, account_id) |
| 202 | except exceptions.UserNotExists: |
| 203 | try: |
| 204 | # Associate account |
| 205 | user = await self.get_by_email(account_email) |
| 206 | if not associate_by_email: |