-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #179 from wtfdivyansh/main
feat(onboarding): add middleware
- Loading branch information
Showing
3 changed files
with
58 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { betterFetch } from "@better-fetch/fetch"; | ||
import type { Session } from "@plura/auth"; | ||
import { NextResponse, type NextRequest } from "next/server"; | ||
const baseDomain = | ||
process.env.NODE_ENV === "production" | ||
? "https://api.plura.pro" | ||
: "http://localhost:3001"; | ||
const redirectUrl = | ||
process.env.NODE_ENV === "production" | ||
? "https://www.plura.pro/auth" | ||
: "http://localhost:3003/auth"; | ||
const appDomain = | ||
process.env.NODE_ENV === "production" | ||
? "https://app.plura.pro" | ||
: "http://localhost:3002"; | ||
|
||
export default async function authMiddleware(request: NextRequest) { | ||
const { data: session } = await betterFetch<Session>( | ||
`${baseDomain}/v1/auth/get-session`, | ||
{ | ||
baseURL: request.nextUrl.origin, | ||
headers: { | ||
cookie: request.headers.get("cookie") || "", | ||
}, | ||
} | ||
); | ||
|
||
if (!session) { | ||
console.log("redirecting to sign in"); | ||
return NextResponse.redirect(redirectUrl); | ||
} | ||
const currentPath = request.nextUrl.pathname; | ||
|
||
if (!currentPath.startsWith("/onboarding") && !session.user.isOnboarding) { | ||
return NextResponse.redirect(`${appDomain}/onboarding`); | ||
} | ||
if (currentPath.startsWith("/onboarding") && session.user.isOnboarding) { | ||
return NextResponse.redirect(`${appDomain}/home`); | ||
} | ||
|
||
return NextResponse.next(); | ||
} | ||
|
||
export const config = { | ||
matcher: [ | ||
"/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)", | ||
], | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.