-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathwith-middleware.tsx
More file actions
38 lines (32 loc) · 1.01 KB
/
with-middleware.tsx
File metadata and controls
38 lines (32 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import type { Route } from './+types/with-middleware';
import type { User } from '../../context';
import { userContext } from '../../context';
import * as Sentry from '@sentry/react-router';
async function getUser() {
await new Promise(resolve => setTimeout(resolve, 500));
return {
id: '1',
name: 'Carlos Gomez',
};
}
const authMiddleware: Route.MiddlewareFunction = async ({ request, context }, next) => {
Sentry.startSpan({ name: 'authMiddleware', op: 'middleware.auth' }, async () => {
const user: User = await getUser();
context.set(userContext, user);
await next();
});
};
export const middleware: Route.MiddlewareFunction[] = [authMiddleware];
export const loader = async ({ context }: Route.LoaderArgs) => {
const user = context.get(userContext);
return { user };
};
export default function WithMiddlewarePage({ loaderData }: Route.ComponentProps) {
const { user } = loaderData;
return (
<div>
<h1>With Middleware Page</h1>
<p>User: {user?.name}</p>
</div>
);
}