我正在尝试在用户注册时自动登录用户,而不是重定向到登录页面然后仅登录。但是,出现错误
"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.'])
如果我直接登录,那么它可以工作,但是如果我要在注册用户时登录,那么它就不起作用,因此在注册用户时,我无法传递令牌。
注册时如何自动登录用户,以便我可以通过令牌?
答案 0 :(得分:1)
一个可能的原因是您正在将新创建的用户对象突变为NOT ACTIVE
。
在代码中,您可以看到正在将新创建的用户定义为非活动状态
serializer = RegistrationSerializer(data={
'email': email,
'password': password,
'is_active': False
})
is_active: False
表示用户名和密码可能有效,但是您的帐户已被禁用,这就是为什么您无法在注册期间登录。
如果还能看到您所依赖的JSONWebTokenSerializer
的源代码,则在validate函数中,它会检查用户是否处于非活动状态,然后抛出错误
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
,它将起作用。