Skip to content

Commit

Permalink
Merge pull request #179 from wtfdivyansh/main
Browse files Browse the repository at this point in the history
feat(onboarding): add middleware
  • Loading branch information
SkidGod4444 authored Jan 10, 2025
2 parents b462b90 + b2086fe commit 0dcc47c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 43 deletions.
6 changes: 2 additions & 4 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@
"@plura/crypt": "workspace:*",
"@plura/db": "workspace:*",
"@plura/mail": "workspace:*",
"@plura/mor": "workspace:*",
"@prisma/client": "^5.22.0",
"@repo/types": "workspace:*",
"@plura/mor":"workspace:*",
"@upstash/ratelimit": "^2.0.5",
"@upstash/redis": "^1.34.3",
"hono": "^4.6.9",

"hono": "^4.6.16",
"nanoid": "^5.0.9",
"next": "15.1.4",

"prisma": "^5.22.0",
"react": "19.0.0-rc-02c0e824-20241028",
"react-dom": "19.0.0-rc-02c0e824-20241028",
Expand Down
48 changes: 48 additions & 0 deletions apps/app/middleware.ts
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).*)",
],
};
47 changes: 8 additions & 39 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0dcc47c

Please sign in to comment.