Skip to content

Commit e414401

Browse files
authored
feat: update nextjs server actions with getAccessToken (#755)
1 parent c5bb2d5 commit e414401

8 files changed

+101
-41
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import AssumingUrl from '../../fragments/_web-assuming-url.mdx';
33
import WebConfigureRedirectUris from '../../fragments/_web-configure-redirect-uris.mdx';
44
import SignInFlowSummary from '../../fragments/_web-sign-in-flow-summary.mdx';
55

6+
import ServerActionsTip from './_server-actions-tip.mdx';
7+
68
### Prepare configs
79

810
Prepare configuration for the Logto client:
@@ -102,6 +104,8 @@ Remember to add `'use client'` to the top of the file to indicate that these com
102104

103105
#### Add buttons to home page
104106

107+
<ServerActionsTip />
108+
105109
Now let's add the sign-in and sign-out buttons in your hoem page. We need to call the server actions in SDK when needed. To help with this, use `getLogtoContext` to fetch authentication status.
106110

107111
```tsx title="app/page.tsx"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:::note
2+
It is not allowed to define inline "use server" annotated Server Actions in Client Components. We have to pass it down through props from a Server Component.
3+
:::

docs/quick-starts/framework/next-app-router/api-resources/_fetch-access-token-for-api-resources.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import FetchAccessTokenForApiResources from '../../../fragments/_fetch-access-token-for-api-resources.mdx';
22

3-
import GetAccessTokenCode from './code/_get-access-token-code.md';
3+
import GetAccessTokenCode from './code/_get-access-token-code.mdx';
44

55
<FetchAccessTokenForApiResources
66
getAccessTokenApi="getAccessToken"

docs/quick-starts/framework/next-app-router/api-resources/_fetch-organization-token-for-user.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import FetchOrganizationTokenForUser from '../../../fragments/_fetch-organization-token-for-user.mdx';
22

33
import ConfigOrganizationCode from './code/_config-organization-code.md';
4-
import GetOrganizationAccessTokenCode from './code/_get-organization-access-token-code.md';
4+
import GetOrganizationAccessTokenCode from './code/_get-organization-access-token-code.mdx';
55

66
<FetchOrganizationTokenForUser
77
organizationScope="UserScope.Organizations"

docs/quick-starts/framework/next-app-router/api-resources/code/_get-access-token-code.md

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import ServerActionsTip from '../../_server-actions-tip.mdx';
2+
3+
<ServerActionsTip />
4+
5+
```tsx title="app/page.ts"
6+
import { getAccessToken } from '@logto/next/server-actions';
7+
import { logtoConfig } from './logto';
8+
import GetAccessToken from './get-access-token';
9+
10+
export default async function Home() {
11+
return (
12+
<main>
13+
<GetAccessToken
14+
onGetAccessToken={async () => {
15+
'use server';
16+
17+
return getAccessToken(logtoConfig, 'https://shopping.your-app.com/api');
18+
}}
19+
/>
20+
</main>
21+
);
22+
}
23+
```
24+
25+
```tsx title="app/get-access-token.ts"
26+
'use client';
27+
28+
type Props = {
29+
onGetAccessToken: () => Promise<string>;
30+
};
31+
32+
const GetAccessToken = ({ onGetAccessToken }: Props) => {
33+
return (
34+
<button
35+
onClick={async () => {
36+
const token = await onGetAccessToken();
37+
console.log(token);
38+
}}
39+
>
40+
Get access token (see console log)
41+
</button>
42+
);
43+
};
44+
45+
export default GetAccessToken;
46+
```

docs/quick-starts/framework/next-app-router/api-resources/code/_get-organization-access-token-code.md

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import ServerActionsTip from '../../_server-actions-tip.mdx';
2+
3+
<ServerActionsTip />
4+
5+
```tsx title="app/page.ts"
6+
import { getOrganizationToken } from '@logto/next/server-actions';
7+
import { logtoConfig } from './logto';
8+
import GetOrganizationToken from './get-organization-token';
9+
10+
export default async function Home() {
11+
return (
12+
<main>
13+
<GetOrganizationToken
14+
onGetOrganizationToken={async () => {
15+
'use server';
16+
17+
return getOrganizationToken(logtoConfig, 'organization-id');
18+
}}
19+
/>
20+
</main>
21+
);
22+
}
23+
```
24+
25+
```tsx title="app/get-organization-token.ts"
26+
'use client';
27+
28+
type Props = {
29+
onGetOrganizationToken: () => Promise<string>;
30+
};
31+
32+
const GetOrganizationToken = ({ onGetOrganizationToken }: Props) => {
33+
return (
34+
<button
35+
onClick={async () => {
36+
const token = await onGetOrganizationToken();
37+
console.log(token);
38+
}}
39+
>
40+
Get organization token (see console log)
41+
</button>
42+
);
43+
};
44+
45+
export default GetOrganizationToken;
46+
```

0 commit comments

Comments
 (0)