|
1 |
| -from django.contrib.auth import authenticate, login |
| 1 | +import random |
| 2 | +import string |
| 3 | +from django.contrib.auth import authenticate |
2 | 4 | from django.contrib.auth import get_user_model
|
3 | 5 | from django.conf import settings
|
4 | 6 | from appwrite.client import Client
|
|
8 | 10 | User = get_user_model()
|
9 | 11 |
|
10 | 12 |
|
| 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 | + |
11 | 18 | class AppwriteMiddleware(MiddlewareMixin):
|
12 | 19 | def __init__(self, get_response):
|
13 | 20 | self.get_response = get_response
|
@@ -57,12 +64,22 @@ def __call__(self, request, *args, **kwargs):
|
57 | 64 | # Get the Django user by its email
|
58 | 65 | user = User.objects.filter(username=user_info['email']).first()
|
59 | 66 |
|
| 67 | + # Generate a random password for the user |
| 68 | + password = get_random_string(16) |
| 69 | + |
60 | 70 | # If the user doesn't exist, create it
|
61 | 71 | if not user:
|
62 |
| - user = User.objects.create_user(username=user_info['email'], password=user_info['password'], email=user_info['email']) |
| 72 | + user = User.objects.create_user( |
| 73 | + username=user_info['email'], |
| 74 | + 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() |
63 | 80 |
|
64 | 81 | # Authenticate the user using the email as the username
|
65 |
| - user = authenticate(request, username=user_info['email'], password=user_info['password']) |
| 82 | + user = authenticate(request, username=user_info['email'], password=password) |
66 | 83 |
|
67 | 84 | # If the authentication was successful, log the user in
|
68 | 85 | if user:
|
|
0 commit comments