Skip to content

Commit 440eeb5

Browse files
committed
Created Serializer
1 parent 70d1adc commit 440eeb5

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

backend/api/serializer.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from rest_framework_simplejwt.tokens import Token
2+
from api.models import User, Profile
3+
4+
from django.contrib.auth.password_validation import validate_password
5+
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
6+
from rest_framework import serializers
7+
8+
9+
class UserSerializer(serializers.ModelSerializer):
10+
class Meta:
11+
model = User
12+
fields = ['id', 'username', 'email']
13+
14+
15+
class MyTokenObtainPairSerializer(TokenObtainPairSerializer):
16+
@classmethod
17+
def get_token(cls, user):
18+
token = super().get_token(user)
19+
20+
token['full_name'] = user.profile.full_name
21+
token['username'] = user.username
22+
token['email'] = user.email
23+
token['bio'] = user.profile.bio
24+
token['image'] = user.profile.image
25+
token['verified'] = user.profile.verified
26+
27+
return token
28+
29+
class RegisterSerializer(serializers.ModelSerializer):
30+
password = serializers.CharField(
31+
write_only=True, validators=[validate_password]
32+
)
33+
password2= serializers.CharField(
34+
write_only=True, validators=True
35+
)
36+
37+
class Meta:
38+
model = User
39+
fields = ['email', 'username', 'password2']
40+
41+
def validate(self, attrs):
42+
if attrs['password'] != attrs['password2']:
43+
raise serializers.ValidationError({
44+
"password": "Password Fields doesnt match"
45+
})
46+
47+
return attrs
48+
49+
def create(self, validated_data):
50+
user = User.objects.create(
51+
username=validated_data['username'],
52+
email=validated_data['email'],
53+
)
54+
55+
user.set_password(validated_data['password'])
56+
user.save()
57+
58+
return user
59+

backend/api/urls.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from rest_framework_simplejwt.tokens import Token
2+
3+
from django.urls import path
4+
5+
from . import views
Binary file not shown.

backend/backend/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150

151151
SIMPLE_JWT = {
152152
"ACCESS_TOKEN_LIFETIME": timedelta(minutes=5),
153-
"REFRESH_TOKEN_LIFETIME": timedelta(days=1),
153+
"REFRESH_TOKEN_LIFETIME": timedelta(days=50),
154154
"ROTATE_REFRESH_TOKENS": False,
155155
"BLACKLIST_AFTER_ROTATION": False,
156156
"UPDATE_LAST_LOGIN": False,

0 commit comments

Comments
 (0)