Skip to content

Commit fb05a1d

Browse files
committed
separate vanilla react and next.js auth example
1 parent 2aeabe7 commit fb05a1d

File tree

1 file changed

+42
-13
lines changed
  • src/pages/[platform]/build-a-backend/auth/set-up-auth

1 file changed

+42
-13
lines changed

src/pages/[platform]/build-a-backend/auth/set-up-auth/index.mdx

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,46 @@ Creating and correctly implementing the sign-in flow can be challenging and time
100100

101101
Amplify has pre-built UI components for React, Vue, Angular, React Native, Swift, Android, and Flutter. In this guide, we are focusing on those for web applications.
102102

103-
<InlineFilter filters={["javascript", "react"]}>
103+
<InlineFilter filters={["javascript", "next.js", "react"]}>
104104

105105
First, install the `@aws-amplify/ui-react` library:
106106

107107
```bash title="Terminal" showLineNumbers={false}
108108
npm add @aws-amplify/ui-react
109109
```
110110

111+
<InlineFilter filters={["javascript", "react"]}>
112+
113+
Next, open **pages/App.tsx** and add the `Authenticator` component.
114+
115+
```tsx title="pages/App.tsx"
116+
import { Authenticator } from '@aws-amplify/ui-react';
117+
import { Amplify } from 'aws-amplify';
118+
import outputs from '../amplify_outputs.json';
119+
import '@aws-amplify/ui-react/styles.css';
120+
121+
Amplify.configure(outputs);
122+
123+
export default function App() {
124+
return (
125+
<Authenticator>
126+
{({ signOut, user }) => (
127+
<main>
128+
<h1>Hello {user?.username}</h1>
129+
<button onClick={signOut}>Sign out</button>
130+
</main>
131+
)}
132+
</Authenticator>
133+
);
134+
};
135+
```
136+
137+
</InlineFilter>
138+
<InlineFilter filters={["next.js"]}>
139+
111140
Next, open **pages/\_app.tsx** and add the `Authenticator` component.
112141

113-
```ts title="pages/_app.tsx"
142+
```tsx title="pages/_app.tsx"
114143
import type { AppProps } from 'next/app';
115144
import { Authenticator } from '@aws-amplify/ui-react';
116145
import { Amplify } from 'aws-amplify';
@@ -134,6 +163,7 @@ export default function App({ Component, pageProps }: AppProps) {
134163
};
135164
```
136165

166+
</InlineFilter>
137167
</InlineFilter>
138168
<InlineFilter filters={["vue"]}>
139169
<BlockSwitcher>
@@ -183,7 +213,7 @@ npm add @aws-amplify/ui-components
183213

184214
Now open **src/main.ts** and add the following below your last import:
185215

186-
```js title="src/main.ts"
216+
```ts title="src/main.ts"
187217
import '@aws-amplify/ui-components';
188218
import {
189219
applyPolyfills,
@@ -204,7 +234,7 @@ Next, open **src/App.ts** and add the `amplify-authenticator` component.
204234

205235
The `amplify-authenticator` component offers a simple way to add authentication flows into your app. This component encapsulates an authentication workflow in the framework of your choice and is backed by your backend Auth resources. The optional `amplify-sign-out` component is available if you would like to render a sign-out button.
206236

207-
```html title="src/App.ts"
237+
```html title="src/App.vue"
208238
<template>
209239
<amplify-authenticator>
210240
<div>
@@ -229,7 +259,7 @@ npm add @aws-amplify/ui-angular
229259

230260
Now open **app.module.ts** and add the Amplify imports and configuration:
231261

232-
```js title="app.module.ts"
262+
```ts title="app.module.ts"
233263
import { NgModule } from '@angular/core';
234264
import { BrowserModule } from '@angular/platform-browser';
235265
import { AmplifyAuthenticatorModule } from '@aws-amplify/ui-angular';
@@ -314,15 +344,20 @@ npx expo prebuild
314344
</Callout>
315345
Next, update the `App.tsx` file with the following to set up the authentication flow:
316346

317-
```typescript
318-
import React from "react";
347+
```tsx
319348
import { Button, View, StyleSheet, SafeAreaView } from "react-native";
320349
import { Amplify } from "aws-amplify";
321350
import { Authenticator, useAuthenticator } from "@aws-amplify/ui-react-native";
322351
import outputs from "./amplify_outputs.json";
323352

324353
Amplify.configure(outputs);
325354

355+
const styles = StyleSheet.create({
356+
signOutButton: {
357+
alignSelf: "flex-end",
358+
},
359+
});
360+
326361
const SignOutButton = () => {
327362
const { signOut } = useAuthenticator();
328363

@@ -345,12 +380,6 @@ const App = () => {
345380
);
346381
};
347382

348-
const styles = StyleSheet.create({
349-
signOutButton: {
350-
alignSelf: "flex-end",
351-
},
352-
});
353-
354383
export default App;
355384
```
356385

0 commit comments

Comments
 (0)