Next.js Discord

Discord Forum

Authentication with aws amplify without using UI component with app router (next js 14)

Answered
Cairn Terrier posted this in #help-forum
Open in Discord
Cairn TerrierOP
I have a problem on how to make the authentication with the app router and aws amplify.
Answered by B33fb0n3
you can use their library to handle auth with amplify and app router:
https://docs.amplify.aws/javascript/build-a-backend/auth/enable-sign-up/#sign-in
View full answer

15 Replies

Answer
@B33fb0n3 you can use their library to handle auth with amplify and app router: https://docs.amplify.aws/javascript/build-a-backend/auth/enable-sign-up/#sign-in
Cairn TerrierOP
Yes I tried but for the logic. where to put Authenticator with the app router. When I do I get some errors
@Cairn Terrier Yes I tried but for the logic. where to put Authenticator with the app router. When I do I get some errors
you can add this (see attached) to your global provider. The global provider is a clientside component that wraps the children in it, so every children can stay server or clientside, but can still access amplify.
@B33fb0n3 you can add this (see attached) to your global provider. The global provider is a clientside component that wraps the children in it, so every children can stay server or clientside, but can still access amplify.
Cairn TerrierOP
Yes I did all those steps. At the. I have already existing ressources so I filled the infos with Amplify.configure({'my data here'}). Now for the logic., I wrapped my root layout with <Authenticator> This works perfectly. I have authentication in the whole app but I don't have the ability to like make a custom page login or signup and make a flow where if the user is not connected to be redirected to login etc..
but I don't have the ability to like make a custom page login or signup and make a flow

When using the Authenticator component from amplify you are not able to create a custom workflow. Of course you can change the visual part (see [here](https://ui.docs.amplify.aws/react/connected-components/authenticator/customization)) but not the functional (flow) part. So either rely on the authenticator and have no custom flow or create your own custom flow with the javascript library
Cairn TerrierOP
Okay so I guess I use jotai or context to have a auth state. If not authenticated , redirect to login and then I create my custom forms to auth with amplify functions right ?
But the problem that occurs with that is that if I try to access a page, I see the page for 1s before getting redirected to login page
@Cairn Terrier But the problem that occurs with that is that if I try to access a page, I see the page for 1s before getting redirected to login page
middleware is your solution ^^
Check the auth directly inside your middleware. It will be executed before the page even started loading
Cairn TerrierOP
Sorry for noob question but what is exactly a middleware. Is that the contxt ?
That’s fine. A middleware is a file that gets executed before another file (for example a layout or a page or whatever) loads. And the other file only loads after the middleware finished execution. You can take a look at the middleware here: https://nextjs.org/docs/app/building-your-application/routing/middleware
Cairn TerrierOP
Ok thanks I will check that
Cairn TerrierOP
It seems that when I put the getcurrentuser in the middle ware, It tells me Auth UserPool not configured but it is configured in the Layout of the route