fix: 500 error caused by duplicate email or username

This commit is contained in:
wangruidong
2024-08-30 17:57:48 +08:00
committed by Bryan
parent 8755ece633
commit 55e8e34226
4 changed files with 58 additions and 8 deletions

View File

@@ -17,13 +17,16 @@ import time
from django.conf import settings
from django.contrib import auth
from django.core.exceptions import SuspiciousOperation
from django.db import IntegrityError
from django.http import HttpResponseRedirect, QueryDict
from django.urls import reverse
from django.utils.crypto import get_random_string
from django.utils.http import urlencode
from django.views.generic import View
from django.utils.translation import gettext_lazy as _
from authentication.utils import build_absolute_uri_for_oidc
from authentication.views.mixins import FlashMessageMixin
from common.utils import safe_next_url
from .utils import get_logger
@@ -113,7 +116,7 @@ class OIDCAuthRequestView(View):
return HttpResponseRedirect(redirect_url)
class OIDCAuthCallbackView(View):
class OIDCAuthCallbackView(View, FlashMessageMixin):
""" Allows to complete the authentication process.
This view acts as the main endpoint to complete the authentication process involving the OIDC
@@ -165,7 +168,13 @@ class OIDCAuthCallbackView(View):
next_url = request.session.get('oidc_auth_next_url', None)
code_verifier = request.session.get('oidc_auth_code_verifier', None)
logger.debug(log_prompt.format('Process authenticate'))
user = auth.authenticate(nonce=nonce, request=request, code_verifier=code_verifier)
try:
user = auth.authenticate(nonce=nonce, request=request, code_verifier=code_verifier)
except IntegrityError:
title = _("OpenID Error")
msg = _('Username or email already exists, Please check')
response = self.get_failed_response('/', title, msg)
return response
if user:
logger.debug(log_prompt.format('Login: {}'.format(user)))
auth.login(self.request, user)