diff --git a/smugfolioapi/views/__init__.py b/smugfolioapi/views/__init__.py index e69de29..0801135 100644 --- a/smugfolioapi/views/__init__.py +++ b/smugfolioapi/views/__init__.py @@ -0,0 +1 @@ +from .auth import login_user, register_user \ No newline at end of file diff --git a/smugfolioapi/views/auth.py b/smugfolioapi/views/auth.py index e69de29..94faa9a 100644 --- a/smugfolioapi/views/auth.py +++ b/smugfolioapi/views/auth.py @@ -0,0 +1,68 @@ +import email +from django.contrib.auth import authenticate +from django.contrib.auth.models import User +from rest_framework.authtoken.models import Token +from rest_framework.decorators import api_view, permission_classes +from rest_framework.permissions import AllowAny +from rest_framework.response import Response +from smugfolioapi.models import Smug_Users + +@api_view(['POST']) +@permission_classes([AllowAny]) +def login_user(request): + '''Handles the authentication of a smug_users + + Method arguments: + request -- The full HTTP request object + ''' + username = request.data['username'] + password = request.data['password'] + + # Use the built-in authenticate method to verify + # authenticate returns the user object or None if no user is found + authenticated_user = authenticate(username=username, password=password) + + # If authentication was successful, respond with their token + if authenticated_user is not None: + token = Token.objects.get(user=authenticated_user) + data = { + 'valid': True, + 'token': token.key + } + return Response(data) + else: + # Bad login details were provided. So we can't log the user in. + data = { 'valid': False } + return Response(data) + +@api_view(['POST']) +@permission_classes([AllowAny]) +def register_user(request): + '''Handles the creation of a new smug_users for authentication + + Method arguments: + request -- The full HTTP request object + ''' + + # Create a new user by invoking the `create_user` helper method + # on Django's built-in User model + new_user = User.objects.create_user( + username=request.data['username'], + password=request.data['password'], + first_name=request.data['first_name'], + last_name=request.data['last_name'], + email =request.data['email'] + ) + + # Now save the extra info in the levelupapi_smug_users table + smug_users = Smug_Users.objects.create( + business_name=request.data['business_name'], + business_owner=request.data['business_owner'], + user=new_user + ) + + # Use the REST Framework's token generator on the new user account + token = Token.objects.create(user=smug_users.user) + # Return the token to the client + data = { 'token': token.key } + return Response(data, status=201) diff --git a/smugfolioserver/db.sql b/smugfolioserver/db.sql new file mode 100644 index 0000000..3502d83 --- /dev/null +++ b/smugfolioserver/db.sql @@ -0,0 +1,2 @@ +DELETE FROM 'auth_user' +WHERE id=2; \ No newline at end of file diff --git a/smugfolioserver/settings.py b/smugfolioserver/settings.py index ec033a2..453d1b6 100644 --- a/smugfolioserver/settings.py +++ b/smugfolioserver/settings.py @@ -59,7 +59,9 @@ REST_FRAMEWORK = { # THIS IS NEW CORS_ORIGIN_WHITELIST = ( 'http://localhost:3000', - 'http://127.0.0.1:3000' + 'http://127.0.0.1:3000', + 'http://127.0.0.1:8000', + 'http://localhost:8000' ) # UPDATE THIS diff --git a/smugfolioserver/urls.py b/smugfolioserver/urls.py index cd0b08b..dc3dd48 100644 --- a/smugfolioserver/urls.py +++ b/smugfolioserver/urls.py @@ -14,8 +14,19 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin +from django.conf.urls import include from django.urls import path +from smugfolioapi.views import register_user, login_user +from rest_framework import routers +from django.contrib import admin +from django.urls import path + +router = routers.DefaultRouter(trailing_slash=False) urlpatterns = [ path('admin/', admin.site.urls), + path('register', register_user), + path('login', login_user), + path('api-auth', include('rest_framework.urls', namespace='rest_framework')), + path('', include(router.urls)), ]