未经授权的用户Vuejs令牌

时间:2017-05-03 15:13:20

标签: laravel vuejs2 access-token laravel-5.4 vue-resource

我使用vuejs作为前端,使用Laravel 5.4作为后端。我在前端使用自定义授权,后端使用laravel护照。我面临一个非常不正常的行为..当我通过令牌验证用户并将该令牌保存在我的localStorage中。就在那之后,我开始另一种方法来获得认证用户。我正在使用授权令牌正确设置标头,但它表示未经授权的用户。当我刷新它发送相同的请求但现在它正确地让用户..我无法弄清楚可能是什么问题

这是我的main.js文件

    import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css'
import 'bootstrap/dist/css/bootstrap.min.css'
import Router from './routes.js'
import VueResource from 'vue-resource'
import { store } from './store';
import App from './App.vue'
import Auth from './packages/auth/Auth'
import _ from 'lodash'

Vue.use(Auth);
Vue.use(VueResource);
Vue.use(ElementUI);
Vue.use(_);

//Vue.http.options.root = 'http://backend.finlinkpro.com/public';
Vue.http.options.root = 'http://localhost/FinLinkWeb/BackEnd/public';
Vue.http.headers.common['Authorization'] = 'Bearer ' + Vue.auth.getToken();

这是发送请求的登录文件

   loginUser: function () {

            this.loginbtn = !this.loginbtn;
            var data = {
                client_id: '2',
                //yGeTHPRWObsjdQeYXMx4tUG84TUqUTyTvd9UnR03 Dev Site Client
                //jB0mH8Tf1SK2MTxXzjcBWiTg7iWgHcybr1nvyDBI Local Client
                client_secret: 'jB0mH8Tf1SK2MTxXzjcBWiTg7iWgHcybr1nvyDBI',
                grant_type: 'password',
                username: this.loginForm.email,
                password: this.loginForm.password,

            };
            let self = this;
            this.$http.post('oauth/token', data).then(function (response) {
                this.$auth.setToken(response.body.access_token, response.body.expires_in + Date.now());

                self.$http.post('api/user' , {
                    headers: {
                        Authorization: 'Bearer '+ response.body.access_token
                    }
                }).then(function(response){
                    console.log(response);
                });
                self.$router.push('/profile/first_login');
                self.loginbtn = !this.loginbtn;


            }).catch(function () {
                self.loginbtn = !this.loginbtn;
            });

        }

0 个答案:

没有答案