@@ -19,50 +19,73 @@ class LoginSerializer(serializers.Serializer):
19
19
email = serializers .EmailField (required = False , allow_blank = True )
20
20
password = serializers .CharField (style = {'input_type' : 'password' })
21
21
22
+ def _validate_email (self , email , password ):
23
+ user = None
24
+
25
+ if email and password :
26
+ user = authenticate (email = email , password = password )
27
+ else :
28
+ msg = _ ('Must include "email" and "password".' )
29
+ raise exceptions .ValidationError (msg )
30
+
31
+ return user
32
+
33
+ def _validate_username (self , username , password ):
34
+ user = None
35
+
36
+ if username and password :
37
+ user = authenticate (username = username , password = password )
38
+ else :
39
+ msg = _ ('Must include "username" and "password".' )
40
+ raise exceptions .ValidationError (msg )
41
+
42
+ return user
43
+
44
+ def _validate_username_email (self , username , email , password ):
45
+ user = None
46
+
47
+ if email and password :
48
+ user = authenticate (email = email , password = password )
49
+ elif username and password :
50
+ user = authenticate (username = username , password = password )
51
+ else :
52
+ msg = _ ('Must include either "username" or "email" and "password".' )
53
+ raise exceptions .ValidationError (msg )
54
+
55
+ return user
56
+
22
57
def validate (self , attrs ):
23
58
username = attrs .get ('username' )
24
59
email = attrs .get ('email' )
25
60
password = attrs .get ('password' )
26
61
62
+ user = None
63
+
27
64
if 'allauth' in settings .INSTALLED_APPS :
28
65
from allauth .account import app_settings
66
+
29
67
# Authentication through email
30
68
if app_settings .AUTHENTICATION_METHOD == app_settings .AuthenticationMethod .EMAIL :
31
- if email and password :
32
- user = authenticate (email = email , password = password )
33
- else :
34
- msg = _ ('Must include "email" and "password".' )
35
- raise exceptions .ValidationError (msg )
69
+ user = self ._validate_email (email , password )
70
+
36
71
# Authentication through username
37
- elif app_settings .AUTHENTICATION_METHOD == app_settings .AuthenticationMethod .USERNAME :
38
- if username and password :
39
- user = authenticate (username = username , password = password )
40
- else :
41
- msg = _ ('Must include "username" and "password".' )
42
- raise exceptions .ValidationError (msg )
72
+ if app_settings .AUTHENTICATION_METHOD == app_settings .AuthenticationMethod .USERNAME :
73
+ user = self ._validate_username (username , password )
74
+
43
75
# Authentication through either username or email
44
76
else :
45
- if email and password :
46
- user = authenticate (email = email , password = password )
47
- elif username and password :
48
- user = authenticate (username = username , password = password )
49
- else :
50
- msg = _ ('Must include either "username" or "email" and "password".' )
51
- raise exceptions .ValidationError (msg )
52
-
53
- elif username or email and password :
54
- # Try get username if we have in request email
55
- if email and not username :
77
+ user = self ._validate_username_email (username , email , password )
78
+
79
+ else :
80
+ # Authentication without using allauth
81
+ if email :
56
82
try :
57
83
username = UserModel .objects .get (email__iexact = email ).username
58
84
except UserModel .DoesNotExist :
59
- user = None
60
- if username :
61
- user = authenticate (username = username , password = password )
85
+ pass
62
86
63
- else :
64
- msg = _ ('Must include either "username" or "email" and "password".' )
65
- raise exceptions .ValidationError (msg )
87
+ if username :
88
+ user = self ._validate_username_email (username , '' , password )
66
89
67
90
# Did we get back an active user?
68
91
if user :
0 commit comments