Skip to content

Commit

Permalink
perf: 优化oauth2登录未激活用户时,会重复跳转登录问题
Browse files Browse the repository at this point in the history
  • Loading branch information
O-Jiangweidong authored and BaiJiangJie committed Mar 6, 2024
1 parent a7a099f commit 670eac4
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions apps/authentication/backends/oauth2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.utils.http import urlencode
from django.utils.translation import gettext_lazy as _

from authentication.utils import build_absolute_uri
from common.utils import get_logger
from authentication.views.mixins import FlashMessageMixin
from authentication.mixins import authenticate
from common.utils import get_logger


logger = get_logger(__file__)

Expand Down Expand Up @@ -39,7 +42,7 @@ def get(self, request):
return HttpResponseRedirect(redirect_url)


class OAuth2AuthCallbackView(View):
class OAuth2AuthCallbackView(View, FlashMessageMixin):
http_method_names = ['get', ]

def get(self, request):
Expand All @@ -51,6 +54,11 @@ def get(self, request):
if 'code' in callback_params:
logger.debug(log_prompt.format('Process authenticate'))
user = authenticate(code=callback_params['code'], request=request)

if err_msg := getattr(request, 'error_message', ''):
login_url = reverse('authentication:login') + '?admin=1'
return self.get_failed_response(login_url, title=_('Authentication failed'), msg=err_msg)

if user and user.is_valid:
logger.debug(log_prompt.format('Login: {}'.format(user)))
auth.login(self.request, user)
Expand Down

0 comments on commit 670eac4

Please sign in to comment.