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(next): add getAccessTokenRSC function to server actions #780

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
37 changes: 33 additions & 4 deletions packages/next/server-actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ export const getLogtoContext = async (
GetContextParameters,
'getAccessToken' | 'resource' | 'organizationId' | 'getOrganizationToken'
> & {
/** @deprecated use getAccessToken() */
/** @deprecated use getAccessTokenRSC() */
getAccessToken?: GetContextParameters['getAccessToken'];
/** @deprecated use getOrganizationToken() */
/** @deprecated use getOrganizationTokenRSC() */
getOrganizationToken?: GetContextParameters['getOrganizationToken'];
/** @deprecated use getAccessToken() */
/** @deprecated use getAccessTokenRSC() */
resource?: GetContextParameters['resource'];
/** @deprecated use getOrganizationToken() */
/** @deprecated use getOrganizationTokenRSC() */
organizationId?: GetContextParameters['organizationId'];
}
): Promise<LogtoContext> => {
Expand Down Expand Up @@ -146,4 +146,33 @@ 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 recommand to use the getAccessToken method
*/
export const getAccessTokenRSC = async (
config: LogtoNextConfig,
resource?: string,
organizationId?: string
): Promise<string> => {
const client = new LogtoClient(config);
const { nodeClient } = 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 recommand to use the getOrganizationToken method
*/
export const getOrganizationTokenRSC = async (
config: LogtoNextConfig,
organizationId?: string
): Promise<string> => {
return getAccessTokenRSC(config, undefined, organizationId);
};

export { default } from './client';