我们要解决的问题
我使用Nuxt.js的@nuxtjs/auth'
模块进行登录验证,但当我使用loginWith
方法时,我会收到一个错误,因为它跳到了一个未指定的URL,而不是配置中设置的URL。我需要知道这是怎么发生的,为什么会发生。
配置中设置的URL
'/api/v1/auth/sign_in'
正在发送的实际URL
/api/auth/login
代码
配置
// Axios module configuration: https://go.nuxtjs.dev/config-axios
axios: {
baseURL: 'http:localhost:3000'
},
auth: {
redirect: {
login: '/user/login',
logout: '/user/login',
callback: false,
home: '/'
},
strategines: {
local: {
endpoints: {
login: { url: '/api/v1/auth/sign_in', method: 'post', propertyName: 'token' },
logout: { url: '/api/v1/auth/sign_out', method: 'delete' },
user: { url: '/api/v1/auth/user', method: 'get' }
}
}
}
},
组件
import logoImg from '~/assets/images/login_logo.png'
export default {
data () {
return {
logoImg,
userInfo: {
email: '',
password: ''
}
}
},
methods: {
async login () {
await this.$auth.loginWith('local', {
data: {
email: this.userInfo.email,
password: this.userInfo.password
}
})
.then(
(response) => {
localStorage.setItem('access-token', response.headers['access-token'])
localStorage.setItem('client', response.headers.client)
localStorage.setItem('uid', response.headers.uid)
localStorage.setItem('token-type', response.headers['token-type'])
return response
},
(error) => {
return error
}
)
}
}
}
错误
OST http://localhost:3000/api/auth/login 404 (Not Found)
您的配置中有拼写错误。
strategines
->strategies
http:localhost:3000
->http://localhost:3000