14
14
15
15
User = get_user_model ()
16
16
17
- class AuthView (APIView ):
17
+ class AuthAPIView (APIView ):
18
18
permission_classes = [permissions .AllowAny ]
19
19
def post (self , request , * args , ** kwargs ):
20
20
#print(request.user)
@@ -23,7 +23,6 @@ def post(self, request, *args, **kwargs):
23
23
data = request .data
24
24
username = data .get ('username' ) # username or email address
25
25
password = data .get ('password' )
26
- user = authenticate (username = username , password = password )
27
26
qs = User .objects .filter (
28
27
Q (username__iexact = username )|
29
28
Q (email__iexact = username )
@@ -39,3 +38,36 @@ def post(self, request, *args, **kwargs):
39
38
return Response ({"detail" : "Invalid credentials" }, status = 401 )
40
39
41
40
41
+
42
+ class RegisterAPIView (APIView ):
43
+ permission_classes = [permissions .AllowAny ]
44
+ def post (self , request , * args , ** kwargs ):
45
+ if request .user .is_authenticated ():
46
+ return Response ({'detail' : 'You are already registered and are authenticated.' }, status = 400 )
47
+ data = request .data
48
+ username = data .get ('username' ) # username or email address
49
+ email = data .get ('username' )
50
+ password = data .get ('password' )
51
+ password2 = data .get ('password2' )
52
+ qs = User .objects .filter (
53
+ Q (username__iexact = username )|
54
+ Q (email__iexact = username )
55
+ )
56
+ if password != password2 :
57
+ return Response ({"password" : "Password must match." }, status = 401 )
58
+ if qs .exists ():
59
+ return Response ({"detail" : "This user already exists" }, status = 401 )
60
+ else :
61
+ user = User .objects .create (username = username , email = email )
62
+ user .set_password (password )
63
+ user .save ()
64
+ # payload = jwt_payload_handler(user)
65
+ # token = jwt_encode_handler(payload)
66
+ # response = jwt_response_payload_handler(token, user, request=request)
67
+ # return Response(response, status=201)
68
+ return Response ({'detail' : "Thank you for registering. Please verify your email." }, status = 201 )
69
+ return Response ({"detail" : "Invalid Request" }, status = 400 )
70
+
71
+
72
+
73
+
0 commit comments