-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
225 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
### Example - Filter by request method | ||
|
||
This examples shows you how you can filter by request method | ||
|
||
```ts filename="middleware.ts" copy showLineNumbers | ||
import { NextResponse, NextRequest } from "next/server"; | ||
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 { | ||
data: { session }, | ||
error, | ||
} = await supabase.auth.getSession(); | ||
|
||
if (error) { | ||
console.error(`Auth Error: `, error); | ||
return null; | ||
} | ||
|
||
return session; | ||
}; | ||
|
||
export default handlePaths( | ||
[ | ||
{ | ||
// do not check auth on GET requests | ||
path: "/events/:id", | ||
method: "GET", | ||
handler: (_, res) => res, | ||
}, | ||
{ | ||
// auth guard on PATCH | ||
path: "/events/:id", | ||
method: "PATCH", | ||
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; | ||
}, | ||
}, | ||
], | ||
{ | ||
// this injects `session` property into the request object | ||
context: async req => { | ||
const session = await getSession(req); | ||
|
||
return { session }; | ||
}, | ||
} | ||
); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.