All vue-router middleware can also be referred to as navigation guards, and they can be attached to the application route changes. Those changes have some hooks that you can apply to your middleware. The authentication middleware takes place before the router changes, so we can handle everything and send the user to the correct route.
- In the src/router folder, create a new folder called middleware, then create and open a new file called authentication.js.
- In this file, we will create a default export function that will have three function parameters – to, from, and next. The to and from parameters are objects, and the next parameter is a callback function:
export default (to, from, next) => {
};
- We need to check whether the route that we are being redirected to has an authenticated meta property set to true and whether we have a sessionStorage item with the 'auth' key. If we pass those validations, we can execute the next callback:
if...