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

feat: add Next.js server-side auth and HTTP-only cookie content #8224

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

HuiSF
Copy link
Member

@HuiSF HuiSF commented Jan 29, 2025

Description of changes:

Related GitHub issue #, if available:

Instructions

If this PR should not be merged upon approval for any reason, please submit as a DRAFT

Which product(s) are affected by this PR (if applicable)?

  • amplify-cli
  • amplify-ui
  • amplify-studio
  • amplify-hosting
  • amplify-libraries

Which platform(s) are affected by this PR (if applicable)?

  • JS
  • Swift
  • Android
  • Flutter
  • React Native

Please add the product(s)/platform(s) affected to the PR title

Checks

  • Does this PR conform to the styleguide?

  • Does this PR include filetypes other than markdown or images? Please add or update unit tests accordingly.

  • Are any files being deleted with this PR? If so, have the needed redirects been created?

  • Are all links in MDX files using the MDX link syntax rather than HTML link syntax?

    ref: MDX: [link](https://docs.amplify.aws/)
    HTML: <a href="https://docs.amplify.aws/">link</a>

When this PR is ready to merge, please check the box below

  • Ready to merge

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.


**Step 2 - Export the `createAuthRouteHandlers` function**

`createAuthRouteHandlers` function is created by the `createServerRunner` function call when you configure Amplify for server-side usage. You can export this function from your `amplifyServerUtils.ts` file. You can also configure cookie attributes with the `runtimeOptions` parameter.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`createAuthRouteHandlers` function is created by the `createServerRunner` function call when you configure Amplify for server-side usage. You can export this function from your `amplifyServerUtils.ts` file. You can also configure cookie attributes with the `runtimeOptions` parameter.
The `createAuthRouteHandlers` function is created by the `createServerRunner` function call when you configure Amplify for server-side usage. You can export this function from your `amplifyServerUtils.ts` file. You can also configure cookie attributes with the `runtimeOptions` parameter.

Sanity check: is the parameter here (and elsewhere below) still runtimeOptions?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes the parameter name is still runtimeOptions.

@@ -123,7 +123,7 @@ export default function ConfigureAmplifyClientSide() {

`layout.tsx`:

```jsx
```jsx title="src/app/layout.tsx"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```jsx title="src/app/layout.tsx"
```tsx title="src/app/layout.tsx"

@@ -108,7 +108,7 @@ If you're using the Next.js App Router, you can create a client component to con

`ConfigureAmplifyClientSide.ts`:

```typescript
```typescript title="src/components/ConfigureAmplifyClientSide.tsx"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```typescript title="src/components/ConfigureAmplifyClientSide.tsx"
```tsx title="src/components/ConfigureAmplifyClientSide.tsx"

@@ -215,6 +215,172 @@ In this example, if the incoming request is not associated with a valid user ses

</Callout>

### (Preview) Perform authentication on the server side and enable HTTP-only cookies
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### (Preview) Perform authentication on the server side and enable HTTP-only cookies
### (Preview) Perform authentication on the server side and enable HttpOnly cookies

nit for the attribute name

To authenticate users on the server side, you must enable either Amazon Cognito Managed Login or Hosted UI in your Amazon Cognito User Pool client.


**Step 1 - Specify the origin of your app in environment variables**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we convert the steps to h4's?

loginWith: {
email: true,
externalProviders: {
google: {...},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
google: {...},
google: {/* ... */},

Comment on lines +249 to +257
import config from '@/amplify_outputs.json';

export const {
runWithAmplifyServerContext
// highlight-start
createAuthRouteHandlers,
// highlight-end
} = createServerRunner({
config,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import config from '@/amplify_outputs.json';
export const {
runWithAmplifyServerContext
// highlight-start
createAuthRouteHandlers,
// highlight-end
} = createServerRunner({
config,
import outputs from '@/amplify_outputs.json';
export const {
runWithAmplifyServerContext
// highlight-start
createAuthRouteHandlers,
// highlight-end
} = createServerRunner({
config: outputs,


Add the following environment variables to your Next.js app. For example in a `.env` file:

```shell title=".env"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```shell title=".env"
```shell title=".env" showLineNumbers={false}

does this need to be added to the Hosting env as well?


```typescript
```typescript title="src/components/client/ConfigureAmplifyClientSide.tsx"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```typescript title="src/components/client/ConfigureAmplifyClientSide.tsx"
```tsx title="src/components/client/ConfigureAmplifyClientSide.tsx"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants