我试图在用户注册时自动登录用户,而不是重定向到登录页面,然后只登录。但是,我得到一个错误
"errors": [
"email",
"Unable to login with provided credentials."
],
这是我所做的:
def get_token(**user):
data = {}
if user.get('email') and user.get('password'):
serializer = JSONWebTokenSerializer(data=user)
if serializer.is_valid():
token = serializer.object['token']
user = serializer.object['user']
data = {
'user': user,
'token': token
}
return data
else:
data = {
'errors': serializer.errors
}
return data
data = {
'errors': 'Email or Password not provided'
}
return data
# creates the user but could not login
class Register(graphene.Mutation):
'''
Mutation to register a user
'''
class Arguments:
email = graphene.String(required=True)
password = graphene.String(required=True)
password_repeat = graphene.String(required=True)
success = graphene.Boolean()
token = graphene.String()
user = graphene.Field(UserQuery)
errors = graphene.List(graphene.String)
def mutate(self, info, email, password, password_repeat):
if password == password_repeat:
try:
serializer = RegistrationSerializer(data={
'email': email,
'password': password,
'is_active': False
})
if serializer.is_valid():
user = serializer.save()
user_identity = get_token(email=user.email, password=user.password)
if not user_identity.get('errors'):
return Register(success=True, user=user_identity.get('user'), token=user_identity.get('token'))
else:
return Register(success=False, token=None, errors=['email', 'Unable to login with provided credentials.'])
except Exception as e:
errors = [e]
return Register(success=False, errors=errors)
errors = ["password", "Passwords don't match."]
return Register(success=False, errors=errors)
# this works
class Login(graphene.Mutation):
"""
Mutation to login a user
"""
class Arguments:
email = graphene.String(required=True)
password = graphene.String(required=True)
success = graphene.Boolean()
errors = graphene.List(graphene.String)
token = graphene.String()
user = graphene.Field(UserQuery)
def mutate(self, info, email, password):
user_identity = get_token(email=email, password=password)
if not user_identity.get('errors'):
return Login(success=True, user=user_identity.get('user'), token=user_identity.get('token'))
else:
return Login(success=False, token=None, errors=['email', 'Unable to login with provided credentials.'])
如果我直接登录那么它可以工作但是如果我想在注册用户时登录那么它就不起作用所以我无法在注册用户时传递 token 。
如何在注册时自动登录用户以便传递 token ?
最佳答案
一个可能的原因是您将新创建的用户对象更改为 NOT ACTIVE
。
在您的代码中,您可以看到您将新创建的用户定义为非事件用户
serializer = RegistrationSerializer(data={
'email': email,
'password': password,
'is_active': False
})
is_active: False
表示用户名和密码可能有效,但您的帐户已被禁用,这就是您在注册期间无法登录的原因。
此外,如果可以看到您所依赖的 JSONWebTokenSerializer
的源代码,在验证函数中它会检查用户是否处于非事件状态,然后抛出错误
这是来自JSONWebTokenSerializer源代码
def validate(self, attrs):
credentials = {
self.username_field: attrs.get(self.username_field),
'password': attrs.get('password')
}
if all(credentials.values()):
user = authenticate(**credentials)
if user:
if not user.is_active:
msg = _('User account is disabled.')
raise serializers.ValidationError(msg)
payload = jwt_payload_handler(user)
return {
'token': jwt_encode_handler(payload),
'user': user
}
else:
msg = _('Unable to log in with provided credentials.')
raise serializers.ValidationError(msg)
else:
msg = _('Must include "{username_field}" and "password".')
msg = msg.format(username_field=self.username_field)
raise serializers.ValidationError(msg)
所以,我能看到的一种解决方案是将 is_active
标志设置为 true 或删除 is_active: False
,它会起作用。
关于python - 无法使用提供的凭据登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56607270/