Skip to content

Commit 5d1584c

Browse files
authored
update next app router quickstart to revise todo state and authenticator placement (#8025)
* update app router template to revise todo state and authenticator placement * update code style
1 parent d489f47 commit 5d1584c

File tree

1 file changed

+63
-34
lines changed
  • src/pages/[platform]/start/quickstart/nextjs-app-router-client-components

1 file changed

+63
-34
lines changed

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

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -219,43 +219,73 @@ This should start a local dev server at http://localhost:3000.
219219

220220
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.
221221

222-
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:
222+
The fastest way to get your login experience up and running is to use our Authenticator UI component. In your **app/layout.tsx** file, import the Authenticator UI component and wrap the children or pages components.
223223

224-
```bash showLineNumbers={false}
225-
npm add @aws-amplify/ui-react
226-
```
227-
228-
Next, import the Authenticator UI component and wrap your `<main>` element.
229224

225+
```tsx title="app/layout.tsx"
226+
"use client"
230227

231-
```tsx title="app/page.tsx"
228+
import React from "react";
229+
import { Amplify } from "aws-amplify";
230+
import "./app.css";
232231
// highlight-start
233-
import { Authenticator } from '@aws-amplify/ui-react'
234-
import '@aws-amplify/ui-react/styles.css'
232+
import { Authenticator } from "@aws-amplify/ui-react";
233+
import "@aws-amplify/ui-react/styles.css";
235234
// highlight-end
236-
// ... other imports
235+
import outputs from "@/amplify_outputs.json";
237236

238-
function App() {
239-
// ...
237+
Amplify.configure(outputs);
238+
239+
export default function RootLayout({
240+
children,
241+
}: {
242+
children: React.ReactNode;
243+
}) {
240244
return (
241245
// highlight-start
242-
<Authenticator>
243-
{({ signOut, user }) => (
246+
<html lang="en">
247+
<body>
248+
<Authenticator>
249+
{children}
250+
</Authenticator>
251+
</body>
252+
</html>
244253
// highlight-end
245-
<main>
246-
{/*...*/}
247-
// highlight-next-line
248-
<button onClick={signOut}>Sign out</button>
249-
</main>
250-
)}
251-
// highlight-next-line
252-
</Authenticator>
253-
)
254+
);
254255
}
255256
```
256257

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

260+
In your **app/page.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.
261+
262+
```tsx title="app/page.tsx"
263+
import type { Schema } from "@/amplify/data/resource";
264+
// highlight-next-line
265+
import { useAuthenticator } from "@aws-amplify/ui-react";
266+
import { useState, useEffect } from "react";
267+
import { generateClient } from "aws-amplify/data";
268+
269+
const client = generateClient<Schema>();
270+
271+
export default function HomePage() {
272+
273+
// highlight-start
274+
const { signOut } = useAuthenticator();
275+
// highlight-end
276+
277+
// ...
278+
279+
return (
280+
<main>
281+
{/* ... */}
282+
// highlight-next-line
283+
<button onClick={signOut}>Sign out</button>
284+
</main>
285+
);
286+
}
287+
```
288+
259289
Try out your application in your localhost environment again. You should be presented with a login experience now.
260290

261291
<Video src="/images/gen2/getting-started/react/demo-auth.mp4" description="Video - Authentication Demo" />
@@ -329,24 +359,23 @@ export const data = defineData({
329359
});
330360
```
331361

332-
In the application client code, let's also render the username to distinguish different users once they're logged in. Go to your **app/page.tsx** file and render the `user` property.
362+
In the application client code, let's also render the username to distinguish different users once they're logged in. Go to your **app/page.tsx** file and render the `user` property from the `useAuthenticator` hook.
333363

334364
```tsx title="app/page.tsx"
335365
// ... imports
336366

337-
function App() {
367+
function HomePage() {
368+
// highlight-next-line
369+
const { user, signOut } = useAuthenticator();
370+
338371
// ...
372+
339373
return (
340-
<Authenticator>
374+
<main>
341375
// highlight-next-line
342-
{({ signOut, user }) => (
343-
<main>
344-
// highlight-next-line
345-
<h1>{user?.signInDetails?.loginId}'s todos</h1>
346-
{/* ... rest of the UI */}
347-
</main>
348-
)}
349-
</Authenticator>
376+
<h1>{user?.signInDetails?.loginId}'s todos</h1>
377+
{/* ... */}
378+
</main>
350379
)
351380
}
352381
```

0 commit comments

Comments
 (0)