link2329 link2330 link2331 link2332 link2333 link2334 link2335 link2336 link2337 link2338 link2339 link2340 link2341 link2342 link2343 link2344 link2345 link2346 link2347 link2348 link2349 link2350 link2351 link2352 link2353 link2354 link2355 link2356 link2357 link2358 link2359 link2360 link2361 link2362 link2363 link2364 link2365 link2366 link2367 link2368 link2369 link2370 link2371 link2372 link2373 link2374 link2375 link2376 link2377 link2378 link2379 link2380 link2381 link2382 link2383 link2384 link2385 link2386 link2387 link2388 link2389 link2390 link2391 link2392 link2393 link2394 link2395 link2396 link2397 link2398 link2399 link2400 link2401 link2402 link2403 link2404 link2405 link2406 link2407 link2408 link2409 link2410 link2411 link2412 link2413 link2414 link2415 link2416 link2417 link2418 link2419 link2420 link2421 link2422 link2423 link2424 link2425 link2426 link2427 link2428 link2429 link2430 link2431 link2432 link2433 link2434 link2435 link2436 link2437 link2438 link2439 link2440 link2441 link2442 link2443 link2444 link2445 link2446 link2447 link2448 link2449 link2450 link2451 link2452 link2453 link2454 link2455 link2456 link2457 link2458 link2459 link2460 link2461 link2462 link2463 link2464 link2465

[Vue.js] check login state, render an overlay if not logged in and continue if login succeeded

I use Vuejs with Vuetify and Vuex and want to create a simple app. Let’s say a very small todo app. For my backend I use an Express REST api and for the HTTP methods I use Axios.

To handle the session I read about two things:

When launching an Axios call I could get an error with the status code 401. I could deal with it using axios.interceptors.response.

instance.interceptors.response.use(res => res, (err) => {
if (err.response.status === 401) {
store.dispatch(‘authentication/destroySession’);
}

return err;
});

When redirecting to another route I could use the beforeRouteEnter event of the vue.js Router and check if the token is valid and not expired.

router.beforeEach((to, from, next) => {
const userIsLoggedIn = checkIfUserIsLoggedIn();

if (!userIsLoggedIn) {
next(false); // stay on this route
} else {
next(); // navigate to the next route
}
});

So far so good. But what happens if one of these approaches have to deal with a “not logged in” state? I don’t want to redirect the user to a login page because then he would loose his current work. And maybe he got no account so he needs to signup first.

If the route or REST endpoint is protected I would cancel the task to execute, show an error alert and after that I would like to render an overlay.

Most resources explain how to redirect to the login page if not logged in but Github for example renders the login page and executes the task after logging in.

How can I render this overlay? Is it even possible? Is there a better approach?

Solution :

Create the overlay HTML (can be in App.vue.js as you mentioned) whose visibility is dependent upon a computed called showOverlay:

<div v-show=”showOverlay”>OVERLAY CONTENT</div>

In the Vuex store, maintain some state which represents the visibility of the overlay:

state.showOverlay = false;

Back in App.vue, map this Vuex state into the showOverlay computed:

import { mapState } from ‘vuex’;
export default {
computed: {
…mapState([‘showOverlay’])
}
}

Wherever you use beforeRouteEnter, or in the router if using beforeEach, import the Vuex store directly (this makes it easy to access the store when this.$store is not accessible):

import store from ‘@/store.js’;

I see you posted example code which uses beforeEach instead of beforeRouteEnter, so use something like:

router.beforeEach((to, from, next) => {
const userIsLoggedIn = checkIfUserIsLoggedIn();

if (!userIsLoggedIn) {
store.state.showOverlay = true;
next(false); // stay on this route
} else {
next(); // navigate to the next route
}
});

After you get this working, it would be best to move the overlay into a component to avoid needlessly editing App.vue. And you’ll need to set the overlay’s state back to false on close.