Skip to content

Commit df44d20

Browse files
committed
fix: add some example code
1 parent f427fe6 commit df44d20

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

src/content/docs/developer-tools/sdks/backend/remix-sdk.mdx

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,27 @@ import { Link } from "@remix-run/react";
182182
</Link>
183183
```
184184

185+
### Redirecting after authentication
186+
187+
**Static redirect**
188+
189+
Set `KINDE_POST_LOGIN_REDIRECT_URL` in `.env` to send users to a fixed page after login.
190+
191+
**Dynamic redirect**
192+
193+
Append `post_login_redirect_url` or `returnTo` to the login/register URL for per-request redirects.
194+
195+
```jsx
196+
<Link
197+
to={{
198+
pathname: "/kinde-auth/login",
199+
search: "?post_login_redirect_url=/dashboard"
200+
}}
201+
>
202+
Login
203+
</Link>
204+
```
205+
185206
**Sign into organizations**
186207

187208
To log into specific organizations you can specify the `org_code` in the search params.
@@ -340,6 +361,35 @@ export default function Dashboard() {
340361

341362
Use the loader example above to block unauthenticated users. To automatically redirect back to the requested route after login, include `returnTo` in the login/register link.
342363

364+
```tsx
365+
// app/routes/protected.tsx
366+
import { json, LoaderFunctionArgs, redirect } from "@remix-run/node";
367+
import { useLoaderData, Link } from "@remix-run/react";
368+
import { getKindeSession } from "@kinde-oss/kinde-remix-sdk";
369+
370+
export const loader = async ({ request }: LoaderFunctionArgs) => {
371+
const { getUser, headers, isAuthenticated } = await getKindeSession(request);
372+
373+
if (!(await isAuthenticated())) {
374+
throw redirect("/kinde-auth/login?returnTo=/protected");
375+
}
376+
377+
const user = await getUser();
378+
return json({ user }, { headers });
379+
};
380+
381+
export default function Protected() {
382+
const { user } = useLoaderData<typeof loader>();
383+
return (
384+
<div>
385+
<p>This page is protected.</p>
386+
<p>Welcome {user?.given_name}</p>
387+
<Link to="/kinde-auth/logout">Logout</Link>
388+
</div>
389+
);
390+
}
391+
```
392+
343393
## Refreshing Kinde data
344394

345395
Pass the `headers` returned from `getKindeSession` in your loader/action responses so refresh tokens rotate automatically. You can also call `refreshTokens()` manually when you need the latest data (for example after a mutation).
@@ -357,6 +407,15 @@ export const action = async ({request}: ActionFunctionArgs) => {
357407
const headers = await refreshTokens();
358408
return redirect("/profile", {headers});
359409
};
410+
411+
// Refresh after an update
412+
export const updateProfile = async ({request}: ActionFunctionArgs) => {
413+
const { refreshTokens, getUser } = await getKindeSession(request);
414+
// ...perform your mutation here...
415+
const headers = await refreshTokens();
416+
const user = await getUser();
417+
return json({ user }, { headers });
418+
};
360419
```
361420

362421
## Kinde Management API
@@ -385,6 +444,20 @@ export const loader = async ({ request }: LoaderFunctionArgs) => {
385444
- To log users into a specific organization, set `org_code` on your login/register links (as shown above).
386445
- To create an organization from your app, link to `/kinde-auth/create_org` or use the Management API.
387446

447+
```tsx
448+
// Create org via Kinde Auth route
449+
<Link
450+
to={{
451+
pathname: "/kinde-auth/create_org",
452+
search: "?org_name=Hurlstone"
453+
}}
454+
>
455+
Create org
456+
</Link>
457+
```
458+
459+
If `org_code` is not specified and the user belongs to multiple organizations, they’ll be prompted to choose during login/registration.
460+
388461
## Self Serve Portal
389462

390463
Send users to the self-serve portal by linking to the portal URL returned from the [Self-serve portal API](/build/self-service-portal/self-serve-portal-for-orgs/) or by using a stored URL in your app.
@@ -393,6 +466,29 @@ Send users to the self-serve portal by linking to the portal URL returned from t
393466
<Link to="https://<your-kinde-subdomain>.kinde.com/portal">Open portal</Link>
394467
```
395468

469+
### subNav
470+
471+
Use `subNav` to land users on a specific portal area.
472+
473+
```tsx
474+
import { Link } from "@remix-run/react";
475+
import { PortalPage } from "@kinde/js-utils";
476+
477+
<Link to={`https://<your-kinde-subdomain>.kinde.com/portal?subNav=${PortalPage.organizationPaymentDetails}`}>
478+
Manage billing
479+
</Link>;
480+
```
481+
482+
### returnUrl
483+
484+
Send users back to your app after portal actions with an absolute `returnUrl`.
485+
486+
```tsx
487+
<Link to="https://<your-kinde-subdomain>.kinde.com/portal?returnUrl=https://app.example.com/settings">
488+
Open portal
489+
</Link>
490+
```
491+
396492
## Analytics
397493

398494
Attach UTM parameters to your auth links to track traffic sources.
@@ -427,6 +523,11 @@ Include `lang` in the search params when sending users to login or register.
427523

428524
An `audience` is the intended recipient of an access token. Set `KINDE_AUDIENCE` in `.env` to populate the `aud` claim.
429525

526+
```shell
527+
KINDE_AUDIENCE=<your-api>
528+
KINDE_AUDIENCE=<api-one> <api-two> # multiple audiences (space separated)
529+
```
530+
430531
## Working with subdomains
431532

432533
If you use a custom domain and start auth on `auth.mysite.com` but redirect back to `app.mysite.com`, set `KINDE_COOKIE_DOMAIN=.mysite.com` so cookies are available across subdomains.
@@ -439,6 +540,15 @@ KINDE_COOKIE_DOMAIN=.mysite.com
439540

440541
When deploying to environments with dynamic URLs (for example Vercel previews), set `KINDE_SITE_URL`, `KINDE_POST_LOGOUT_REDIRECT_URL`, and `KINDE_POST_LOGIN_REDIRECT_URL` at build time to the preview URL. Wildcards are supported in Kinde callback/logout URLs if you prefer a simpler setup.
441542

543+
```bash
544+
# example in a Remix deploy script
545+
export KINDE_SITE_URL="https://${VERCEL_URL}"
546+
export KINDE_POST_LOGOUT_REDIRECT_URL="https://${VERCEL_URL}"
547+
export KINDE_POST_LOGIN_REDIRECT_URL="https://${VERCEL_URL}/dashboard"
548+
```
549+
550+
To keep Kinde allowlists up to date, you can use an M2M token with the Kinde Management API to add the preview callback/logout URLs during your deploy step (similar to the Next.js example).
551+
442552
## Health check
443553

444554
`/kinde-auth/health` exposes your configuration. The client secret is masked (only indicates if it is set correctly).

0 commit comments

Comments
 (0)