peter
peter

Reputation: 1572

Django rest-auth Token authentication

I'm trying to use axios to get information from the /rest-auth/user/ page. This is my function:

export const fetchUser = () => {
  const token = localStorage.getItem('token');
  return dispatch => {
    dispatch(fetchUserPending());
    axios.get('http://localhost:8000/api/v1/rest-auth/user/', {headers: { 'authorization': `Bearer ${token}`}})
      .then(response => {
        const user = response.data;
        dispatch(fetchUserFulfilled(user));
      })
      .catch(err => {
        dispatch(fetchUserRejected(err));
      })
  }
}

It uses the django token I get from login, that is stored in localStorage. I get error status 403, authentication credentials were not provided. I've tried editing my django settings.py file to include

REST_FRAMEWORK = {
   'DEFAULT_AUTHENTICATION_CLASSES': [
       'rest_framework.authentication.TokenAuthentication',
   ],
} 

and then I get error code 401 unauthorized. Can anyone direct me in the right direction? Thanks!

Upvotes: 3

Views: 1614

Answers (1)

grouchoboy
grouchoboy

Reputation: 1024

By default rest_framework.authentication.TokenAuthentication uses the keyword Token instead of Bearer. The call should be:

axios.get('http://localhost:8000/api/v1/rest-auth/user/', {headers: { 'Authorization': `Token ${token}`}})

Upvotes: 5

Related Questions