Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set header after login? #8

Open
cannap opened this issue Jun 21, 2016 · 2 comments
Open

Set header after login? #8

cannap opened this issue Jun 21, 2016 · 2 comments

Comments

@cannap
Copy link

cannap commented Jun 21, 2016

I have a question what is the best way to set the Header in
auth.login() {}?

current solution:

import Vue from 'vue';
....

   login(context, creds, redirect) {
        context.$http.post(LOGIN_URL, creds).then((data) => {

            localStorage.setItem('id_token', data.data.id_token)
            Vue.http.headers.common['Authorization'] = 'Bearer ' + localStorage.getItem('id_token');

            this.user.authenticated = true
            if (redirect) {
                router.go(redirect)
            }
        }).catch(({data}) => {
            context.error = data
        })


    },

i solve in a other way i switch to VUEX thank for the tutorial

@chenkie
Copy link
Contributor

chenkie commented Jun 24, 2016

I'm a bit unclear on what you'd like to achieve. Are you wanting to send a header on the POST request that you have in your login method? Or are you wanting to set up a global Authorization header with the user's JWT if login is successful?

@cannap
Copy link
Author

cannap commented Jun 24, 2016

Hi i solved with interceptors

Vue.http.interceptors.push((request, next) => {
    const token = getLoginToken();
    if (token) {
        request.headers['Authorization'] = 'Bearer ' + token
    }

    next((response) => {
        if (response.status === 401 && response.request.url !== "/session/create") {
            //Todo: no hardcoded routes
            window.location.pathname = "/admin/login"
        }
    });
});

The Tutorials show:

//index.js
Vue.http.headers.common['Authorization'] = auth.getAuthHeader();

but this will first work after a refresh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants