@@ -35,34 +35,24 @@ def authenticate_by_token(callback_token):
35
35
return None
36
36
37
37
38
- def create_callback_token_for_user (user , alias_type , token_type ):
39
- token = None
38
+ def create_callback_token_for_user (user , alias_type , token_type , to_alias ):
40
39
alias_type_u = alias_type .upper ()
41
- to_alias_field = getattr (api_settings , f'PASSWORDLESS_USER_{ alias_type_u } _FIELD_NAME' )
42
- if user .pk in api_settings .PASSWORDLESS_DEMO_USERS .keys ():
43
- token = CallbackToken .objects .filter (user = user ).first ()
44
- if token :
45
- return token
46
- else :
47
- return CallbackToken .objects .create (
48
- user = user ,
49
- key = api_settings .PASSWORDLESS_DEMO_USERS [user .pk ],
50
- to_alias_type = alias_type_u ,
51
- to_alias = getattr (user , to_alias_field ),
52
- type = token_type
53
- )
54
-
55
- token = CallbackToken .objects .create (user = user ,
56
- to_alias_type = alias_type_u ,
57
- to_alias = getattr (user , to_alias_field ),
58
- type = token_type )
59
-
60
-
61
-
62
- if token is not None :
40
+ demo_key = api_settings .PASSWORDLESS_DEMO_USERS .get (user .pk ) or getattr (
41
+ api_settings , f"PASSWORDLESS_DEMO_USERS_{ alias_type_u } "
42
+ ).get (to_alias )
43
+ if demo_key :
44
+ token , _ = CallbackToken .objects .get_or_create (
45
+ user = user ,
46
+ key = demo_key ,
47
+ to_alias_type = alias_type_u ,
48
+ to_alias = to_alias ,
49
+ type = token_type )
63
50
return token
64
51
65
- return None
52
+ return CallbackToken .objects .create (user = user ,
53
+ to_alias_type = alias_type_u ,
54
+ to_alias = to_alias ,
55
+ type = token_type )
66
56
67
57
68
58
def validate_token_age (callback_token ):
0 commit comments