login/logout/sign up
Installation
장고로 로그인 기능을 구현하기 위해서는 djnago-rest-auth라는 패키지를 설치해 줘야 한다.
pip install django-rest-auth
그 다음 installed_apps에 아래의 코드를 추가해 준다.
INSTALLED_APPS = (
...,
'rest_framework',
'rest_framework.authtoken',
...,
'rest_auth'
)
그 다음 urls.py에 아래의 코드를 추가해 준다.
urlpatterns = [
...,
url(r'^rest-auth/', include('rest_auth.urls'))
]
여기서 jwt토큰 url을 삭제해 준 후 위 url을 추가해 주자.
<변경 전>
urlpatterns = [
# Django Admin, use {% url 'admin:index' %}
path("api-token-auth/", obtain_jwt_token),
# User management
path('rest-auth/', include('rest_auth.urls')),
path('rest-auth/registration/', include('rest_auth.registration.urls')),
path(
"users/",
include("instagram.users.urls", namespace="users"),
),
path(
"notifications/",
include("instagram.notifications.urls", namespace="notifications"),
),
path(
"images/",
include("instagram.images.urls", namespace="images"),
),
path("accounts/", include("allauth.urls")),
# Your stuff: custom urls includes go here
] + static(
settings.MEDIA_URL, document_root=settings.MEDIA_ROOT
)
<변경 후>
urlpatterns = [
# Django Admin, use {% url 'admin:index' %}
path(settings.ADMIN_URL, admin.site.urls),
# User management
path('rest-auth/', include('rest_auth.urls')),
path('rest-auth/registration/', include('rest_auth.registration.urls')),
path(
"users/",
include("instagram.users.urls", namespace="users"),
),
path(
"notifications/",
include("instagram.notifications.urls", namespace="notifications"),
),
path(
"images/",
include("instagram.images.urls", namespace="images"),
),
path("accounts/", include("allauth.urls")),
# Your stuff: custom urls includes go here
] + static(
settings.MEDIA_URL, document_root=settings.MEDIA_ROOT
)
다 추가했으면 아래 명령어로 마이그레이션을 해 주자.
python manage.py migrate
Registration(optional)
마이그레이트를 해 줬으면 이제 Registration을 할 수 있다.
사용하기 위해 installed_apps에 추가해 준다.
INSTALLED_APPS = (
...,
'rest_auth.registration',
)
그리고 urls.py에 아래 코드를 추가해 준다.
urlpatterns = [
...,
url(r'^rest-auth/registration/', include('rest_auth.registration.urls'))
]
여기서 로그인을 하기 전에 jwt를 사용하고 싶으면 아래의 코드를 settings.py 맨 아래에 추가해 준다.
REST_USE_JWT = True
ex)

API endpoints
login

공식 문서를 보면 위 사진처럼 username과 emal, password가 필요하다. 그런데 여기서 email을 사용하고 싶지 않을 경우 아래와 같이 작업해 주면 된다.
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
# 'rest_framework.authentication.SessionAuthentication',
# 'rest_framework.authentication.BasicAuthentication',
),
}
Session과 BasicAuth를 사용하지 않을 것이기 때문에 아래 두 코드를 주석처리 하거나 삭제 해 준 후 아래와 같이 해 준다.
ACCOUNT_EMAIL_REQUIRED = False
# https://django-allauth.readthedocs.io/en/latest/configuration.html
ACCOUNT_EMAIL_VERIFICATION = None
이제 postman에서 확인해 보면 로그인을 성공적으로 할 수 있는 것을 볼 수 있다.

logout
이제 로그아웃을 해 보겠다.
로그아웃을 위해서 ACCOUNT_LOGOUT_ON_GET = True를 settings.py에 추가해 준다.
# Your stuff...
# ------------------------------------------------------------------------------
TAGGIT_CASE_INSENSITIVE = True
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
# 'rest_framework.authentication.SessionAuthentication',
# 'rest_framework.authentication.BasicAuthentication',
),
}
REST_USE_JWT = True
ACCOUNT_LOGOUT_ON_GET = True
원래 기술적으로는 post를 사용해야 하고 get으로 데이터베이스의 모양을 변형시키면 안 된다.
하지만 해당 케이스의 경우 get을 허용하고 있고 get이 더 편하기 때문에 get으로 로그아웃을 하겠다.
sign up
먼저 회원가입을 하기 전에 base.py에서 아래 코드를 삭제 해 준다.
LOGIN_REDIRECT_URL = 'users:redirect'
그리고 회원가입을 해 보자

여기서 password1, password2를 해 주는 이유는 비번확인을 하는 작업이다.