From c08135c65253d426653417c8342b30d0d5669886 Mon Sep 17 00:00:00 2001 From: Sajad Hashemian Date: Mon, 28 Feb 2022 11:33:51 +0330 Subject: [PATCH] fix: navigation guards for login and register --- cypress/integration/auth.test.js | 16 ++++++++++++++++ src/router.ts | 3 +++ 2 files changed, 19 insertions(+) diff --git a/cypress/integration/auth.test.js b/cypress/integration/auth.test.js index 818f4e9..b15d643 100644 --- a/cypress/integration/auth.test.js +++ b/cypress/integration/auth.test.js @@ -46,6 +46,14 @@ describe('Auth', () => { cy.wrap($el.checkValidity()).should('to.be', false) }) }) + + it('should not allow visiting login page when the user is logged in', () => { + cy.login() + + cy.visit('/#/login') + + cy.url().should('match', /\/#\/$/) + }) }) describe('Register', () => { @@ -81,5 +89,13 @@ describe('Auth', () => { cy.contains('email has already been taken') cy.contains('username has already been taken') }) + + it('should not allow visiting register page when the user is logged in', () => { + cy.login() + + cy.visit('/#/register') + + cy.url().should('match', /\/#\/$/) + }) }) }) diff --git a/src/router.ts b/src/router.ts index fb77c21..b2a809f 100644 --- a/src/router.ts +++ b/src/router.ts @@ -1,5 +1,6 @@ import { createRouter, createWebHashHistory, RouteParams } from 'vue-router' import Home from './pages/Home.vue' +import { isAuthorized } from './store/user' export type AppRouteNames = 'global-feed' | 'my-feed' @@ -50,11 +51,13 @@ export const router = createRouter({ name: 'login', path: '/login', component: () => import('./pages/Login.vue'), + beforeEnter: () => !isAuthorized.value, }, { name: 'register', path: '/register', component: () => import('./pages/Register.vue'), + beforeEnter: () => !isAuthorized.value, }, { name: 'profile',