-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: add Next.js server-side auth and HTTP-only cookie content #8224
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
Changes from 1 commit
e032de5
437d9b8
0e0a7de
3b53898
ccca1a4
6c39640
4db9f25
8445d87
f54ca4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -52,7 +52,7 @@ You can create an `amplifyServerUtils.ts` file under a `utils` folder in your co | |||||
|
||||||
For example, the `utils/amplifyServerUtils.ts` file may contain the following content: | ||||||
|
||||||
```typescript | ||||||
```typescript title="src/utils/amplifyServerUtils.ts" | ||||||
import { createServerRunner } from '@aws-amplify/adapter-nextjs'; | ||||||
import outputs from '@/amplify_outputs.json'; | ||||||
|
||||||
|
@@ -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" | ||||||
'use client'; | ||||||
|
||||||
import { Amplify } from 'aws-amplify'; | ||||||
|
@@ -123,7 +123,7 @@ export default function ConfigureAmplifyClientSide() { | |||||
|
||||||
`layout.tsx`: | ||||||
|
||||||
```jsx | ||||||
```jsx title="src/app/layout.tsx" | ||||||
HuiSF marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
import ConfigureAmplifyClientSide from '@/components/ConfigureAmplifyClientSide'; | ||||||
import './globals.css'; | ||||||
|
||||||
|
@@ -162,7 +162,7 @@ You can use the Amplify Auth category APIs to sign up and sign in your end users | |||||
|
||||||
You can use the `fetchAuthSession` API to check the auth sessions that are attached to the incoming requests in the middleware of your Next.js app to protect your routes. For example: | ||||||
|
||||||
```typescript | ||||||
```typescript title="src/middleware.ts" | ||||||
import { fetchAuthSession } from 'aws-amplify/auth/server'; | ||||||
import { NextRequest, NextResponse } from 'next/server'; | ||||||
import { runWithAmplifyServerContext } from '@/utils/amplifyServerUtils'; | ||||||
|
@@ -215,6 +215,166 @@ 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 | ||||||
|
### (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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
HuiSF marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
```shell title=".env" | |
```shell title=".env" showLineNumbers={false} |
does this need to be added to the Hosting env as well?
HuiSF marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`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
?
There was a problem hiding this comment.
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
.
HuiSF marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
runWithAmplifyServerContext | |
runWithAmplifyServerContext, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.