Fix login issue
This commit is contained in:
36
rog/management/commands/check_user_auth.py
Normal file
36
rog/management/commands/check_user_auth.py
Normal file
@ -0,0 +1,36 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from rog.models import CustomUser
|
||||
from django.contrib.auth import authenticate
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Check user authentication'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('email', type=str, help='User email to check')
|
||||
parser.add_argument('--password', type=str, help='Password to test', default='')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
email = options['email']
|
||||
password = options.get('password', '')
|
||||
|
||||
self.stdout.write(f'Checking user: {email}')
|
||||
|
||||
try:
|
||||
user = CustomUser.objects.get(email=email)
|
||||
self.stdout.write(f'✅ User found: {user.email}')
|
||||
self.stdout.write(f' Active: {user.is_active}')
|
||||
self.stdout.write(f' Has password: {bool(user.password)}')
|
||||
self.stdout.write(f' Password hash start: {user.password[:30]}...')
|
||||
|
||||
if password:
|
||||
self.stdout.write(f'\n🔐 Testing authentication with provided password...')
|
||||
auth_user = authenticate(username=email, password=password)
|
||||
if auth_user:
|
||||
self.stdout.write('✅ Authentication successful')
|
||||
else:
|
||||
self.stdout.write('❌ Authentication failed')
|
||||
|
||||
except CustomUser.DoesNotExist:
|
||||
self.stdout.write(f'❌ User does not exist: {email}')
|
||||
except Exception as e:
|
||||
self.stdout.write(f'❌ Error: {e}')
|
||||
@ -535,9 +535,10 @@ class LoginUserSerializer(serializers.Serializer):
|
||||
else:
|
||||
# Check if the user exists
|
||||
try:
|
||||
user_obj = User.objects.get(email=email)
|
||||
from .models import CustomUser
|
||||
user_obj = CustomUser.objects.get(email=email)
|
||||
raise serializers.ValidationError("Incorrect password.")
|
||||
except User.DoesNotExist:
|
||||
except CustomUser.DoesNotExist:
|
||||
raise serializers.ValidationError("User with this email does not exist.")
|
||||
else:
|
||||
raise serializers.ValidationError("Must include 'email' and 'password'.")
|
||||
|
||||
@ -908,12 +908,15 @@ class LoginAPI(generics.GenericAPIView):
|
||||
def post(self, request, *args, **kwargs):
|
||||
logger.info(f"Login attempt for identifier: {request.data.get('identifier', 'identifier not provided')}")
|
||||
logger.debug(f"Request data: {request.data}")
|
||||
logger.debug(f"Request headers: {dict(request.headers)}")
|
||||
|
||||
# フロントエンドの 'identifier' フィールドを 'email' にマッピング
|
||||
data = request.data.copy()
|
||||
if 'identifier' in data and 'email' not in data:
|
||||
data['email'] = data['identifier']
|
||||
|
||||
logger.debug(f"Processed data: {data}")
|
||||
|
||||
serializer = self.get_serializer(data=data)
|
||||
try:
|
||||
serializer.is_valid(raise_exception=True)
|
||||
|
||||
Reference in New Issue
Block a user