Redzwan Latif
Redzwan Latif

Reputation: 886

Laravel pass Auth to vue SPA

I have a laravel + vue app. the authentication is managed fully using laravel Auth and blades. Now I'm building a vue application inside the system. This vue app is integrated with vue-router and vuex. Im using Api controller for any http request from my vue aplication.

app.js

Vue.use(VueRouter);

global.router = new VueRouter({
    base: window.location.pathname.replace(/^(\/c\/[^\/]+).*/i,'$1'),
    routes: routes
});

new Vue({
    el: '#spa',
    render: h => h(App),
    router,
    store: store,
});

Everything else is working good except on how to pass Auth data from laravel to vue. And how to auto log out the user if user is logged out from the main laravel system. Is there any solution or workaround to this?

Thanks

Upvotes: 2

Views: 1976

Answers (1)

jelhan
jelhan

Reputation: 6328

Assuming session information is stored in a cookie and vue app is served on same domain as laravel app exposing login and API, it's available in frontend via document.cookie API. Since cookies are included in every request per default, you don't have to care about sending it along with your AJAX requests.

If vue application is not served on the same domain, you have to use token based approaches as described in API Authentication chapter of Laravel docs. In that case you also have to care about CORS.

Upvotes: 2

Related Questions