Skip to content

Commit

Permalink
docs: readme update
Browse files Browse the repository at this point in the history
  • Loading branch information
rawnly committed Oct 18, 2023
1 parent e7267f1 commit 0b104dd
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 174 deletions.
176 changes: 89 additions & 87 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,46 +53,48 @@ import { NextResponse } from "next/server";

// the first matching middleware will be applied
export default handlePaths(
[
{
path: "/dashboard/:lang/:path*",
// We can filter this to apply only if some params matches exactly our needs
guard: (params) => params.lang === "en",
handler: async (req) => {
// url params are injected by `handlePaths`
// in addition to req.query
// this is done because you might want to handle paths
// that are not available under your `app` or `pages` directory.
console.log(req.params);

// do some checks
if (!isAuthenticated(req)) {
return NextResponse.redirect("/");
}

// continue the request
return NextResponse.next();
},
},
{
hostname: /^app\./,
// rewrites all routes on hostname app.* to the `pages/app/<path>`
handler: (req) =>
NextResponse.rewrite(new URL("/app${req.nextUrl.pathname}", req.url)),
},
[
{
path: "/dashboard/:lang/:path*",
// We can filter this to apply only if some params matches exactly our needs
guard: params => params.lang === "en",
handler: async req => {
// url params are injected by `handlePaths`
// in addition to req.query
// this is done because you might want to handle paths
// that are not available under your `app` or `pages` directory.
console.log(req.params);

// do some checks
if (!isAuthenticated(req)) {
return NextResponse.redirect("/");
}

// continue the request
return NextResponse.next();
},
},
{
hostname: /^app\./,
// rewrites all routes on hostname app.* to the `pages/app/<path>`
handler: req =>
NextResponse.rewrite(
new URL("/app${req.nextUrl.pathname}", req.url)
),
},
{
// easy syntax for redirects
path: "/blog/:slug/edit",
// params will be replaced automatically
redirectTo: "/dashboard/posts/:slug",
},
],
{
// easy syntax for redirects
path: "/blog/:slug/edit",
// params will be replaced automatically
redirectTo: "/dashboard/posts/:slug",
},
],
{
// inject custom data, like session etc
// it will be available inside `req.injected`
injector: async (req) => ({ isLoggedIn: !!req.session }),
debug: process.env.NODE_ENV !== "production",
}
// inject custom data, like session etc
// it will be available inside `req.ctx`
context: async req => ({ isLoggedIn: !!req.session }),
debug: process.env.NODE_ENV !== "production",
}
);
```

Expand All @@ -105,19 +107,20 @@ This can be easily achieved by passing an array of middleware as a handler. The
// middleware.ts

export default handlePaths([
{
hostname: /^app\./,
handler: [
{
path: "/",
handler: (req) => NextResponse.redirect(new URL("/dashboard", req.url)),
},
{
path: "/:path*",
handler: () => NextResponse.next(),
},
],
},
{
hostname: /^app\./,
handler: [
{
path: "/",
handler: req =>
NextResponse.redirect(new URL("/dashboard", req.url)),
},
{
path: "/:path*",
handler: () => NextResponse.next(),
},
],
},
]);
```

Expand All @@ -130,46 +133,45 @@ import { handlePaths, NextREquestWithParams } from "next-wayfinder";
import { createMiddlewareSupabaseClient } from "@supabase/auth-helpers-nextjs";

const getSession = async (req: NextRequestWithParams) => {
const res = NextResponse.next();
const supabase = createMiddlewareSupabaseClient({ req, res });
const res = NextResponse.next();
const supabase = createMiddlewareSupabaseClient({ req, res });

const {
data: { session },
error,
} = await supabase.auth.getSession();
const {
data: { session },
error,
} = await supabase.auth.getSession();

if (error) {
console.error(`Auth Error: `, error);
return null;
}
if (error) {
console.error(`Auth Error: `, error);
return null;
}

return session;
return session;
};

export default handlePaths(
[
[
{
// auth guard
path: "/dashboard/:path*",
pre: req =>
req.ctx?.session ? true : { redirectTo: "/auth/sign-in" },
handler: (req, res) => {
console.log("User authenticated: ", req.ctx?.session);

// do your stuff here
return res;
},
},
],
{
// auth guard
path: "/dashboard/:path*",
pre: (req) =>
req.injected?.session ? true : { redirectTo: "/auth/sign-in" },
handler: (req) => {
console.log("User authenticated: ", req.injected?.session);

// do your stuff here
// this injects `session` property into the request object
context: async req => {
const session = await getSession(req);

return NextResponse.next();
},
},
],
{
// this injects `session` property into the request object
injector: async (req) => {
const session = await getSession(req);

return { session };
},
}
return { session };
},
}
);
```

Expand All @@ -185,14 +187,14 @@ interface WayfinderOptions<T> {
*
* A function that returns the data to be injected into the request
*/
injector?: RequestInjector<T>;
context?: <T>((request: NextRequest) => T) | T;

/**
* Global middleware to be executed before all other middlewares
* Useful if you want to set a cookie or apply some logic before each request
* It receives the `options.response` (or `NextResponse.next()` if not provided) and `NextRequest` as params
*/
beforeAll?: BeforeAllMiddleware;
beforeAll?: (request: NextRequest, response: NextResponse) => Promise<NextResponse> | NextResponse;

/**
*
Expand All @@ -205,7 +207,7 @@ interface WayfinderOptions<T> {
* Useful when you want to chain other middlewares or return a custom response
* Default to `NextResponse.next()`
*/
response?: NextResponse;
response?: NextResponse | ((request: NextRequest) => NextResponse);
}
```

Expand Down
Loading

0 comments on commit 0b104dd

Please sign in to comment.