Skip to content

Commit

Permalink
feat(next): add getAccessTokenRSC function to server actions
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsograziano committed Aug 5, 2024
1 parent 7c65c7b commit 73aa212
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .changeset/angry-moons-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@logto/next-server-actions-sample": minor
"@logto/next": minor
---

add getAccessTokenRSC to the server actions package

Introduced two new asynchronous functions, getAccessTokenRSC and getOrganizationTokenRSC, designed to retrieve access tokens within React Server Components (RSC) environments. These functions facilitate token management in a server-side context without updating the session, since in RSC cookies are not writable.
9 changes: 8 additions & 1 deletion packages/next-server-actions-sample/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getAccessToken, getLogtoContext, signIn, signOut } from '@logto/next/server-actions';
import { getAccessToken, getLogtoContext, signIn, signOut, getAccessTokenRSC } from '@logto/next/server-actions';
import SignIn from './sign-in';
import SignOut from './sign-out';
import { logtoConfig } from './logto';
Expand All @@ -7,6 +7,12 @@ import GetAccessToken from './get-access-token';
export default async function Home() {
const { isAuthenticated, claims } = await getLogtoContext(logtoConfig);

const token = await getAccessTokenRSC(logtoConfig);

// Use the token in RSC
// You can't write to the cookie in a React Server Component, so if the access token is refreshed, it won't be persisted in the session.
console.log(token)

return (
<main>
<h1>Hello Logto.</h1>
Expand All @@ -27,6 +33,7 @@ export default async function Home() {
return getAccessToken(logtoConfig);
}}
/>
<p>RSC Token: {token}</p>
</>
) : (
<SignIn
Expand Down
33 changes: 33 additions & 0 deletions packages/next/server-actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,37 @@ export const getOrganizationToken = async (
return getAccessToken(config, undefined, organizationId);
};


/**
* Get access token for the specified resource or organization,
* this function can be used in React Server Components (RSC)
* Note: You can't write to the cookie in a React Server Component, so if the access token is refreshed, it won't be persisted in the session.
* When using server actions or API routes, we highly reccomend to use the getAccessToken method
*/
export const getAccessTokenRSC = async (
config: LogtoNextConfig,
resource?: string,
organizationId?: string
): Promise<string> => {
const client = new LogtoClient(config);
const { nodeClient, session } = await client.createNodeClientFromHeaders(
await getCookies(config)
);
return nodeClient.getAccessToken(resource, organizationId);
};


/**
* Get organization token from session,
* this function can be used in React Server Components (RSC)
* Note: You can't write to the cookie in a React Server Component, so if the access token is refreshed, it won't be persisted in the session.
* When using server actions or API routes, we highly reccomend to use the getOrganizationToken method
*/
export const getOrganizationTokenRSC = async (
config: LogtoNextConfig,
organizationId?: string
): Promise<string> => {
return getAccessTokenRSC(config, undefined, organizationId);
};

export { default } from './client';

0 comments on commit 73aa212

Please sign in to comment.