diff --git a/cypress/integration/auth.test.js b/cypress/integration/auth.test.js index 818f4e91..b15d6437 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/pages/Login.vue b/src/pages/Login.vue index 095f4254..1ea4acae 100644 --- a/src/pages/Login.vue +++ b/src/pages/Login.vue @@ -75,6 +75,8 @@ const form = reactive({ const errors = ref({}) const login = async () => { + errors.value = {} + if (!formRef.value?.checkValidity()) return const result = await postLogin(form) diff --git a/src/pages/Register.vue b/src/pages/Register.vue index 8ca10a18..a7a3f0c5 100644 --- a/src/pages/Register.vue +++ b/src/pages/Register.vue @@ -83,6 +83,8 @@ const form = reactive({ const errors = ref({}) const register = async () => { + errors.value = {} + if (!formRef.value?.checkValidity()) return const result = await postRegister(form) diff --git a/src/router.ts b/src/router.ts index fb77c21a..b2a809f2 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', diff --git a/src/utils/map-checkable-response.spec.ts b/src/utils/map-checkable-response.spec.ts index 9dae9195..f2b9bc3e 100644 --- a/src/utils/map-checkable-response.spec.ts +++ b/src/utils/map-checkable-response.spec.ts @@ -1,3 +1,4 @@ +import wrapTests from 'src/utils/test/wrap-tests' import { ValidationError, AuthorizationError, NetworkError } from 'src/types/error' import { Either, fail, isEither, success } from './either' import { mapAuthorizationResponse, mapValidationResponse } from './map-checkable-response' @@ -53,19 +54,24 @@ describe('# mapValidationResponse', function () { expect(result.value).toEqual(RESPONSE) }) - it('should return Either with ValidationError and failed Response', async function () { - const RESPONSE = { ok: false, status: 422, json: () => Promise.resolve({ errors: { foo: 'bar' } }) } - const response = createCheckableResponse(RESPONSE) - - const result = mapValidationResponse>(response) - - expect(isEither(result)).toBe(true) - expect(result.isFail()).toBe(true) - expect(result.value).toBeInstanceOf(ValidationError) - expect(result.isFail() && await result.value.getErrors()).toEqual((await RESPONSE.json()).errors) + wrapTests({ + task: 'should return Either with ValidationError and failed Response', + list: [422, 403], + testName: (status) => `status code ${status}`, + fn: async (status) => { + const RESPONSE = { ok: false, status, json: () => Promise.resolve({ errors: { foo: 'bar' } }) } + const response = createCheckableResponse(RESPONSE) + + const result = mapValidationResponse>(response) + + expect(isEither(result)).toBe(true) + expect(result.isFail()).toBe(true) + expect(result.value).toBeInstanceOf(ValidationError) + expect(result.isFail() && await result.value.getErrors()).toEqual((await RESPONSE.json()).errors) + }, }) - it('should throw NetworkError when Response is failed with status != 422', function () { + it('should throw NetworkError when Response is failed with status other than 422 and 403', function () { const RESPONSE = { ok: false, status: 400 } const response = createCheckableResponse(RESPONSE) diff --git a/src/utils/map-checkable-response.ts b/src/utils/map-checkable-response.ts index b7b8dae0..a5a07a2f 100644 --- a/src/utils/map-checkable-response.ts +++ b/src/utils/map-checkable-response.ts @@ -15,7 +15,7 @@ export const mapAuthorizationResponse = (result: Either): Ei export const mapValidationResponse = (result: Either): Either, T> => { if (result.isOk()) { return success(result.value) - } else if (result.value.response.status === 422) { + } else if ([422, 403].includes(result.value.response.status)) { return fail(new ValidationError(result.value.response)) } else { throw result.value