generated from taylorbryant/gatsby-starter-tailwind
-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: run import ticks on Next server (#1218)
- Loading branch information
Showing
3 changed files
with
79 additions
and
75 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { getServerSession } from 'next-auth' | ||
import { NextRequest, NextResponse } from 'next/server' | ||
import { authOptions } from 'pages/api/auth/[...nextauth]' | ||
|
||
type Next13APIHandler = (req: NextRequest) => Promise<any> | ||
|
||
/* | ||
* A high-order function to protect Next 13 (and later) API route | ||
* by checking that the user has a valid session. | ||
*/ | ||
export const withUserAuth = (handler: Next13APIHandler): Next13APIHandler => { | ||
return async (req: NextRequest) => { | ||
const session = await getServerSession({ req, ...authOptions }) | ||
if (session != null) { | ||
// Passing useful session data downstream | ||
req.headers.set('x-openbeta-user-uuid', session.user.metadata.uuid) | ||
req.headers.set('x-auth0-userid', session.id) | ||
return await handler(req) | ||
} else { | ||
return NextResponse.json({ status: 401 }) | ||
} | ||
} | ||
} |