[英]DRF - Authentication credentials were not provided. (Djoser + SimpleJWT)
I am currently getting an error when making a request to /users/me
to receive back my user's data.我目前在向
/users/me
发出请求以接收我的用户数据时遇到错误。 From what I have been reading, I am not sending the token, though I'm not sure how to store it when I receive it from the jwt/create
endpoint when signing in.从我一直在阅读的内容来看,我没有发送令牌,尽管我不确定在登录时从
jwt/create
端点收到它时如何存储它。
This is from my Auth-Test/nuxt-auth/pages/index.vue
file:这是来自我的
Auth-Test/nuxt-auth/pages/index.vue
文件:
onMounted(async () => {
const cookie = useCookie('jwt');
console.log('COOKIE: ' + JSON.stringify(cookie));
const response = await fetch('http://localhost:8000/api/auth/users/me/', {
headers: {
'Content-Type': 'application/json',
'Authorization': `JWT ${JSON.stringify(cookie)}`,
},
credentials: 'include'
})
const content = await response.json();
console.log(content);
})
and this is from my Auth-Test/nuxt-auth/pages/login.vue
这是来自我的
Auth-Test/nuxt-auth/pages/login.vue
const router = useRouter();
async function submit() {
console.log(JSON.stringify({
email: user.email,
password: user.password
}))
await fetch('http://localhost:8000/api/auth/jwt/create/', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
credentials: 'include',
body: JSON.stringify({
email: user.email,
password: user.password
})
});
await router.push({ path: '/' });
}
Could anyone help me realize what I might be (am) doing wrong?谁能帮助我意识到我可能做错了什么? I can't seem to figure out it myself through the use of documentation after a lot of reading.
经过大量阅读后,我似乎无法通过使用文档自己弄明白。
In case you might need to access the other files (front and back end), here is the Github repo .如果您可能需要访问其他文件(前端和后端),这里是Github 存储库。
Follow the usage guide here https://django-rest-framework-simplejwt.readthedocs.io/en/latest/getting_started.html#usage按照这里的使用指南https://django-rest-framework-simplejwt.readthedocs.io/en/latest/getting_started.html#usage
You should store the JWT token instead of using cookie.您应该存储 JWT 令牌而不是使用 cookie。
const router = useRouter();
async function submit() {
console.log(JSON.stringify({
email: user.email,
password: user.password
}))
const response = await fetch('http://localhost:8000/api/auth/jwt/create/', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
credentials: 'include',
body: JSON.stringify({
email: user.email,
password: user.password
})
});
// it's common to use localStorage to store it so when users go back to your site it's still there.
// If you don't want that you can just store it in memory.
const responseJson = await response.json();
localStorage.setItem('token', responseJson.access);
await router.push({ path: '/' });
}
Then you can use it as a Bearer
token然后您可以将其用作
Bearer
令牌
Bearer authentication (also called token authentication) is an HTTP authentication scheme that involves security tokens called bearer tokens.
不记名身份验证(也称为令牌身份验证)是一种 HTTP 身份验证方案,涉及称为不记名令牌的安全令牌。 The name “Bearer authentication” can be understood as “give access to the bearer of this token.”
“Bearer authentication”这个名字可以理解为“授予对这个token的bearer的访问权”。 The bearer token is a cryptic string, usually generated by the server in response to a login request.
不记名令牌是一个神秘的字符串,通常由服务器生成以响应登录请求。 The client must send this token in the Authorization header when making requests to protected resources:
向受保护资源发出请求时,客户端必须在授权 header 中发送此令牌:
Authorization: Bearer 'token'
授权:不记名“令牌”
onMounted(async () => {
const cookie = useCookie('jwt');
console.log('COOKIE: ' + JSON.stringify(cookie));
const token = localStorage.getItem('token');
const response = await fetch('http://localhost:8000/api/auth/users/me/', {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
},
credentials: 'include'
})
const content = await response.json();
console.log(content);
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.