Javascript 如何使用vue auth的所有功能而不使用其登录功能
我是VueJS新手,正在尝试为我的网站构建授权功能。 首先,我尝试使用库名称来处理授权。它工作正常,下面是我的代码: Login.vueJavascript 如何使用vue auth的所有功能而不使用其登录功能,javascript,vue.js,vuejs2,vue-router,Javascript,Vue.js,Vuejs2,Vue Router,我是VueJS新手,正在尝试为我的网站构建授权功能。 首先,我尝试使用库名称来处理授权。它工作正常,下面是我的代码: Login.vue login () { var redirect = this.$auth.redirect() this.$auth.login({ headers: { 'Content-Type': 'application/json' }, data: this.data
login () {
var redirect = this.$auth.redirect()
this.$auth.login({
headers: {
'Content-Type': 'application/json'
},
data: this.data.body,
rememberMe: this.data.rememberMe,
redirect: {name: redirect ? redirect.from.name : 'Home'},
success (res) {
console.log('Auth Success')
},
error (err) {
console.log(err)
}
导航栏():
在app.js中:
Vue.use(VueAuth, {
auth: {
request: function (req, token) {
this.options.http._setHeaders.call(this, req, {Authorization: 'Bearer ' + token})
},
response: function (res) {
// Get Token from response body
return res.data
}
},
http: require('@websanova/vue-auth/drivers/http/axios.1.x.js'),
router: require('@websanova/vue-auth/drivers/router/vue-router.2.x.js'),
loginData: { url: 'http://localhost:6789/login', fetchUser: false },
refreshData: { enabled: false }
})
但现在我想自己编写一个服务来调用axios到API Url,不再使用$auth.login函数。我改变了主意
login () {
var self = this;
_AuthenticationService
.login(this.data.body)
.then(response => {
self.info = response;
console.log(response);
})
.catch(err => {
self.info = err;
});
我的服务:
import axiosconfigurator from '../axiosconfigurator'
class AuthenticationService {
login (request) {
var self = this
return new Promise((resolve, reject) => {
axios.post('https://reqres.in/api/login', {
username: 'Fred',
password: '123'
})
.then(function (response) {
// get token from this response
var token = response.data.token
self._setAuthToken(token, true)
console.log(token)
// var data = core.Parsers.UserParser.parse(response);
// history.update(data);
// deferred.resolve(history);
})
.catch(function (error) {
console.log(error)
reject(error)
});
})
}
所以我的问题是:我不想再使用vue auth库登录函数了,但我仍然想使用它的优点,比如$auth.ready函数,或者路由器中的auth属性和$auth.user。我怎样才能做到这一点呢?根据您提出问题的日期以及图书馆最近发生变化的事实 您可以通过以下选项调用vue auth对象上的登录方法
makeRequest:false
这里描述了一个解决方案
我测试了它,但它不起作用,所以我开了一张罚单
此方法是否会在浏览器的开发者控制台中设置一些值对不起,我不明白您的问题,即“此方法”。这两种方法都可以工作,并且可以从API获取数据。您在浏览器开发人员控制台(在应用程序/本地存储中)中拥有的数据。我只是想知道,因为我没有使用此方法登录,也就是说,我没有尝试第三方登录
import axiosconfigurator from '../axiosconfigurator'
class AuthenticationService {
login (request) {
var self = this
return new Promise((resolve, reject) => {
axios.post('https://reqres.in/api/login', {
username: 'Fred',
password: '123'
})
.then(function (response) {
// get token from this response
var token = response.data.token
self._setAuthToken(token, true)
console.log(token)
// var data = core.Parsers.UserParser.parse(response);
// history.update(data);
// deferred.resolve(history);
})
.catch(function (error) {
console.log(error)
reject(error)
});
})
}
makeRequest:false
this.$auth.watch.authenticated = true
this.$auth.watch.loaded = true
this.$user(response.user)
this.$router.push('/dashboard')