Skip to content

Commit ff038f8

Browse files
committed
Update examples
1 parent 3b1973c commit ff038f8

File tree

9 files changed

+81
-33
lines changed

9 files changed

+81
-33
lines changed

examples/with-next-ssr-app-directory/app/actions/checkSSRSession.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use server";
2+
3+
import { cookies } from "next/headers";
4+
import { getServerActionSession, init } from "supertokens-auth-react/nextjs/ssr";
5+
import { ssrConfig } from "../config/ssr";
6+
7+
init(ssrConfig());
8+
9+
export async function serverActionThatLoadsTheSession() {
10+
const cookiesStore = await cookies();
11+
const session = await getServerActionSession(cookiesStore);
12+
console.log("session", session);
13+
return Promise.resolve(true);
14+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"use server";
2+
3+
import { cookies } from "next/headers";
4+
import { redirect } from "next/navigation";
5+
import {
6+
getServerComponentSession,
7+
getServerActionSession,
8+
init,
9+
AuthenticatedServerAction,
10+
} from "supertokens-auth-react/nextjs/ssr";
11+
import { ssrConfig } from "../config/ssr";
12+
13+
init(ssrConfig());
14+
15+
export const serverActionThatReceivesTheSession: AuthenticatedServerAction<() => Promise<boolean>> = async (
16+
session
17+
) => {
18+
console.log("session", session);
19+
20+
return Promise.resolve(true);
21+
};

examples/with-next-ssr-app-directory/app/components/home.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import { SessionAuthForNextJS } from "./sessionAuthForNextJS";
1010
import { getServerComponentSession, init } from "supertokens-auth-react/nextjs/ssr";
1111
import { ssrConfig } from "../config/ssr";
1212
import { useState } from "react";
13-
import { SSRButton } from "./ssrButton";
13+
import { MiddlewareServerActionButton } from "./middlewareServerActionButton";
14+
import { ServerActionButton } from "./serverActionButton";
1415

1516
init(ssrConfig());
1617

@@ -35,7 +36,8 @@ export async function HomePage() {
3536
<div>Your userID is:</div>
3637
<div className={`${styles.truncate} ${styles.userId}`}>{session.userId}</div>
3738
<CallAPIButton />
38-
<SSRButton />
39+
<MiddlewareServerActionButton />
40+
<ServerActionButton />
3941
</div>
4042
</div>
4143
<LinksComponent />
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
"use client";
22

3-
import { checkSSRSession } from "../actions/checkSSRSession";
3+
import { serverActionThatReceivesTheSession } from "../actions/serverActionThatReceivesTheSession";
44
import { authenticateServerAction, init } from "supertokens-auth-react/nextjs/ssr";
55
import styles from "../page.module.css";
66
import { ssrConfig } from "../config/ssr";
77

8-
import { getAccessToken, attemptRefreshingSession } from "supertokens-web-js/recipe/session";
9-
108
init(ssrConfig());
119

12-
export const SSRButton = () => {
10+
export const MiddlewareServerActionButton = () => {
1311
return (
1412
<div
1513
style={{ marginTop: "20px" }}
1614
onClick={async (e) => {
17-
// const session = await getAccessToken();
18-
// console.log("sessionn in component", session);
19-
const result = await authenticateServerAction(checkSSRSession);
15+
const result = await authenticateServerAction(serverActionThatReceivesTheSession);
2016
console.log(result);
2117
}}
2218
className={styles.sessionButton}
2319
>
24-
Check SSR Session
20+
Server Action that receives the session
2521
</div>
2622
);
2723
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"use client";
2+
3+
import { serverActionThatLoadsTheSession } from "../actions/serverActionThatLoadsTheSession";
4+
import styles from "../page.module.css";
5+
6+
export const ServerActionButton = () => {
7+
return (
8+
<div
9+
style={{ marginTop: "20px" }}
10+
onClick={async (e) => {
11+
const result = await serverActionThatLoadsTheSession();
12+
console.log(result);
13+
}}
14+
className={styles.sessionButton}
15+
>
16+
Server Action that loads the session
17+
</div>
18+
);
19+
};

lib/build/nextjs/ssr.d.ts

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/build/nextjsssr.js

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/ts/nextjs/ssr.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ export default class SuperTokensNextjsSSRAPIWrapper {
111111
* Get the session state inside a server action
112112
* The function is meant to be used inside Next.js server actions
113113
* @param cookies - The cookies store exposed by next/headers (await cookies())
114-
* @returns The session context value or undefined if the session does not exist or is invalid
114+
* @returns An object that includes session context value and the status of the session ('valid' | 'expired' | 'invalid')
115+
* If the status is 'invalid' or 'expired' then the users should be considered as unauthenticated
115116
**/
116117
static async getServerActionSession(
117118
cookies: CookiesStore
@@ -122,15 +123,19 @@ export default class SuperTokensNextjsSSRAPIWrapper {
122123
logDebugMessage(`SSR Session State: ${state}`);
123124
if (state === "tokens-match") {
124125
return { session: session as LoadedSessionContext, status: "valid" };
125-
} else if (["tokens-do-not-match", "access-token-not-found", "access-token-invalid"].includes(state)) {
126+
} else if (
127+
["tokens-do-not-match", "front-token-expired", "access-token-not-found", "access-token-invalid"].includes(
128+
state
129+
)
130+
) {
126131
return { status: "expired", session: undefined };
127132
}
128133

129134
return { status: "invalid", session: undefined };
130135
}
131136

132137
/**
133-
* Authenticate a server action by passing the session context as a parameter
138+
* Authenticates a server action and then passes the session context as a parameter
134139
* If the session does not exist/user is not authenticated, it will automatically redirect to the login page
135140
* The function is meant to run on the client, before calling the actual server action
136141
* @param action - A server action that takes the session context as its first parameter
@@ -207,6 +212,9 @@ export const getServerComponentSession = SuperTokensNextjsSSRAPIWrapper.getServe
207212
export const getServerActionSession = SuperTokensNextjsSSRAPIWrapper.getServerActionSession;
208213
export const getServerSidePropsSession = SuperTokensNextjsSSRAPIWrapper.getServerSidePropsSession;
209214
export const authenticateServerAction = SuperTokensNextjsSSRAPIWrapper.authenticateServerAction;
215+
export type AuthenticatedServerAction<T extends (...args: any[]) => any> = T extends (...args: infer A) => infer R
216+
? (session?: LoadedSessionContext, ...originalArgs: A) => R
217+
: never;
210218

211219
function getAuthPagePath(redirectPath: string): string {
212220
const authPagePath = SuperTokensNextjsSSRAPIWrapper.getConfigOrThrow().appInfo.websiteBasePath || "/auth";

0 commit comments

Comments
 (0)