in Education by
I need to create JWT token authentication, but I don't know how, could you explain me how to do it better or put some examples? my view: class UserLogin(generics.CreateAPIView): """ POST auth/login/ """ # This permission class will overide the global permission # class setting permission_classes = (permissions.AllowAny,) queryset = User.objects.all() serializer_class = TokenSerializer def post(self, request, *args, **kwargs): username = request.data.get("username", "") password = request.data.get("password", "") user = auth.authenticate(request, username=username, password=password) if user is not None: auth.login(request, user) return Response({ "token": jwt_encode_handler(jwt_payload_handler(user)), 'username': username, }, status=200) return Response(status=status.HTTP_401_UNAUTHORIZED) JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You are creating the token in that view. After that, you need two other mechanism in place: Your client should send this token the the API with each request, in the Authorization header, like: Authorization: Bearer your_token On the api side, you need to use an authentication class, that looks for Authorization header, takes the token and decodes it, and finds the user instance associated with the token, if the token is valid. If you are using a library for drf jwt authentication, it should have an authentication class that you can use. If you are implementing it manually, you need to write an authentication class that subclasses DRF's BaseAuthentication class yourself. It could basically look like this: class JwtAuthentication(authentication.BaseAuthentication): def authenticate(self, request): auth_header = request.META.get('HTTP_AUTHORIZATION') if auth_header: key, token = auth_header.split(' ') if key == 'Bearer': # Decode the token here. If it is valid, get the user instance associated with it and return it ... return user, None # If token exists but it is invalid, raise AuthenticationFailed exception # If token does not exist, return None so that another authentication class can handle authentication You need to tell DRF to use this authentication class. Add this to your settings file for that: REST_FRAMEWORK = { ... 'DEFAULT_AUTHENTICATION_CLASSES': [ 'path.to.JwtAuthentication', ... ] }

Related questions

0 votes
    I am learning about OAuth2 and OpenID Connect by experimenting with ASP.NET Core and IdentityServer4. So far ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 21, 2022 in Education by JackTerrance
0 votes
    I have a situation where i have a list of primary keys. And i need to filter(OR) a queryset ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    I have a situation where i have a list of primary keys. And i need to filter(OR) a queryset ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    I have a situation where i have a list of primary keys. And i need to filter(OR) a queryset ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    Issue Recently I came across an issue with a client, that has many solutions, but none was easy. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I have the following models: class Project(models.Model): name = models.CharField(max_length=300, unique= ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 22, 2022 in Education by JackTerrance
0 votes
    So in my project, I have a model Chat with many Messages. I want to have ChatSerializer with ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    I have a very long forms.py and I'd like to split it to smaller parts with as few as possible ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    I'm trying to write custom jwt login view for django rest api authentication. But it doesn't work, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    Symfony version: 3.1.3 Due to development reason suddenly my app giving the following error and I believe ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    Suppose, my license expires today. Can my users be able to view the dashboards or workbooks that I published in the server earlier?...
asked Oct 30, 2020 in Technology by JackTerrance
0 votes
    How do I support redundancy on my OpenID login website? For instance, I have users that demand 100% uptime ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    How do I support redundancy on my OpenID login website? For instance, I have users that demand 100% uptime ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    I’ve done my btech in information Technology. How can I get a job in cyber security field? Select the correct answer from above options...
asked Dec 23, 2021 in Education by JackTerrance
0 votes
    How to get time in hours and the hour difference between login and logout times? I used the below ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 12, 2022 in Education by JackTerrance
...