Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoredirect user to login page after session timeout #1174

Open
lisatjide opened this issue Feb 9, 2021 · 3 comments
Open

Autoredirect user to login page after session timeout #1174

lisatjide opened this issue Feb 9, 2021 · 3 comments
Labels
P2 Not urgent, but work on it once you're done with current tasks

Comments

@lisatjide
Copy link

lisatjide commented Feb 9, 2021

Is your feature request related to a problem? Please describe.
A 401 unauthorised alert will be prompted when a user who is already logged out trying to perform action on the UserPage.

Describe the solution you'd like
Ideally user should be redirected to the LogIn Page

@liangyuanruo
Copy link
Contributor

@lisatjide should the user be redirected to the login page instead?

@liangyuanruo liangyuanruo changed the title Autoredirect user to homepage after session timeout Autoredirect user to login page after session timeout Feb 9, 2021
@lisatjide lisatjide added the P2 Not urgent, but work on it once you're done with current tasks label Feb 16, 2021
@aniruddha-adhikary
Copy link

There is an existing implementation of the requested feature in two actions, one of them being creating a new link.

user/actions/index.ts

const createUrlOrRedirect =
  (history: History) =>
....
      if (response.status === 401) {
        history.push(LOGIN_PAGE)
        dispatch<SetUrlUploadStateAction>(setUrlUploadState(false))
      }

However, handling the error response in every single method where an HTTP call is made, would result in a number of duplicates. I suggest interacting with the history object directly, instead of passing it through props and arguments of methods in a long chain.

Proposal

Instantiate the history object outside of the entry-point.

history.ts

import { createHashHistory } from 'history'
export default createHashHistory()

Use the history object in the React Root.

index.ts

import * as history from './history';
...

And taking care of the redirection to the login page at the API Call layer.

app/utils/request.js

import * as history from '../../../history';

export const postJson = (
  url: string = '',
  data: object = {},
  options?: RequestInit,
) => {
  // ....
  const response = crossFetch(url, opts);
  if (response.code === 401) history.push(LOGIN_PAGE);
}

Something of this nature :) Any thoughts? @liangyuanruo

@aniruddha-adhikary
Copy link

aniruddha-adhikary commented Jul 8, 2021

Then again I just realized the state would be out-of-sync. Example:

dispatch<SetUrlUploadStateAction>(setUrlUploadState(false))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P2 Not urgent, but work on it once you're done with current tasks
Projects
None yet
Development

No branches or pull requests

3 participants