Hamza
Hamza

Reputation: 71

Token Authentication In Django Restframework Using Django-rest-auth

I'm using vue.js for the front end of my application which is made on Django-restframework. I am using django-rest-auth for social authentication via Google.

On the front end, I am using the vue-google-oauth2 library. The two work fine. I send an auth code from the front end and the backend responds with a token key. However, when I use curl on my terminal with the key

curl -X GET http://127.0.0.1:8000/api/example/ -H 'Authorization: Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b'

I get the error:

{"detail":"Authentication credentials were not provided."}

With my terminal showing:

[06/Mar/2019 15:38:54] "GET /newsfeed/ HTTP/1.1" 401 58

What is it that I am supposed to do to ensure that the cookies or whatever it is at the backend to maintain my session isn't lost?

Similarly, when I login on the front end using the oauth2 library, I refresh my page and that too shows that I am no longer logged in.

What exactly am I missing out on?

Upvotes: 1

Views: 460

Answers (1)

biswa1991
biswa1991

Reputation: 546

looks like 1st authentication class is set to base authentication. make sure you activate tokenbase authentication for that view as 1st authentication class. or you can set authentication class globally in setting.py.

if you are using class base view . do it like this

class AccountDetailsView(RetrieveAPIView):
    authentication_classes = [TokenAuthentication,BasicAuthentication]
    serializer_class = AccountDetails
    permission_classes = [IsAuthenticated, ]

Upvotes: 1

Related Questions