Skip to content

Commit 084e0e5

Browse files
authored
Move oauthTokens out of Session and make available via authLoader (#33)
* Run `npm i` and commit new package version in lockfile * Add `onSuccess` param for side-effects when using `authLoader` * Remove `oauthTokens` from session data and interfaces * Remove mention of `oauthTokens` in README * Remove mention of `oauthTokens` in comment * Add to `options` object instead of a new parameter * Add example to README
1 parent db89c94 commit 084e0e5

File tree

5 files changed

+42
-20
lines changed

5 files changed

+42
-20
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ You can also control the pathname the user will be sent to after signing-in by p
6666
export const loader = authLoader({ returnPathname: '/dashboard' });
6767
```
6868

69+
If your application needs to persist `oauthTokens` or other auth-related information after the callback is successful, you can pass an `onSuccess` option:
70+
71+
```ts
72+
export const loader = authLoader({
73+
onSuccess: async ({ oauthTokens }) => {
74+
await saveToDatabase(oauthTokens);
75+
},
76+
});
77+
```
78+
6979
## Usage
7080

7181
### Access authentication data in your Remix application
@@ -80,10 +90,9 @@ import { authkitLoader } from '@workos-inc/authkit-remix';
8090
export const loader = (args: LoaderFunctionArgs) => authkitLoader(args);
8191

8292
export function App() {
83-
8493
// Retrieves the user from the session or returns `null` if no user is signed in
85-
// Other supported values include sessionId, accessToken, organizationId,
86-
// role, permissions, entitlements, impersonator and oauthTokens
94+
// Other supported values include `sessionId`, `accessToken`, `organizationId`,
95+
// `role`, `permissions`, `entitlements`, and `impersonator`.
8796
const { user, signInUrl, signUpUrl } = useLoaderData<typeof loader>();
8897

8998
return (
@@ -115,7 +124,6 @@ export async function action({ request }: ActionFunctionArgs) {
115124
}
116125

117126
export default function HomePage() {
118-
119127
const { user, signInUrl, signUpUrl } = useLoaderData<typeof loader>();
120128

121129
if (!user) {

package-lock.json

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

src/authkit-callback-route.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { redirect, json, LoaderFunctionArgs } from '@remix-run/node';
77

88
export function authLoader(options: HandleAuthOptions = {}) {
99
return async function loader({ request }: LoaderFunctionArgs) {
10-
const { returnPathname: returnPathnameOption = '/' } = options;
10+
const { returnPathname: returnPathnameOption = '/', onSuccess } = options;
1111

1212
const url = new URL(request.url);
1313

@@ -17,10 +17,11 @@ export function authLoader(options: HandleAuthOptions = {}) {
1717

1818
if (code) {
1919
try {
20-
const { accessToken, refreshToken, user, impersonator, oauthTokens } = await workos.userManagement.authenticateWithCode({
21-
clientId: WORKOS_CLIENT_ID,
22-
code,
23-
});
20+
const { accessToken, refreshToken, user, impersonator, oauthTokens } =
21+
await workos.userManagement.authenticateWithCode({
22+
clientId: WORKOS_CLIENT_ID,
23+
code,
24+
});
2425

2526
// Clean up params
2627
url.searchParams.delete('code');
@@ -41,14 +42,14 @@ export function authLoader(options: HandleAuthOptions = {}) {
4142
url.pathname = returnPathname;
4243
}
4344

44-
// The refreshToken and oauthTokens should never be accesible publicly, hence why we encrypt it in the cookie session
45-
// Alternatively you could persist the refresh token in a backend database
45+
// The refreshToken should never be accesible publicly, hence why we encrypt it
46+
// in the cookie session. Alternatively you could persist the refresh token in a
47+
// backend database.
4648
const encryptedSession = await encryptSession({
4749
accessToken,
4850
refreshToken,
4951
user,
5052
impersonator,
51-
oauthTokens,
5253
headers: {},
5354
});
5455

@@ -57,6 +58,16 @@ export function authLoader(options: HandleAuthOptions = {}) {
5758
session.set('jwt', encryptedSession);
5859
const cookie = await commitSession(session);
5960

61+
if (onSuccess) {
62+
await onSuccess({
63+
accessToken,
64+
impersonator: impersonator ?? null,
65+
oauthTokens: oauthTokens ?? null,
66+
refreshToken,
67+
user,
68+
});
69+
}
70+
6071
return redirect(url.toString(), {
6172
headers: {
6273
'Set-Cookie': cookie,

src/interfaces.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import { OauthTokens, User } from '@workos-inc/node';
22

33
export interface HandleAuthOptions {
44
returnPathname?: string;
5+
onSuccess?: (data: AuthLoaderSuccessData) => void | Promise<void>;
6+
}
7+
8+
export interface AuthLoaderSuccessData {
9+
accessToken: string;
10+
impersonator: Impersonator | null;
11+
oauthTokens: OauthTokens | null;
12+
refreshToken: string;
13+
user: User;
514
}
615

716
export interface Impersonator {
@@ -14,7 +23,6 @@ export interface Session {
1423
refreshToken: string;
1524
user: User;
1625
impersonator?: Impersonator;
17-
oauthTokens?: OauthTokens;
1826
headers: Record<string, string>;
1927
}
2028

@@ -45,7 +53,6 @@ export interface AuthorizedData {
4553
permissions: string[];
4654
entitlements: string[];
4755
impersonator: Impersonator | null;
48-
oauthTokens: OauthTokens | null;
4956
sealedSession: string;
5057
}
5158

@@ -58,6 +65,5 @@ export interface UnauthorizedData {
5865
permissions: null;
5966
entitlements: null;
6067
impersonator: null;
61-
oauthTokens: null;
6268
sealedSession: null;
6369
}

src/session.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ async function updateSession(request: Request, debug: boolean) {
4242
refreshToken,
4343
user: session.user,
4444
impersonator: session.impersonator,
45-
oauthTokens: session.oauthTokens,
4645
headers: {},
4746
};
4847

@@ -130,7 +129,6 @@ async function authkitLoader<Data = unknown>(
130129
user: null,
131130
accessToken: null,
132131
impersonator: null,
133-
oauthTokens: null,
134132
organizationId: null,
135133
permissions: null,
136134
entitlements: null,
@@ -161,7 +159,6 @@ async function authkitLoader<Data = unknown>(
161159
permissions,
162160
entitlements,
163161
impersonator: session.impersonator ?? null,
164-
oauthTokens: session.oauthTokens ?? null,
165162
sealedSession: cookieSession.get('jwt'),
166163
};
167164

0 commit comments

Comments
 (0)