Skip to content

Commit

Permalink
add auth
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfdivyansh committed Nov 10, 2024
1 parent b49b450 commit e775aeb
Show file tree
Hide file tree
Showing 34 changed files with 2,944 additions and 163 deletions.
46 changes: 46 additions & 0 deletions apps/api/app/api/[[...route]]/hello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Hono } from "hono";
const app = new Hono();
import { prisma } from "@repo/db";
app
.get("/", async (c) => {
const user = await prisma.user.findMany();
return c.json({
user,
});
})
.patch(async (c) => {
const name = await c.req.json();
const test = await prisma.user.update({
where: {
id: "123",
},
data: {
name: name.name,
},
});
return c.json({
test,
});
})
.delete(async (c) => {
const test = await prisma.user.delete({
where: {
id: "2",
},
});
return c.json({
test,
});
})
.post(async (c) => {
const body = await c.req.json();
console.log(body);
const test = await prisma.user.create({
data: body,
});
return c.json({
test,
});
});

export default app;
62 changes: 62 additions & 0 deletions apps/api/app/api/[[...route]]/mail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Hono } from "hono";
import { sendBatchEmail, sendEmail } from "@repo/mail";
import { mailBatchSchema, mailSchema } from "@repo/types";
import { zValidator } from "@hono/zod-validator";

const app = new Hono();

app
.post("/send", zValidator("json", mailSchema), async (c) => {
const { email, subject } = c.req.valid("json");
const { data, error } = await sendEmail(email, subject);
if (error) {
return c.json(
{
message: "Email sent failed",
},
400,
);
}
return c.json(
{
message: "Email sent successfully",
data,
},
200,
);
})
.get((c) => {
return c.json({
message: "mail api is alive",
status: 200,
});
});

app
.post("/send-batch", zValidator("json", mailBatchSchema), async (c) => {
const { emails, subject } = c.req.valid("json");
const { data, error } = await sendBatchEmail(emails, subject);
if (error) {
return c.json(
{
message: "Email sent failed",
},
400,
);
}
return c.json(
{
message: "All Emails sent successfully",
data,
},
200,
);
})
.get((c) => {
return c.json({
message: "all mail api is alive",
status: 200,
});
});

export default app;
124 changes: 82 additions & 42 deletions apps/api/app/api/[[...route]]/route.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,103 @@
import { prisma } from "@repo/db";
import { Hono } from "hono";
import { handle } from "hono/vercel";
import { Hono } from "hono";
import { auth } from "@repo/auth";
import { cors } from "hono/cors";
import mail from "./mail";
import hello from "./hello";

const allowedOrigins = [
"http://localhost:3003",
"https://www.plura.pro",
"http://app.plura.pro",
];

export const runtime = "nodejs";

const app = new Hono().basePath("/api");
const app = new Hono<{
Variables: {
user: typeof auth.$Infer.Session.user | null;
session: typeof auth.$Infer.Session.session | null;
};
}>().basePath("/api");

app
.get("/hello", async (c) => {
const test = await prisma.user.findMany();
return c.json({
test,
});
})
.patch(async (c) => {
const name = await c.req.json();
const test = await prisma.user.update({
where: {
id: "123",
},
data: {
name: name.name,
},
});
return c.json({
test,
});
})
.delete(async (c) => {
const test = await prisma.user.delete({
where: {
id: "2",
app.use(
"/auth/**",
cors({
origin: allowedOrigins,
allowHeaders: ["Content-Type", "Authorization"],
allowMethods: ["POST", "GET", "OPTIONS"],
exposeHeaders: ["Content-Length"],
maxAge: 600,
credentials: true,
}),
);
app.options("/auth/**", (c) => {
const origin = c.req.raw.headers.get("origin") ?? "";

if (allowedOrigins.includes(origin)) {
return new Response(null, {
status: 204,
headers: {
"Access-Control-Allow-Origin": origin,
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Max-Age": "600",
},
});
return c.json({
test,
});
})
.post(async (c) => {
const body = await c.req.json();
console.log(body);
const test = await prisma.user.create({
data: body,
});
return c.json({
test,
});
}

return new Response("Forbidden", {
status: 403,
});
});
app.use("*", async (c, next) => {
const session = await auth.api.getSession({ headers: c.req.raw.headers });

if (!session) {
c.set("user", null);
c.set("session", null);
return next();
}

c.set("user", session.user);
c.set("session", session.session);
return next();
});

app.get("/health", async (c) => {
return c.json({
message: "i am alive",
status: 200,
});
});
app.get("/session", async (c) => {
const session = c.get("session");
const user = c.get("user");

if (!user) return c.body(null, 401);

return c.json({
session,
user,
});
});
app.route("/hello", hello);
app.route("/mail", mail);

app.on(["POST", "GET"], "/auth/**", (c) => {
return auth.handler(c.req.raw);
});
app.get("/multi-sessions", async (c) => {
const res = await auth.api.listDeviceSessions({
headers: c.req.raw.headers,
});
return c.json(res);
});
const GET = handle(app);
const POST = handle(app);
const PATCH = handle(app);
const DELETE = handle(app);
const OPTIONS = handle(app);

export { GET, PATCH, POST, DELETE };
export { GET, PATCH, POST, DELETE, OPTIONS };
8 changes: 6 additions & 2 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@
"format:check": "prettier --check \"**/*.{ts,tsx,mdx}\" --cache"
},
"dependencies": {
"@hono/node-server": "^1.13.5",
"@hono/zod-validator": "^0.4.1",
"@repo/auth": "workspace:*",
"@repo/db": "workspace:*",
"contentlayer2": "^0.5.3",
"hono": "^4.6.9",
"next": "15.0.2",
"react": "19.0.0-rc-02c0e824-20241028",
"react-dom": "19.0.0-rc-02c0e824-20241028"
"react-dom": "19.0.0-rc-02c0e824-20241028",
"zod": "^3.23.8"
},
"devDependencies": {
"@types/node": "18.11.18",
"@types/react": "18.0.26",
"@types/react-dom": "18.0.10",
"dotenv": "^16.4.5",
"typescript": "^5"
}
}
10 changes: 6 additions & 4 deletions apps/api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "NodeNext",
"moduleResolution": "NodeNext",
"module": "ESNext",
"moduleResolution": "Bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
Expand All @@ -34,8 +34,10 @@
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts"
],
".next/types/**/*.ts",
"src/**/*.ts",
"src/**/*.d.ts"
, "../../packages/types/auth.ts" ],
"exclude": [
"node_modules"
]
Expand Down
7 changes: 5 additions & 2 deletions apps/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@
"sonner": "^1.7.0",
"tailwind-merge": "^2.5.4",
"tailwindcss-animate": "^1.0.7",
"zod": "^3.23.8"
"zod": "^3.23.8",
"@repo/auth": "workspace:*",
"@repo/db": "workspace:*",
"@repo/types": "workspace:*"
},
"devDependencies": {
"@types/node": "^20",
Expand All @@ -60,4 +63,4 @@
"tailwindcss": "^3.4.1",
"typescript": "^5"
}
}
}
14 changes: 14 additions & 0 deletions apps/www/app/(auth)/dashboard/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import AccountSwitcher from "@/components/custom/account-switcher";
import { getMultipleSessions, getSession } from "@/lib/server";

export default async function page() {
const session = await getSession();
const multipleSessions = await getMultipleSessions();
return (
<div>
<AccountSwitcher session={multipleSessions} activeSession={session} />
<pre className="font-sm">{JSON.stringify(session, null, 1)}</pre>
<pre>{JSON.stringify(multipleSessions,null,2)}</pre>
</div>
);
}
9 changes: 9 additions & 0 deletions apps/www/app/(auth)/sign-in/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import SignInComponent from "@/components/custom/signInComponent";

export default function page() {
return (
<div className="w-full h-screen flex items-center justify-center">
<SignInComponent />
</div>
);
}
9 changes: 9 additions & 0 deletions apps/www/app/(auth)/sign-up/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import SignUpComponent from "@/components/custom/signUpComponent";

export default function page() {
return (
<div className="w-full h-screen flex items-center justify-center">
<SignUpComponent />
</div>
);
}
16 changes: 0 additions & 16 deletions apps/www/app/sign-in/[[...sign-in]]/layout.tsx

This file was deleted.

Loading

0 comments on commit e775aeb

Please sign in to comment.