python - get() returned more than one AccessToken -
i facing 2 issues here.
1) if user has not old token, new 1 should generated , logged in not happening so.
2)a newly created user when logs in gets 'you have multiple authentication backends configured , therefore must provide backend
argument or set backend
attribute on user.'
3)old user when trying login gets error of get() returned more 1 accesstoken -- returned 8!
expert might understand code
class userloginapi(apiview): permission_classes = [allowany] serializer_class = userloginserializer def post(self, request, *args, **kwargs): access_token = request.get.get('access_token') data = request.data print('data',data) serializer = userloginserializer(data=data) if serializer.is_valid(raise_exception=true): new_data = serializer.data if new_data: app = application.objects.get(name="foodie") try: user = user.objects.get(username=data['username']) print ('user',user) except objectdoesnotexist: return httpresponse("can't find user") else: try: access_token = accesstoken.objects.get(user=user) except objectdoesnotexist: return httpresponse('have not set token') else: access_token.delete() new_token = generate_token() print('new_token',new_token) accesstoken.objects.create(user=user, application=app, expires=datetime.now() + timedelta(days=365),token=new_token) print('aceess',accesstoken) login(request, user) return response(new_data, status=status.http_200_ok) return response(serializer.errors, status=status.http_400_bad_request)
serializers.py
class userloginserializer(modelserializer): username = charfield() class meta: model = user fields = ['username', 'password'] class usercreateserializer(modelserializer): class meta: model = user extra_kwargs = {"password": {"write_only": true}} def create(self, validated_data): user_obj = user( username = username, first_name = first_name, last_name = last_name, email = email ) user_obj.set_password(password) user_obj.save() if user_obj: expire_seconds = oauth2_settings.user_settings['access_token_expire_seconds'] scopes = oauth2_settings.user_settings['scopes'] application = application.objects.get(name="foodie") expires = datetime.now() + timedelta(seconds=expire_seconds) access_token = accesstoken.objects.create(user=user_obj, application=application, token = generate_token(), expires=expires, scope=scopes) return validated_data
Comments
Post a Comment