I'm trying to find a solution for 3 days now and I didn't find anything related to my problem. I have a Nuxt.js frontend that uses the auth module to get a JWT token from a DRF backend.
After login, the server prints out this:
Forbidden (CSRF cookie not set.): /api/accounts/login/
[04/Jun/2021 10:36:44] "POST /api/accounts/login/ HTTP/1.1" 403 2870
In the network tab, I can see this in the request headers when I try to log in: X-CSRFToken: csrftoken
, which means that the csrf token is not passed properly. Right?
In my nuxt.config.js file, I have these settings:
axios: {
baseURL: 'http://localhost:8000',
headers: {
"X-CSRFToken": "csrftoken",
},
xsrfCookieName: "csrftoken",
xsrfHeaderName: "X-CSRFToken",
},
// authentication module configuration
auth: {
strategies: {
local: {
endpoints: {
login: {
url: '/api/accounts/login/',
method: 'post',
propertyName: 'access',
altProperty: 'refresh'
},
logout: {},
user: false
}
}
},
redirect: {
login: '/login',
},
},
router: {
middleware: ['auth']
},
...
}
Here is my login script:
export default {
data() {
return {
user: {
username: "",
password: "",
},
};
},
methods: {
async login() {
try {
await this.$auth.loginWith("local", {
data: this.user,
});
console.log("VALID");
} catch (err) {
console.log("INVALID");
}
},
},
};
How can I pass the csrf token properly to my Django backend?