숨밈
들숨에 건강을 날숨에 재력을
숨밈
전체 방문자
오늘
어제
  • 분류 전체보기 (55)
    • 💻 프로젝트 (8)
      • 🍝 홍잇 (5)
      • 🏕 캠퍼 (3)
    • 👩‍💻 개발 (30)
      • ⚙️ BACKEND (16)
      • 🖥 FRONTEND (3)
      • 📡 DEVOPS (7)
      • 💡SOFTWARE (4)
    • 📑 개발 이론 (13)
      • 🚎 JAVA (1)
      • 🌱 SPRING (12)
    • 📚 CS (2)
      • 🔎 Infra (2)
    • 📔 회고 (2)

블로그 메뉴

  • 홈
  • 태그
  • 글쓰기

인기 글

태그

  • django-rest-auth
  • 자바스크립트
  • notion
  • querydsl
  • 타임리프
  • django-auth
  • static final
  • Django
  • jsp
  • 스프링부트
  • django-rest-auth_custom
  • springboot
  • 스프링
  • 프리코스
  • Tistory

티스토리

hELLO · Designed By 정상우.
숨밈

들숨에 건강을 날숨에 재력을

[Django] DRF JWT 회원가입, 로그인 기능/ dj-rest-auth
👩‍💻 개발/⚙️ BACKEND

[Django] DRF JWT 회원가입, 로그인 기능/ dj-rest-auth

2022. 12. 22. 22:27

 

Simple-jwt로 회원가입 기능을 직접 구현할 수 있지만

필자는 빠르고 신속하게 구현하기 위해 dj-rest-auth를 사용하여 구현했다.

 

간단한 커스텀은 이후 포스팅에 기록할 예정!

또한 장고 설치 및 구성은 패스하고 dj-rest-auth 관한 내용만 정리하겠다.

 

setting.py 추가

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # my-app
    'api',

    # third party
    'rest_framework',
    # auth
    'rest_framework.authtoken',
    'dj_rest_auth',
    # login
    'django.contrib.sites',

    'allauth',
    'allauth.account',

    'dj_rest_auth.registration',
]

# dj-rest-auth
REST_USE_JWT = True

JWT_AUTH_COOKIE = 'api-auth'
JWT_AUTH_REFRESH_COOKIE = 'api-refresh-token'

# django-allauth
SITE_ID = 1
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_USERNAME_REQUIRED = True
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = "none"
# ACCOUNT_AUTHENTICATION_METHOD = 'email'
# ACCOUNT_EMAIL_VERIFICATION = 'none'

설치한 라이브러리를 사용할 수 있도록 추가 (배포 시 requirement.txt 추가 필수)

 

dj-rest-auth 관련 

  • REST_USE_JWT = True : 필수 
  • JWT_AUTH_COOKIE ='your app cookie name' : 필수 / 설정 값없을 시 logout 제대로 작동 안 함
  • JWT_AUTH_REFRESH_COOKIE = refresh token 사용 시 설정 

django-allauth 관련

※ dj-rest-auth가 django-allauth 기반으로 구현됨

  • SITE_ID:  해당 도메인의 id 
  • ACCOUNT_UNIQUE_EMAIL: User email unique 사용 여부
  • ACCOUNT_USER_MODEL_USERNAME_FIELD: User username type
  • ACCOUNT_USERNAME_REQUIRED: User username 필수 여부
  • ACCOUNT_EMAIL_REQUIRED: User email 필수 여부 / uniquekey로 생성
  • ACCOUNT_AUTHENTICATION_METHOD: 로그인 인증 수단
  • ACCOUNT_EMAIL_VERIFICATION: Email 인증 필수 여부 / 필자는 none으로 설정

더 많은 변수가 있으니 찾아보는 것을 추천! - 필자는 여기까지...

api/urls.py

urlpatterns = [
    path('auth/', include('dj_rest_auth.urls')),
    path('auth/registration/', include('dj_rest_auth.registration.urls'))
]

* 필자는 account를 따로 만들지 않고 api 내부에 auth를 생성

   따로 만들고 싶다면 최상단의 urls에 위와 같이 연결 필요

 

위와 같이 설정해 주면 다음과 같은 url을 사용할 수 있음

  • http://localhost:8000/api/auth/password/reset/
  • http://localhost:8000/api/auth/password/reset/confirm/
  • http://localhost:8000/api/auth/login/
  • http://localhost:8000/api/auth/logout/
  • http://localhost:8000/api/auth/user/
  • http://localhost:8000/api/auth/password/change/
  • http://localhost:8000/api/auth/token/verify/
  • http://localhost:8000/api/auth/token/refresh/
  • http://localhost:8000/api/auth/registration/

 


공식 문서 :

https://dj-rest-auth.readthedocs.io/en/latest/installation.html#json-web-token-jwt-support-optional

 

Installation — dj-rest-auth 2.2.5 documentation

Using django-allauth, dj-rest-auth provides helpful class for creating social media authentication view. Note Points 1 and 2 are related to django-allauth configuration, so if you have already configured social authentication, then please go to step 3. See

dj-rest-auth.readthedocs.io

출처 및 참고:

https://minwoo.kim/posts/create-register-and-jwt-login-api-using-django-rest-framework/

 

Django REST framework (DRF) JWT로 로그인 및 회원가입 API 구현 (dj-rest-auth)

Django, Django REST framework을 이용하여, 로그인 및 회원가입을 구현해보도록 하겠습니다. 최대한 직접 구현하지 않고, 빠르게 구현하는 데 초점을 맞추었습니다. Django와 관련된 package들을 활용하여

minwoo.kim

 

저작자표시 (새창열림)

'👩‍💻 개발 > ⚙️ BACKEND' 카테고리의 다른 글

[Spring/Batch] Step간 변수 공유 - ExecutionContext  (1) 2025.01.28
[Django] dj-rest-auth Custom  (0) 2022.12.22
[Django] DRF Method Override 방법  (1) 2022.10.08
[Django] ViewSet , Router  (4) 2022.10.08
[Django] admin 계정 생성  (0) 2022.10.08
    '👩‍💻 개발/⚙️ BACKEND' 카테고리의 다른 글
    • [Spring/Batch] Step간 변수 공유 - ExecutionContext
    • [Django] dj-rest-auth Custom
    • [Django] DRF Method Override 방법
    • [Django] ViewSet , Router
    숨밈
    숨밈
    기술블로그

    티스토리툴바