imcvampire / vue-axios

A small wrapper for integrating axios to Vuejs
https://www.npmjs.com/package/vue-axios
MIT License
2.01k stars 183 forks source link

authorization headers not working #1

Closed kyoukhana closed 7 years ago

kyoukhana commented 7 years ago

If I set a common header it isn't working for some reason here is the code below.

this.axios.defaults.headers.common['Authorization'] = 'bearer:'+atoken;

Here is the update user method

   updateUser: function(user) {
                var _this=this;
                var atoken = this.$auth.token();

                this.axios.defaults.headers.common['Authorization'] = 'bearer:'+atoken;

                this.axios.post('api/users/updateprofile', user)
                    .then(function (response) {

                        var data=response['data'];

                        // Save Message Dialog
                        _this.$refs.updateMessage.open();

                    })
                    .catch(function (error) {
                        //console.log(error);
                    });

            }
imcvampire commented 7 years ago

Do you try to replace this with Vue. I'm using this function and everything is fine:

const setAuthorizationHeader = token => Vue.axios.defaults.headers.common.Authorization = token ? `Bearer ${token}` : ''
kyoukhana commented 7 years ago

here is how i have my project set up.

import Vue from 'vue'
import VueResource from 'vue-resource'
import VueRouter from 'vue-router'
import auth from '@websanova/vue-auth'
import * as App from './components/app.vue'
import mdl from 'material-design-lite'
import VueMdl from 'vue-mdl'
import VueX from 'vuex'
import axios from 'axios'
import VueAxios from 'vue-axios'
import vdropzone from 'vue-dropzone/lib/Dropzone'

Vue.use(VueRouter);
Vue.use(VueResource);
Vue.use(VueMdl);
Vue.use(VueX);
Vue.use(VueAxios, axios);

// register custom select
Vue.component('select-c', require('./components/custom/select.vue'));

/*Register Dropzone */
Vue.component('dropzone', require('./components/custom/dropzone.vue'))

Vue.router = new VueRouter; /*Must be declared before using vue auth */

/*vue-jwt-auth options */
var options = {
    token: [{request: 'token', response: 'token', authType: 'bearer', foundIn: 'header'}],
    tokenName:'token',
    loginData: {url: 'api/auth', method: 'POST'},
    logoutData: {url: 'api/logout', method: 'POST', redirect: '/admin/login', makeRequest: false},
    authRedirect: {path: '/admin/login'},
    fetchData: {url: 'api/account', method: 'GET' , authType: 'bearer'},
    rolesVar: 'role_id'
};

/*CRFC TOKEN For form validations for Laravel*/
Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('#token').getAttribute('content');

Vue.router.map({
    '/admin/login': {
        component: (resolve) => { require(['./components/views/admin/login.vue'], resolve); }
    }

});

Vue.http.options.root="http://www.devqi.com";

Vue.use(auth,options, { router: VueRouter, http: Vue.http });

//Vue.use(auth,options);
Vue.router.start(Vue.extend(App), '#app');
kyoukhana commented 7 years ago

I also tried it like this

 updateUser: function(user) {
                var _this=this;
                var atoken = this.$auth.token();

                this.axios.defaults.headers.common.Authorization = token ? `Bearer ${atoken}` : ''

                this.axios.post('api/users/updateprofile', user)
                    .then(function (response) {

                        var data=response['data'];

                        // Save Message Dialog
                        _this.$refs.updateMessage.open();

                    })
                    .catch(function (error) {
                        //console.log(error);
                    });

            },

This line fires

this.axios.post('api/users/updateprofile', user)

But doesn't pass then token header

imcvampire commented 7 years ago

Could you give a working demo ?

kyoukhana commented 7 years ago

So a working demo is that this code works. But it doesn't require a token to be passed

registerUser: function(user){

                this.axios.post('/api/usr/register', user)
                .then(function (response) {

                 var data=response['data'];

                    console.log(data);

                 _this.user.referralcode=data.referralid;

                // Save Message Dialog
                _this.$refs.registerMessage.open();

                })
                .catch(function (error) {
                //console.log(error);
                });

            }
imcvampire commented 7 years ago

No, what I want is a full project working code. Your code given to me is too confused.

kyoukhana commented 7 years ago

i can give you access to the repo hang on

kyoukhana commented 7 years ago

When you pull the project navigate to

/resources/assets/js/components/views/users/account/profile.vue

imcvampire commented 7 years ago

OK. Give me some second!

imcvampire commented 7 years ago

Sorry! But I can't find any code using my wrapper in your file. Could you give more detail?

kyoukhana commented 7 years ago

So if you navigate here /resources/assets/js/components/views/users/account/profile.vue line 149

resources/assets/js/app.js your libraries are included in there

kyoukhana commented 7 years ago

You might have to do a pull.

kyoukhana commented 7 years ago

I even tried it like this

 var axiosConfig = this.axios.create({
                    timeout: 1000,
                    headers: {'Authorization': 'bearer:'+this.$auth.token()}
                });

                this.axios.post('api/users/updateprofile', user, axiosConfig)
                    .then(function (response) {

                        var data=response['data'];

                        // Save Message Dialog
                        _this.$refs.updateMessage.open();

                    })
                    .catch(function (error) {
                        //console.log(error);
                    });
kyoukhana commented 7 years ago

in vue-resource it works here is the code for that

  this.$http.post('api/users/updateprofile', {Authorization: 'bearer:'+token}).then(response => response.json()).then((data) => {

                    let datar=data['data'];

                    _this.$refs.updateMessage.open();

                }).catch((response) => { console.error(response); });