after login redirect the user to the page he requeted

This commit is contained in:
Nuwan 2021-12-06 18:59:32 +05:30
parent c6c1e96e85
commit cb344665cb
3 changed files with 53 additions and 28 deletions

View File

@ -1,5 +1,32 @@
///<reference types="cypress" />
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')
})
})

View File

@ -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");
}

View File

@ -16,7 +16,14 @@ const PrivateRoute = ({ component: Component, ...otherProps }) => {
isAuthenticated ? (
<Component {...props} />
) : (
<Redirect to={otherProps.redirectTo ? otherProps.redirectTo : '/authentication/basic/login'} />
// <Redirect to={otherProps.redirectTo ? otherProps.redirectTo : '/authentication/basic/login'} />
<Redirect
to={{
pathname: otherProps.redirectTo ? otherProps.redirectTo : '/authentication/basic/login',
state: { from: props.location }
}}
/>
)
) : (
// <Loader animation="grow" variant="primary" />