Skip to content

Commit d489f47

Browse files
authored
update next pages quickstart to revise todo state and authenticator placement (#8026)
* update next pages quickstart to revise todo state and authenticator placement * update code style
1 parent 10480fd commit d489f47

File tree

1 file changed

+51
-34
lines changed
  • src/pages/[platform]/start/quickstart/nextjs-pages-router

1 file changed

+51
-34
lines changed

src/pages/[platform]/start/quickstart/nextjs-pages-router/index.mdx

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -221,39 +221,28 @@ This should start a local dev server at http://localhost:3000.
221221

222222
The starter application already has a pre-configured auth backend defined in the **amplify/auth/resource.ts** file. We've configured it to support email and password login but you can extend it to support a variety of login mechanisms, including Google, Amazon, Sign In With Apple, and Facebook.
223223

224-
The fastest way to get your login experience up and running is to use our Authenticator UI component. First, install the Amplify UI component library:
224+
The fastest way to get your login experience up and running is to use our Authenticator UI component. In your **pages/_app.tsx** file, import the Authenticator UI component and wrap your `<App>` component.
225225

226-
```bash showLineNumbers={false}
227-
npm add @aws-amplify/ui-react
228-
```
229-
230-
Next, import the Authenticator UI component and wrap your `<main>` element.
231-
232-
233-
```tsx title="pages/index.tsx"
226+
```tsx title="pages/_app.tsx"
227+
import type { AppProps } from "next/app";
234228
// highlight-start
235229
import { Authenticator } from '@aws-amplify/ui-react'
236230
import '@aws-amplify/ui-react/styles.css'
237231
// highlight-end
238-
// ... other imports
232+
import "@/styles/app.css";
233+
import { Amplify } from "aws-amplify";
234+
import outputs from "@/amplify_outputs.json";
239235

240-
function App() {
241-
// ...
242-
return (
236+
Amplify.configure(outputs);
237+
238+
export default function App({ Component, pageProps }: AppProps) {
239+
return(
243240
// highlight-start
244241
<Authenticator>
245-
{({ signOut, user }) => (
246-
// highlight-end
247-
<main>
248-
{/*...*/}
249-
// highlight-next-line
250-
<button onClick={signOut}>Sign out</button>
251-
</main>
252-
// highlight-start
253-
)}
242+
<Component {...pageProps} />;
254243
</Authenticator>
255244
// highlight-end
256-
)
245+
)
257246
}
258247
```
259248
<Accordion title="See the complete amplify/auth/resources.ts">
@@ -305,6 +294,35 @@ export const auth = defineAuth({
305294

306295
The Authenticator component auto-detects your auth backend settings and renders the correct UI state based on the auth backend's authentication flow.
307296

297+
In your **pages/index.tsx** file, add a button to enable users to sign out of the application. Import the [`useAuthenticator`](https://ui.docs.amplify.aws/react/connected-components/authenticator/advanced#access-auth-state) hook from the Amplify UI library to hook into the state of the Authenticator.
298+
299+
```tsx title="pages/index.tsx"
300+
import type { Schema } from "@/amplify/data/resource";
301+
// highlight-next-line
302+
import { useAuthenticator } from "@aws-amplify/ui-react";
303+
import { useState, useEffect } from "react";
304+
import { generateClient } from "aws-amplify/data";
305+
306+
const client = generateClient<Schema>();
307+
308+
export default function HomePage() {
309+
310+
// highlight-start
311+
const { signOut } = useAuthenticator();
312+
// highlight-end
313+
314+
// ...
315+
316+
return (
317+
<main>
318+
{/* ... */}
319+
// highlight-next-line
320+
<button onClick={signOut}>Sign out</button>
321+
</main>
322+
);
323+
}
324+
```
325+
308326
Try out your application in your localhost environment again. You should be presented with a login experience now.
309327

310328
<Video src="/images/gen2/getting-started/react/demo-auth.mp4" description="Video - Authentication Demo" />
@@ -378,24 +396,23 @@ export const data = defineData({
378396
});
379397
```
380398

381-
In the application client code, let's also render the username to distinguish different users once they're logged in. Go to your **src/App.tsx** file and render the `user` property.
399+
In the application client code, let's also render the username to distinguish different users once they're logged in. Go to your **pages/index.tsx** file and render the `user` property from the `useAuthenticator` hook.
382400

383401
```tsx title="pages/index.tsx"
384402
// ... imports
385403

386-
function App() {
404+
function HomePage() {
405+
// highlight-next-line
406+
const { user, signOut } = useAuthenticator();
407+
387408
// ...
409+
388410
return (
389-
<Authenticator>
411+
<main>
390412
// highlight-next-line
391-
{({ signOut, user }) => (
392-
<main>
393-
// highlight-next-line
394-
<h1>{user?.signInDetails?.loginId}'s todos</h1>
395-
{/* ... rest of the UI */}
396-
</main>
397-
)}
398-
</Authenticator>
413+
<h1>{user?.signInDetails?.loginId}'s todos</h1>
414+
{/* ... */}
415+
</main>
399416
)
400417
}
401418
```

0 commit comments

Comments
 (0)