mirror of
https://github.com/andwati/store-api.git
synced 2026-10-26 21:12:16 +00:00
completed authentication serializers
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class AuthenticationConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "authentication"
|
||||
@@ -0,0 +1,26 @@
|
||||
import jwt
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import User
|
||||
from rest_framework import authentication, exceptions
|
||||
|
||||
|
||||
class JWTAuthentication(authentication.BaseAuthentication):
|
||||
def authenticate(self, request):
|
||||
|
||||
auth_data = authentication.get_authorization_header(request)
|
||||
|
||||
if not auth_data:
|
||||
return None
|
||||
|
||||
prefix, token = auth_data.decode("utf-8").split(" ")
|
||||
|
||||
try:
|
||||
payload = jwt.decode(token, settings.JWT_SECRET_KEY, algorithms="HS256")
|
||||
user = User.objects.get(username=payload["username"])
|
||||
return (user, token)
|
||||
except jwt.DecodeError as err:
|
||||
raise exceptions.AuthenticationFailed("Your token is invalid, please login")
|
||||
except jwt.ExpiredSignatureError as err:
|
||||
raise exceptions.AuthenticationFailed("Your token is expired, login")
|
||||
|
||||
return super().authenticate(request)
|
||||
@@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
@@ -0,0 +1,32 @@
|
||||
from django.contrib.auth.models import User
|
||||
from rest_framework import serializers
|
||||
|
||||
|
||||
class UserSerializer(serializers.ModelSerializer):
|
||||
password = serializers.CharField(max_length=65, min_length=8, write_only=True)
|
||||
email = serializers.EmailField(max_length=255)
|
||||
first_name = serializers.CharField(max_length=255)
|
||||
last_name = serializers.CharField(max_length=255)
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ["username", "first_name", "last_name", "email", "password"]
|
||||
|
||||
def validate(self, attrs):
|
||||
|
||||
email = attrs.get("email", "")
|
||||
if User.objects.filter(email=email).exists():
|
||||
raise serializers.ValidationError({"error": "Email is already in use"})
|
||||
return super().validate(attrs)
|
||||
|
||||
def create(self, validated_data):
|
||||
return User.objects.create_user(**validated_data)
|
||||
|
||||
|
||||
class LoginSerializer(serializers.ModelSerializer):
|
||||
password = serializers.CharField(max_length=65)
|
||||
username = serializers.CharField(max_length=255)
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ["username", "password"]
|
||||
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
@@ -0,0 +1,3 @@
|
||||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
||||
Reference in New Issue
Block a user