[英]How to extract JWT token from server response?
I'd like to receive JWT token fron express back into my vue.js app using axios. 我想使用axios将JWT令牌fron表达返回到我的vue.js应用程序中。
Here is what I came up with: 这是我想出的:
handleSubmit: function () {
axios.post( this.BASE_URL + "/users/login", {
username: this.username,
password: this.password,
}).then( (res) => {
console.log('res is:', res); //JWT found here
console.log('token is:', res.headers.common['Authorization']); //<-Problem here
this.$router.push({ path: '/' });
})
.catch( (error) => {
this.showErrorLogin = true;
if (error) {
this.error = error;
}
});
After a scussessful post, I receive this in the console : 经过一番专心致志的帖子,我在控制台中收到了此消息:
res is: {data: {…}, status: 200, statusText: "OK", headers: {…}, config: {…}, …}config: {adapter: ƒ, transformRequest: {…}, transformResponse: {…}, timeout: 0, xsrfCookieName: "XSRF-TOKEN", …}data: success: truetoken: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjViODZmOTE4YWNiMmI2Nzk3ZmEyYzFjNCIsInVzZXJuYW1lIjoiamFzZW0iLCJpYXQiOjE1MzYxNTgyNTAsImV4cCI6MTUzNjE5NDI1MH0.Sgl8cTHTQxU8rvBWdr365SBKABnUdbeUNHYLflqXURM"__proto__: Objectheaders: {content-type: "application/json; charset=utf-8"}request: XMLHttpRequest {onreadystatechange: ƒ, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}status: 200statusText: "OK"__proto__: Object
token is: undefined
but I don't know how to extract it. 但我不知道如何提取它。 I've also tried
res.headers['Authorization']
but get undefined
instead. 我也尝试过
res.headers['Authorization']
但是取而代之的是undefined
。
The token can be passed in many ways 令牌可以通过多种方式传递
// check header or url parameters or post parameters for token
const token = req.body.token || req.query.token || req.headers['x-access-token'] || req.headers['Authorization'] || req.headers['Bearer'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.