This repository has been archived on 2026-10-10. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
smugfolio-server/smugfolioapi/views/auth.py
T
2022-03-23 17:21:44 -05:00

70 lines
2.3 KiB
Python

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)
smug_user= Smug_Users.objects.get(user=authenticated_user)
data = {
'valid': True,
'token': token.key,
'smug_user_id': smug_user.id
}
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(
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, 'id': smug_users.id }
return Response(data, status=201)