-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from punkyoon/feature/add-auth-endpoint
feature/add-auth-endpoint
- Loading branch information
Showing
12 changed files
with
152 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from django.contrib.auth.backends import ModelBackend | ||
|
||
from accounts.models import MockUser | ||
|
||
|
||
class EmailAuthenticationBackend(ModelBackend): | ||
def authenticate(self, request, username=None, password=None, **kwargs): | ||
try: | ||
user = MockUser.objects.get(email=username) | ||
except MockUser.DoesNotExist: | ||
return None | ||
else: | ||
if user.check_password(password): | ||
return user | ||
|
||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from rest_framework import status | ||
from rest_framework.exceptions import APIException | ||
|
||
|
||
class ProfileDoesNotExist(APIException): | ||
status_code = status.HTTP_400_BAD_REQUEST | ||
default_detail = 'User profile does not exist' | ||
default_code = '400_PROFILE_DOES_NOT_EXIST' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from django_countries.serializer_fields import CountryField | ||
from rest_framework import serializers | ||
from rest_framework_serializer_extensions.serializers import SerializerExtensionsMixin | ||
|
||
from accounts.models import MockUser, MockProfile | ||
|
||
|
||
class MockUserSerializer(SerializerExtensionsMixin, serializers.ModelSerializer): | ||
language = serializers.CharField(write_only=True, max_length=2) | ||
country = CountryField(write_only=True) | ||
locale = serializers.CharField(write_only=True, max_length=5) | ||
|
||
def create(self, validated_data): | ||
# validated_data: email, password, language, country, locale | ||
return MockUser.create(**validated_data) | ||
|
||
class Meta: | ||
model = MockUser | ||
fields = ('email', 'password', 'locale', 'country', 'language', ) | ||
write_only_fields = ('email', 'password', ) | ||
|
||
|
||
class MockProfileSerializer(SerializerExtensionsMixin, serializers.ModelSerializer): | ||
class Meta: | ||
model = MockProfile | ||
fields = ('nickname', 'locale', 'country', 'language', ) | ||
read_only_fields = ('nickname', 'locale', 'country', 'language', ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from django.urls import path | ||
|
||
from rest_auth.views import LogoutView, PasswordChangeView, PasswordResetView, PasswordResetConfirmView | ||
from rest_framework_jwt.views import obtain_jwt_token, refresh_jwt_token, verify_jwt_token | ||
|
||
from accounts.views import RegisterMockUserView, FetchMockProfileView | ||
|
||
|
||
app_name = 'accounts' | ||
urlpatterns = [ | ||
path(r'login', obtain_jwt_token, name='login'), | ||
path(r'logout', LogoutView.as_view(), name='logout'), | ||
path('register', RegisterMockUserView.as_view(), name='register'), | ||
path(r'profile', FetchMockProfileView.as_view(), name='profile'), | ||
|
||
path(r'password/change', PasswordChangeView.as_view(), name='password_change'), | ||
path(r'password/reset', PasswordResetView.as_view(), name='password_reset'), | ||
path(r'password/reset/confirm', PasswordResetConfirmView.as_view(), name='password_reset_confirm'), | ||
|
||
path('token/refresh', refresh_jwt_token), | ||
path('token/verify', verify_jwt_token), | ||
] | ||
|
||
# More details: https://django-rest-auth.readthedocs.io/en/latest/api_endpoints.html#basic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,25 @@ | ||
from django.shortcuts import render | ||
from rest_framework.generics import CreateAPIView, RetrieveAPIView | ||
from rest_framework.permissions import AllowAny, IsAuthenticated | ||
|
||
# Create your views here. | ||
from accounts.exceptions import ProfileDoesNotExist | ||
from accounts.models import MockUser, MockProfile | ||
from accounts.serializers import MockUserSerializer, MockProfileSerializer | ||
|
||
|
||
class RegisterMockUserView(CreateAPIView): | ||
model = MockUser | ||
permission_classes = (AllowAny, ) | ||
serializer_class = MockUserSerializer | ||
|
||
|
||
class FetchMockProfileView(RetrieveAPIView): | ||
permission_classes = (IsAuthenticated, ) | ||
serializer_class = MockProfileSerializer | ||
|
||
def get_object(self): | ||
try: | ||
mock_profile = MockProfile.objects.get(mock_user=self.request.user) | ||
except MockProfile.DoesNotExist: | ||
raise ProfileDoesNotExist() | ||
else: | ||
return mock_profile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import hashids | ||
|
||
|
||
HASH_IDS = hashids.Hashids(salt='MOCK-SERVER-SALT') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
from django.contrib import admin | ||
from django.urls import path, include | ||
|
||
from rest_framework_simplejwt import views | ||
|
||
|
||
urlpatterns = [ | ||
path('admin/', admin.site.urls), | ||
path('admin-api/', include('rest_framework.urls')), | ||
|
||
path('api/token/', views.TokenObtainPairView.as_view(), name='token_obtain_pair'), | ||
path('api/token/refresh/', views.TokenRefreshView.as_view(), name='token_refresh'), | ||
path('accounts/', include('accounts.urls')), | ||
] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters