현제의 현재이야기

[OSOD] dj-rest-auth custom + logout 본문

DRF/OSOD

[OSOD] dj-rest-auth custom + logout

현재의 현제 2023. 1. 26. 21:22

| dj-rest-auth custom

기존에 nickname을 추가할 수 없었던 것을 고침

 

accounts/register

{
    "email": "test@gmail.com",
    "password1": "leee1234",
    "password2": "leee1234",
    "nickname": "test"
}

result

accounts/login

{
    "email": "test@gmail.com",
    "password": "leee1234"
}

result

해결법

 

How to serialize custom user model and register new instances with Django REST Framework

Most of the time, when you are building a real-world web application, Django's built-in user model will not be a perfect fit for your application because you might need to collect other vital user information that are not part of the built-in user mo...

tolu.hashnode.dev

  • 이곳에서 serializers를 작성
  • get_cleaned data 함수로 nickname을 추가로 받고 save함수로 데이터를 저장. 실제로 이 어뎁터가 담긴 save를 넣지 않으면 닉네임이 저장되지 않는다.
 

Custom users using Django REST framework | Kraken Systems Ltd.

The built-in Django User model follows the pattern consisted of username, email and password. In this tutorial you will learn to RESTfully simplify it to just email and password.

krakensystems.co

  • 로그인되고 nickname을 안 뱉어주길래 여기서 serialzier를 만들고 USER_DETAILS_SERIALIZER에서 추가함

Serializers.py

from rest_framework import serializers
from .models import *
from django.db import transaction
from dj_rest_auth.registration.serializers import RegisterSerializer
from allauth.account.adapter import get_adapter

class UserSerializer(RegisterSerializer):
    nickname = serializers.CharField(max_length=50)
    class Meta:
        model = User
        fields = ['email', 'password', 'nickname']

    def get_cleaned_data(self):
        super(UserSerializer, self).get_cleaned_data()
        return {
            'email': self.validated_data.get('email', ''),
            'password1': self.validated_data.get('password1', ''),
            'password2': self.validated_data.get('password2', ''),
            'nickname': self.validated_data.get('nickname', '')
        }
    def save(self, request):
        adapter = get_adapter()
        user = adapter.new_user(request)
        self.cleaned_data = self.get_cleaned_data()
        user.nickname = self.cleaned_data.get('nickname')
        user.save()
        adapter.save_user(request, user, self)
        return user

class UserDetailSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['id', 'email', 'password', 'nickname']

settings.py

REST_AUTH_REGISTER_SERIALIZERS = {
    'REGISTER_SERIALIZER': 'accounts.serializers.UserSerializer',
}
REST_AUTH_SERIALIZERS = {
    'USER_DETAILS_SERIALIZER': 'accounts.serializers.UserDetailSerializer',
}

잘 들어온다 ^^

 

| logout

accounts/logout

{
"refresh": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTY3NTM0MjM3OSwiaWF0IjoxNjc0NzM3NTc5LCJqdGkiOiJiMDU0NTEwNzMwZWQ0NTc1ODQwMjQ5N2U2NmUwYjVlMSIsInVzZXJfaWQiOjJ9.-LOBj20xEy4QNJRwViDz6v89UqiKpp-if-PU3k9K7fU"
}
  • 이렇게 refresh 토큰을 로그아웃 바디에 넣고 post를 누르면, blacklist에 refresh token이 넣어져서 쓸 수 없게된다.

 

Comments