Hi everyone, I hope you are fine.
I have a project using vue and django rest and I have done the authentication by djoser.
When a user logs in, a token would be set and the user can login to the website correctly but in views.py when I use self.request.user in get_queryset(self) function it says that the user is anonymous.
I will thank if anyone give me a piece of advice.
Thanks.
login axios code in vue js file:
login(){
axios.defaults.headers.common["Authorization"] = ""
localStorage.removeItem("token")
const formData = {
username: this.username,
password: this.password
}
axios.post("/api/v1/token/login/", formData)
.then(response => {
const token = response.data.auth_token
this.$store.commit('setToken', token)
axios.defaults.headers.common["Authorization"] = "Token " + token
localStorage.setItem("token", token)
})
.catch(error=>{
console.log(error.response.data)
})
}
And in the setting.py I have put:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES':(
'rest_framework.authentication.TokenAuthentication',
'rest_framework.authentication.SessionAuthentication',
)}
and two lines of my urls.py about djoser:
path('api/v1/', include('djoser.urls')),
path('api/v1/', include('djoser.urls.authtoken')),