How to handle redirects with aws cognito federated identities

Viewed 1072

I have a SPA (Single page application) using ReactJS. Currently, I am allowing users to login with Facebook or Google through AWS Cognito with the Amplify console (My site is hosted with Amplify on S3).

My problem is that when a user tries to enter a page that requires login, he is then redirected to the Sign In page (Using react-router-dom v4), then when they log in, they get redirected to Facebook/Google and then redirected back to my site's home page.

I would like to return them to the page the sent them to sign in as this will cause a bad user experience for my users.

The Auth function I use is: Auth.federatedSignIn({ provider: "Facebook" }).

How can I send them back to the page that sent them to the redirect without storing the page in my store as the session refreshes and the store is not maintained?

1 Answers

You can pass your page as a custom state

Auth.federatedSignIn({provider: 'Google', customState: JSON.stringify(route)})

and get it back in an auth event

Hub.listen('auth', ({payload: {event, data}}) => {

  switch (event) {
    case 'signIn':
      user = data
      break

  case 'customOAuthState':
      route = JSON.parse(data)
      // go to route
      break;
  }

})

Related