Skip to content

Commit 9eedcee

Browse files
committed
Added user authentication API
1 parent a6bcf81 commit 9eedcee

File tree

5 files changed

+33
-11
lines changed

5 files changed

+33
-11
lines changed

FashVibesAPI/settings.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@
4242
'profiles_api',
4343
]
4444

45+
REST_FRAMEWORK = {
46+
'DEFAULT_PERMISSION_CLASSES': (
47+
'rest_framework.permissions.IsAuthenticated',
48+
),
49+
'DEFAULT_AUTHENTICATION_CLASSES': (
50+
'rest_framework.authentication.TokenAuthentication',
51+
)
52+
}
53+
4554
MIDDLEWARE = [
4655
'django.middleware.security.SecurityMiddleware',
4756
'django.contrib.sessions.middleware.SessionMiddleware',

FashVibesAPI/urls.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
"""
1616
from django.contrib import admin
1717
from django.urls import path, include
18+
from rest_framework.authtoken import views
1819

1920
urlpatterns = [
2021
path('admin/', admin.site.urls),
21-
path('profiles/', include('profiles_api.urls'))
22+
path('profiles/', include('profiles_api.urls')),
23+
path('api-token-auth/', views.obtain_auth_token),
2224
]

profiles_api/models.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
from django.contrib.auth.models import AbstractBaseUser
33
from django.contrib.auth.models import PermissionsMixin
44
from django.contrib.auth.models import BaseUserManager
5+
6+
from django.db.models.signals import post_save
7+
from django.dispatch import receiver
8+
from rest_framework.authtoken.models import Token
9+
from django.conf import settings
510
# Create your models here.
611

712
class UserProfileManager(BaseUserManager):
@@ -66,3 +71,9 @@ def __str__(self):
6671

6772
return self.email
6873

74+
# This code is triggered whenever a new user has been created and saved to the database
75+
76+
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
77+
def create_auth_token(sender, instance=None, created=False, **kwargs):
78+
if created:
79+
Token.objects.create(user=instance)

profiles_api/urls.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
from django.conf.urls import url, include
22
from rest_framework.routers import DefaultRouter
3-
43
from . import views
54

65
router = DefaultRouter()
76
router.register('userinfo', views.UserProfileViewSet)
8-
router.register('login', views.LoginViewSet, base_name='login')
97

108
urlpatterns = [
119
url('', include(router.urls))

profiles_api/views.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
from rest_framework import filters
23
from rest_framework import viewsets
34
from rest_framework.authentication import TokenAuthentication
@@ -20,12 +21,13 @@ class UserProfileViewSet(viewsets.ModelViewSet):
2021
search_fields = ('name', 'email', 'bio', 'web', 'full_name',)
2122

2223

23-
class LoginViewSet(viewsets.ViewSet):
24-
"""Checks email and password and returns an auth token."""
25-
26-
serializer_class = AuthTokenSerializer
27-
28-
def create(self, request):
29-
"""Use the ObtainAuthToken APIView to validate and create a token"""
24+
# class LoginViewSet(viewsets.ViewSet):
25+
# """Checks email and password and returns an auth token."""
26+
#
27+
# serializer_class = AuthTokenSerializer
28+
#
29+
# def create(self, request):
30+
# """Use the ObtainAuthToken APIView to validate and create a token"""
31+
#
32+
# return ObtainAuthToken().post(request)
3033

31-
return ObtainAuthToken().post(request)

0 commit comments

Comments
 (0)