jeudi 19 août 2021

How to get cookies (basically I need jwt token) in Ember.js which was set in Django?

Django Rest Framework

class LoginView(APIView):
        password = request.data['password']
        user = User.objects.filter(email=request.data['email']).first()

        if user is None:
            return Response({
                'success': False,
                'message': 'Username or password is invalid',
                'errors': 'Username or password is invalid',
            }, status=status.HTTP_401_UNAUTHORIZED)

        if not user.check_password(password):
            return Response({
                'success': False,
                'message': 'Username or password is invalid',
                'errors': {'username-or-password': ['Username or password is invalid']},
            }, status=status.HTTP_401_UNAUTHORIZED)

        payload = {
            'id': user.id,
            'iat': datetime.datetime.utcnow()
        }

        token = jwt.encode(payload, 'secret', algorithm='HS256')
        response = Response()
        response.set_cookie(key='token', value=token, httponly=True)

        response.data = {
            'success': True,
            'data': {
                'token': token
            }
        }
        return response

Here I send token in response data and also set token in cookies.

How can I get/receive/retrieve token from cookies in Ember.js?




Aucun commentaire:

Enregistrer un commentaire