|
1 |
| -import random |
2 |
| -import string |
3 | 1 | from django.contrib.auth import authenticate
|
4 | 2 | from django.contrib.auth import get_user_model
|
5 | 3 | from django.conf import settings
|
|
10 | 8 | User = get_user_model()
|
11 | 9 |
|
12 | 10 |
|
13 |
| -def get_random_string(length): |
14 |
| - characters = string.ascii_letters + string.digits + string.punctuation |
15 |
| - return ''.join(random.choice(characters) for i in range(length)) |
16 |
| - |
17 |
| - |
18 | 11 | class AppwriteMiddleware(MiddlewareMixin):
|
19 | 12 | def __init__(self, get_response):
|
20 | 13 | self.get_response = get_response
|
@@ -61,25 +54,20 @@ def __call__(self, request, *args, **kwargs):
|
61 | 54 |
|
62 | 55 | # If the user information was retrieved successfully
|
63 | 56 | if user_info:
|
| 57 | + email = user_info['email'] |
| 58 | + password = settings.SECRET_KEY+user_id |
64 | 59 | # Get the Django user by its email
|
65 |
| - user = User.objects.filter(username=user_info['email']).first() |
66 |
| - |
67 |
| - # Generate a random password for the user |
68 |
| - password = get_random_string(16) |
| 60 | + user = User.objects.filter(username=email).first() |
69 | 61 |
|
70 | 62 | # If the user doesn't exist, create it
|
71 | 63 | if not user:
|
72 | 64 | user = User.objects.create_user(
|
73 |
| - username=user_info['email'], |
| 65 | + username=email, |
74 | 66 | password=password,
|
75 |
| - email=user_info['email']) |
76 |
| - |
77 |
| - # Set the user's password to the random password and save it |
78 |
| - user.set_password(password) |
79 |
| - user.save() |
| 67 | + email=email) |
80 | 68 |
|
81 | 69 | # Authenticate the user using the email as the username
|
82 |
| - user = authenticate(request, username=user_info['email'], password=password) |
| 70 | + user = authenticate(request, username=email, password=password) |
83 | 71 |
|
84 | 72 | # If the authentication was successful, log the user in
|
85 | 73 | if user:
|
|
0 commit comments