diff --git a/jam-ui/cypress/integration/auth/login.js b/jam-ui/cypress/integration/auth/login.js index 0522f5ae9..4b6fac0c1 100644 --- a/jam-ui/cypress/integration/auth/login.js +++ b/jam-ui/cypress/integration/auth/login.js @@ -1,5 +1,32 @@ /// +function submitLogin(){ + const userAttrs = { + id: '1', + first_name: 'Peter', + last_name: 'Pan', + name: 'Peter Pan', + photo_url: '', + email: 'peter@example.com' + } + + cy.intercept('POST', `${Cypress.env('apiBaseUrl')}/auths/login`, { + statusCode: 200, + body: userAttrs + }).as('createSession') + + cy.intercept('GET', `${Cypress.env('apiBaseUrl')}/me`, { + statusCode: 200, + body: userAttrs + }).as('getCurrentUser') + + cy.reload() + cy.get('[data-testid=email]').type('peter@example.com') + cy.get('[data-testid=password]').type('jam123') + cy.get('[data-testid=submit]').click() + +} + describe('Unauthenticated users redirect to login page', () => { it('redirects to login page', () => { cy.clearCookie('remeber_token') @@ -12,10 +39,10 @@ describe('Unauthenticated users redirect to login page', () => { describe('Login page', () => { before(() => { cy.clearCookie('remeber_token') - cy.visit('/authentication/basic/login') }) it('validate login form', () => { + cy.visit('/authentication/basic/login') cy.reload() cy.get('[data-testid=submit]').should('be.disabled') cy.get('[data-testid=email]').type('invalid-email-format@example') @@ -27,6 +54,7 @@ describe('Login page', () => { }) it('submit login form with invalid credentials', () => { + cy.visit('/authentication/basic/login') cy.reload() cy.get('[data-testid=email]').type('peter@example.com') cy.get('[data-testid=password]').type('wrong') @@ -35,35 +63,20 @@ describe('Login page', () => { }) it('submits login form', () => { - const userAttrs = { - id: '1', - first_name: 'Peter', - last_name: 'Pan', - name: 'Peter Pan', - photo_url: '', - email: 'peter@example.com' - } - - cy.intercept('POST', `${Cypress.env('apiBaseUrl')}/auths/login`, { - statusCode: 200, - body: userAttrs - }).as('createSession') - - cy.intercept('GET', `${Cypress.env('apiBaseUrl')}/me`, { - statusCode: 200, - body: userAttrs - }).as('getCurrentUser') - - cy.reload() - cy.get('[data-testid=email]').type('peter@example.com') - cy.get('[data-testid=password]').type('jam123') - cy.get('[data-testid=submit]').click() + cy.visit('/authentication/basic/login') + submitLogin() cy.url().should('eq', Cypress.config().baseUrl + '/') // tests won't fail in case the port changes //cy.contains('Signed in as peter@example.com') //cy.contains('Peter Pan') //cy.getCookie('remember_token').should('exist') + }) - + it('redirect to requested page', () => { + cy.visit('/friends') + cy.url().should('include', '/authentication/basic/login') + cy.reload() + submitLogin() + cy.url().should('eq', Cypress.config().baseUrl + '/friends') }) }) diff --git a/jam-ui/src/components/auth/LoginForm.js b/jam-ui/src/components/auth/LoginForm.js index 32193dfd6..104fe6f6a 100644 --- a/jam-ui/src/components/auth/LoginForm.js +++ b/jam-ui/src/components/auth/LoginForm.js @@ -1,6 +1,6 @@ import React, { useState, useEffect } from 'react'; import PropTypes from 'prop-types'; -import { Link, useHistory } from 'react-router-dom'; +import { Link, useHistory, useLocation } from 'react-router-dom'; import { toast } from 'react-toastify'; import { Button, Form, Row, Col, FormGroup, Input, CustomInput, Label } from 'reactstrap'; import Divider from '../common/Divider'; @@ -17,6 +17,10 @@ const LoginForm = ({ setRedirect, hasLabel, layout }) => { const [isDisabled, setIsDisabled] = useState(true); const history = useHistory(); + + const location = useLocation(); + let { from } = location.state || { from: { pathname: "/" } }; + const { login, setCurrentUser } = useAuth(); // Handler @@ -31,7 +35,8 @@ const LoginForm = ({ setRedirect, hasLabel, layout }) => { toast.success(`Signed in as ${email}`); setEmail('') setPassword('') - setRedirect(true) + //setRedirect(true) + history.replace(from); }else{ toast.error("Incorrect email or password"); } diff --git a/jam-ui/src/helpers/privateRoute.js b/jam-ui/src/helpers/privateRoute.js index 6b00f2271..786fa78b9 100644 --- a/jam-ui/src/helpers/privateRoute.js +++ b/jam-ui/src/helpers/privateRoute.js @@ -16,7 +16,14 @@ const PrivateRoute = ({ component: Component, ...otherProps }) => { isAuthenticated ? ( ) : ( - + // + + ) ) : ( //