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

Question: Using Authenticator with App Router #4905

Closed
4 tasks done
zvictor opened this issue Jan 11, 2024 · 5 comments
Closed
4 tasks done

Question: Using Authenticator with App Router #4905

zvictor opened this issue Jan 11, 2024 · 5 comments
Labels
Authenticator An issue or a feature-request for an Authenticator UI Component pending-response Issue is pending response from the issue requestor question General question

Comments

@zvictor
Copy link

zvictor commented Jan 11, 2024

Before creating a new issue, please confirm:

On which framework/platform are you having an issue?

React

Which UI component?

Authenticator

How is your app built?

Next.js

What browsers are you seeing the problem on?

Chrome, Firefox

Please describe your bug.

Note: Similar issues have been reported before, but I believe there are important nuances on this one.

While using the <Authenticator> component (as a wrapper of my App) to setup Amplify in the new Nextjs app router, I could notice that logged in users would sometimes be mistakenly redirected to /sign-in but been shown a 404 page instead of the signInUp form.

I did some debugging and could notice that useAuthenticator is returning idle as route.

const { route } = useAuthenticator(({ route }) => [route]);

@ErikCH commented on a similar issue:

On refresh the route will temporarily be in a setup or idle state before it transitions to an authenticated state. If you check route as soon as the page loads, and redirect somewhere while it's in the idle or setup state, that could be an issue.

...

In this scenario if someone goes to an authenticated route, and it's an idle or setup state then we redirect back to /login. Which will then by that time see the user is authenticated and will re-route back to the authenticated page.

What's the expected behaviour?

  1. Logged in users should not be redirected to /sign-in
  2. Logged in users should not see a 404 page when visiting /sign-in

Help us reproduce the bug!

  1. Start a new next.js project
  2. Set middleware.ts as defined at the Server-Side Rendering guide
  3. Add Authenticator to /app/providers.tsx, as shown below:
'use client'

import { Amplify } from 'aws-amplify'
import { Authenticator } from '@aws-amplify/ui-react'
import * as amplifyConfig from '@/modules/auth/config'
import { ThemeProvider as NextThemesProvider } from 'next-themes'
import '@aws-amplify/ui-react/styles.css'

Amplify.configure(amplifyConfig, { ssr: true })

export function Providers({ children }: { children: React.ReactNode }) {
  return (
      <NextThemesProvider attribute="class" defaultTheme="system">
        <Authenticator variation="modal">{children}</Authenticator>
      </NextThemesProvider>
  )
}

Once that is done you should notice the problem reported as long as you try navigating and reloading the pages while logged in.

Code Snippet

  "dependencies": {
    "@aws-amplify/adapter-nextjs": "1.0.10",
    "@aws-amplify/auth": "6.0.10",
    "@aws-amplify/ui-react": "6.1.0",
    "aws-amplify": "6.0.10",
    "next": "14.0.4",
    "next-themes": "0.2.1",
    "react": "18.2.0",
    "react-dom": "18.2.0",
  }

Related to

@github-actions github-actions bot added the pending-triage Issue is pending triage label Jan 11, 2024
@reesscot
Copy link
Member

Hi @zvictor,
I'm sorry you're having trouble with the Authenticator. Can you please provide more details on how you are doing the redirect to your /sign-in page? The Authenticator is a Client Component and doesn't have any control over routing in Next.js App Router. I'm unclear what you're trying to accomplish using the useAuthenticator hook, but that will not work in a server component. The best way to handle routing is to use middleware to do the /sign-in redirect on the server side. You may want to take a look at this guide:
https://docs.amplify.aws/gen2/start/quickstart/nextjs-app-router/

@reesscot reesscot added pending-response Issue is pending response from the issue requestor Authenticator An issue or a feature-request for an Authenticator UI Component question General question and removed pending-triage Issue is pending triage labels Jan 12, 2024
@zvictor
Copy link
Author

zvictor commented Jan 12, 2024

Oh, you are right! I didn't remember setting up middleware.ts in my app and I was really struggling to understand why there was a redirection happening. I thought there was some magic happening inside Authenticator, but couldn't find it in the source code 🤦

Anyway, the content of my middleware.ts is the same recommended by the Server-Side Rendering guide, verbatim.


the bug description has been updated to include this info

@reesscot
Copy link
Member

reesscot commented Jan 18, 2024

@zvictor Can you clarify how you are trying to use useAuthenticator in your situation? It's not recommended to wrap your application in Authenticator when using the Next.js App Router, it would be better to render it on your /sign-in page instead and use middleware to protect your routes. We have a guide that will be coming out soon showing this pattern: aws-amplify/docs#6776

@thaddmt thaddmt added the bug Something isn't working label Jan 22, 2024
@reesscot reesscot removed the bug Something isn't working label Jan 22, 2024
@reesscot reesscot changed the title Authenticator component returns idle in authenticated state [2] Question: Using Authenticator with App Router Jan 22, 2024
@reesscot
Copy link
Member

@zvictor Following up here to say that our Next.js App Router (Server Components) guide has been published.

Did you have any further questions?

@reesscot
Copy link
Member

reesscot commented Feb 8, 2024

Closing this out as we haven't heard back. Please open a new issue if you are still having trouble with App Router.

@reesscot reesscot closed this as completed Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Authenticator An issue or a feature-request for an Authenticator UI Component pending-response Issue is pending response from the issue requestor question General question
Projects
None yet
Development

No branches or pull requests

3 participants