Skip to content

Commit 7f23644

Browse files
authored
feat: add react router v7 (#978)
fixed #964
1 parent 474fd95 commit 7f23644

File tree

67 files changed

+2329
-1874
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+2329
-1874
lines changed

.changeset/dry-hornets-perform.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@logto/react-router-sample": major
3+
"@logto/react-router": major
4+
---
5+
6+
add react-router SDK
7+
8+
Migrate from Remix to React Router, please refer to the [official React Router migration documentation](https://reactrouter.com/upgrading/remix).
9+
10+
And you can check the new sample project to see how to use the SDK.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.react-router

packages/remix-sample/README.md renamed to packages/react-router-sample/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Remix Sample
1+
# React Router Sample
22

3-
This is a sample project for Logto's Remix SDK.
3+
This is a sample project for Logto's React Router SDK.
44

55
## Configuration
66

packages/remix-sample/app/root.tsx renamed to packages/react-router-sample/app/root.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable consistent-default-export-name/default-export-match-filename */
2-
import { Links, Meta, Outlet, Scripts, ScrollRestoration } from '@remix-run/react';
2+
import { Links, Meta, Outlet, Scripts, ScrollRestoration } from 'react-router';
33

44
const App = () => {
55
return (
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { type RouteConfig } from '@react-router/dev/routes';
2+
import { flatRoutes } from '@react-router/fs-routes';
3+
4+
export default flatRoutes() satisfies RouteConfig;

packages/remix-sample/app/routes/_index.tsx renamed to packages/react-router-sample/app/routes/_index.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
/* eslint-disable consistent-default-export-name/default-export-match-filename */
2-
import { type LogtoContext } from '@logto/remix';
3-
import { type LoaderFunction } from '@remix-run/node';
4-
import { json, Link, useLoaderData } from '@remix-run/react';
2+
import { type LogtoContext } from '@logto/react-router';
3+
import { Link, type LoaderFunctionArgs } from 'react-router';
54

65
import { logto } from '../services/auth.server';
76

87
type LoaderResponse = {
98
readonly context: LogtoContext;
109
};
1110

12-
export const loader: LoaderFunction = async ({ request }) => {
11+
export const loader = async ({ request }: LoaderFunctionArgs) => {
1312
const context = await logto.getContext({ getAccessToken: false })(request);
1413

1514
// You can uncomment this to protect the route and
@@ -19,11 +18,11 @@ export const loader: LoaderFunction = async ({ request }) => {
1918
// return redirect('/api/logto/sign-in');
2019
// }
2120

22-
return json<LoaderResponse>({ context });
21+
return { context };
2322
};
2423

25-
const Home = () => {
26-
const { context } = useLoaderData<LoaderResponse>();
24+
const Home = ({ loaderData }: { readonly loaderData: LoaderResponse }) => {
25+
const { context } = loaderData;
2726
const { isAuthenticated, claims } = context;
2827

2928
return (

packages/remix-sample/app/routes/access-token.tsx renamed to packages/react-router-sample/app/routes/access-token.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
// eslint-disable-next-line consistent-default-export-name/default-export-match-filename
2-
import type { LogtoContext } from '@logto/remix';
3-
import { type LoaderFunction, json, redirect } from '@remix-run/node';
4-
import { useLoaderData } from '@remix-run/react';
2+
import type { LogtoContext } from '@logto/react-router';
3+
import { redirect, type LoaderFunctionArgs } from 'react-router';
54

65
import { logto } from '../services/auth.server';
76

87
type LoaderResponse = {
98
readonly context: LogtoContext;
109
};
1110

12-
export const loader: LoaderFunction = async ({ request }) => {
11+
export const loader = async ({ request }: LoaderFunctionArgs) => {
1312
const context = await logto.getContext({ getAccessToken: true })(request);
1413

1514
if (!context.isAuthenticated) {
@@ -19,13 +18,13 @@ export const loader: LoaderFunction = async ({ request }) => {
1918
// You can now use the access token here,
2019
// for demonstration purposes, we'll just pass it back to the client
2120

22-
return json<LoaderResponse>({ context });
21+
return { context };
2322
};
2423

25-
const AccessToken = () => {
24+
const AccessToken = ({ loaderData }: { readonly loaderData: LoaderResponse }) => {
2625
const {
2726
context: { accessToken },
28-
} = useLoaderData<LoaderResponse>();
27+
} = loaderData;
2928

3029
return (
3130
<div>
File renamed without changes.

packages/remix-sample/app/routes/user-info.tsx renamed to packages/react-router-sample/app/routes/user-info.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
// eslint-disable-next-line consistent-default-export-name/default-export-match-filename
22
import type { LogtoContext } from '@logto/remix';
3-
import { type LoaderFunction, json, redirect } from '@remix-run/node';
4-
import { useLoaderData } from '@remix-run/react';
3+
import { type LoaderFunctionArgs, redirect } from 'react-router';
54

65
import { logto } from '../services/auth.server';
76

87
type LoaderResponse = {
98
readonly context: LogtoContext;
109
};
1110

12-
export const loader: LoaderFunction = async ({ request }) => {
11+
export const loader = async ({ request }: LoaderFunctionArgs) => {
1312
// This will fetch the user info from Logto every time the route is loaded
1413
const context = await logto.getContext({ fetchUserInfo: true })(request);
1514

1615
if (!context.isAuthenticated) {
1716
return redirect('/api/logto/sign-in');
1817
}
1918

20-
return json<LoaderResponse>({ context });
19+
return { context };
2120
};
2221

23-
const UserInfo = () => {
22+
const UserInfo = ({ loaderData }: { readonly loaderData: LoaderResponse }) => {
2423
const {
2524
context: { userInfo },
26-
} = useLoaderData<LoaderResponse>();
25+
} = loaderData;
2726

2827
return (
2928
<div>

packages/remix-sample/app/services/auth.server.ts renamed to packages/react-router-sample/app/services/auth.server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { makeLogtoRemix } from '@logto/remix';
2-
import { createCookieSessionStorage } from '@remix-run/node';
1+
import { makeLogtoReactRouter } from '@logto/react-router';
2+
import { createCookieSessionStorage } from 'react-router';
33

44
const sessionStorage = createCookieSessionStorage({
55
cookie: {
@@ -14,7 +14,7 @@ if (!process.env.LOGTO_ENDPOINT || !process.env.LOGTO_APP_ID || !process.env.LOG
1414
throw new Error('Missing Logto environment variables');
1515
}
1616

17-
export const logto = makeLogtoRemix(
17+
export const logto = makeLogtoReactRouter(
1818
{
1919
endpoint: process.env.LOGTO_ENDPOINT,
2020
appId: process.env.LOGTO_APP_ID,

0 commit comments

Comments
 (0)