[dj-rest-auth] CustomRegisterSerializer

·2023년 10월 25일
0

에러

목록 보기
17/24

이메일 인증을 하기 위해 dj-rest-auth를 사용하던 중 nickname 필드를 추가해야 해서
https://www.rootstrap.com/blog/registration-and-authentication-in-django-apps-with-dj-rest-auth 를 참고로 시리얼라이저와 ConfirmRegisterView 를 커스텀했는데 막상 dj-rest-auth/registration/ 으로 접속해보니 필드가 그대로였다.

알고보니 views.py 의 ConfirmRegisterView 에도 커스텀한 시리얼라이저를 사용하겠다는 것을 명시해줘야 했다는 것

accounts/urls.py

from rest_framework_simplejwt.views import (
    TokenObtainPairView,
    TokenRefreshView,
)
from dj_rest_auth.registration.views import VerifyEmailView
from django.urls import path, include, re_path
from accounts import views


urlpatterns = [
    path("signup/", views.AccountCreateView.as_view(), name="signup_view"),
    path("dj-rest-auth/", include("dj_rest_auth.urls")),
    # path("dj-rest-auth/registration/", include("dj_rest_auth.registration.urls")),
    path(
        "dj-rest-auth/registration/",
        views.CustomRegisterView.as_view(), # 내가 커스텀한 RegisterView 사용
        name="register",
    ),
    # 유효한 이메일이 유저에게 전달
    re_path(
        r"^account-confirm-email/$",
        VerifyEmailView.as_view(),
        name="account_email_verification_sent",
    ),
    # 유저가 클릭한 이메일(=링크) 확인
    re_path(
        r"^account-confirm-email/(?P<key>[-:\w]+)/$",
        views.ConfirmEmailView.as_view(),
        name="account_confirm_email",
    ),
    path("profile/<int:user_id>/", views.ProfileView.as_view(), name="profile"),
    path(
        "api/token/",
        views.CustomTokenObtainPairView.as_view(),
        name="token_obtain_pair",
    ),
    path("api/token/refresh/", TokenRefreshView.as_view(), name="token_refresh"),
    path("profile/<int:user_id>/", views.ProfileView.as_view(), name="profile"),
    path("<int:user_id>/mypage/", views.MyPageView.as_view(), name="my_page_view"),
    path("mocks/", views.Token_Test.as_view(), name="login_test"),
]

accounts/views.py

class CustomRegisterView(RegisterView):
    serializer_class = CustomRegisterSerializer # 이 부분!!!!
    permission_classes = [permissions.AllowAny]

class ConfirmEmailView(APIView):
    
    permission_classes = [AllowAny]

    def get(self, *args, **kwargs):
        self.object = confirmation = self.get_object()
        confirmation.confirm(self.request)
        # A React Router Route will handle the failure scenario
        return HttpResponseRedirect("http://127.0.0.1:5500/login.html")  # 인증성공

    def get_object(self, queryset=None):
        key = self.kwargs["key"]
        email_confirmation = EmailConfirmationHMAC.from_key(key)
        if not email_confirmation:
            if queryset is None:
                queryset = self.get_queryset()
            try:
                email_confirmation = queryset.get(key=key.lower())
            except EmailConfirmation.DoesNotExist:
                # A React Router Route will handle the failure scenario
                return HttpResponseRedirect("http://127.0.0.1:5500/login.html")  # 인증실패
        return email_confirmation

    def get_queryset(self):
        qs = EmailConfirmation.objects.all_valid()
        qs = qs.select_related("email_address__user")
        return qs
profile
공부 중

0개의 댓글

관련 채용 정보