-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
a2177c8
commit a08279a
Showing
15 changed files
with
92 additions
and
205 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,12 +1,30 @@ | ||
"use server"; | ||
import axios from "axios"; | ||
import { hankoApiUrl } from "./vars"; | ||
import { createRemoteJWKSet, jwtVerify } from "jose"; | ||
import { NextRequest } from "next/server"; | ||
import { z } from "zod"; | ||
|
||
export const getUserId = async () => { | ||
// get user details from Hanko | ||
const { | ||
data: { id }, | ||
} = await axios.get<{ id: string }>(`${hankoApiUrl}/me`); | ||
export const fetchUserId = async (hanko: string) => { | ||
const JWKS = createRemoteJWKSet( | ||
new URL(`${hankoApiUrl}/.well-known/jwks.json`) | ||
); | ||
|
||
return id; | ||
const verifiedJWT = await jwtVerify(hanko ?? "", JWKS); | ||
return verifiedJWT.payload.sub; | ||
}; | ||
|
||
export const getUserId = async (req: NextRequest) => { | ||
// const userId = req.headers.get("x-user-id"); | ||
const hanko = req.cookies.get("hanko")?.value; | ||
if (!hanko) { | ||
throw new Error("No Hanko cookie found"); | ||
} | ||
const userId = await fetchUserId(hanko); | ||
// validate userId is uuid using zod | ||
const schema = z.string().uuid(); | ||
const validatedUserId = schema.safeParse(userId); | ||
if (!validatedUserId.success) { | ||
throw new Error("Invalid user id"); | ||
} | ||
return validatedUserId.data; | ||
}; |
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,31 @@ | ||
import { NextResponse, NextRequest } from "next/server"; | ||
|
||
import { fetchUserId, getUserId } from "@/components/auth/server"; | ||
|
||
export async function middleware(req: NextRequest) { | ||
try { | ||
const hanko = req.cookies.get("hanko")?.value; | ||
if (!hanko) { | ||
throw new Error("No Hanko cookie found"); | ||
} | ||
const userId = await fetchUserId(hanko); | ||
|
||
userId && req.headers.set("x-user-id", userId); | ||
return NextResponse.next(); | ||
} catch { | ||
if (req.url.startsWith("/api/secure")) { | ||
return NextResponse.json( | ||
{ | ||
message: "Unauthenticated", | ||
detail: "Please log in to access this resource.", | ||
}, | ||
{ status: 401 } | ||
); | ||
} | ||
return NextResponse.redirect(new URL("/login", req.url)); | ||
} | ||
} | ||
|
||
export const config = { | ||
matcher: ["/dashboard/:path*", "/api/secure/:path*"], | ||
}; |
Oops, something went wrong.