Skip to content

Commit 3443536

Browse files
authored
feat: add tip for auto sign-in in next app (#1311)
1 parent f5c272e commit 3443536

File tree

1 file changed

+31
-0
lines changed
  • docs/quick-starts/framework/next-app-router

1 file changed

+31
-0
lines changed

docs/quick-starts/framework/next-app-router/README.mdx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,37 @@ HTTP does not allow setting cookies after streaming starts, `getOrganizationToke
106106

107107
<ExternalStorage />
108108

109+
## Automatic sign-in redirect on unauthorized \{#automatic-sign-in-redirect\}
110+
111+
:::tip
112+
The `signIn` helper modifies cookies to establish the sign-in session, so it cannot be invoked directly in a React Server Component (RSC). To trigger sign-in automatically when an RSC detects an unauthorized user, call `signIn` in a dedicated route handler and redirect to that route.
113+
:::
114+
115+
```ts title="app/sign-in/route.ts"
116+
import { signIn } from '@logto/next/server-actions';
117+
import { logtoConfig } from '../../logto';
118+
119+
export async function GET() {
120+
await signIn(logtoConfig);
121+
}
122+
```
123+
124+
```tsx title="app/protected/page.tsx"
125+
import { getLogtoContext } from '@logto/next/server-actions';
126+
import { redirect } from 'next/navigation';
127+
import { logtoConfig } from '../logto';
128+
129+
export default async function ProtectedPage() {
130+
const { isAuthenticated } = await getLogtoContext(logtoConfig);
131+
132+
if (!isAuthenticated) {
133+
redirect('/sign-in');
134+
}
135+
136+
return <div>Protected content</div>;
137+
}
138+
```
139+
109140
## Further readings \{#further-readings}
110141

111142
<FurtherReadings />

0 commit comments

Comments
 (0)