현제의 현재이야기

[OSOD] dj-rest-auth + jwt 로그인 본문

DRF/OSOD

[OSOD] dj-rest-auth + jwt 로그인

현재의 현제 2023. 1. 26. 15:53

pip install

pip install djangorestframework
pip install djangorestframework-simplejwt
pip install django-allauth
pip install dj-rest-auth

settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'accounts',
    'writing',
    'rest_framework_simplejwt.token_blacklist',
    'rest_framework.authtoken',
    'dj_rest_auth',
    'dj_rest_auth.registration',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
]

AUTH_USER_MODEL = 'accounts.User'

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.SessionAuthentication',
        'dj_rest_auth.jwt_auth.JWTCookieAuthentication',
    ),
}
LANGUAGE_CODE = 'ko-kr'

TIME_ZONE = 'Asia/Seoul'

ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_VERIFICATION = 'none'
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
REST_USE_JWT = True

models.py

class User(AbstractUser):
    email = models.EmailField(max_length=50,unique=True)
    password = models.CharField(max_length=512)
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []
    username = None
    nickname = models.CharField(max_length=50, unique=True, null=True)
  • username = None -> 이걸 해줘야 username 중복이 나지 않는다. migrate시 필드를 없애는 기능
  • USERNAME_FIELD = 'email'
        REQUIRED_FIELDS = [] 이게 있어야 email을 username으로 인식한다.
  • 근데 닉네임은 어떻게 추가할까...?

login

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

signup

{
    "email": "nick@gmail.com",
    "password1": "leee1234",
    "password1": "leee1234"
}

result

닉네임은 어떻게 추가하지..?

Comments