Skip to content

Commit

Permalink
Merge pull request #55 from wtfdivyansh/feature/backend
Browse files Browse the repository at this point in the history
Feature/backend
  • Loading branch information
SkidGod4444 authored Nov 16, 2024
2 parents 98bc7c7 + fc0537d commit 0b93d50
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 57 deletions.
10 changes: 10 additions & 0 deletions apps/api/app/api/[[...route]]/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { auth } from "@repo/auth";
import { Hono } from "hono";

const app = new Hono();

app.on(["POST", "GET"], "/**", (c) => {
return auth.handler(c.req.raw);
});

export default app;
35 changes: 9 additions & 26 deletions apps/api/app/api/[[...route]]/route.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import { handle } from "hono/vercel";
import { Hono } from "hono";
import { auth } from "@repo/auth";
import { auth as Auth } from "@repo/auth";
import { cors } from "hono/cors";
import mail from "./mail";
import hello from "./hello";
import session from "./session";
import auth from "./auth";

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

export const runtime = "nodejs";
export const runtime = "edge";

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

Expand All @@ -32,7 +34,7 @@ app.use(
}),
);
app.use("*", async (c, next) => {
const session = await auth.api.getSession({ headers: c.req.raw.headers });
const session = await Auth.api.getSession({ headers: c.req.raw.headers });

if (!session) {
c.set("user", null);
Expand All @@ -52,29 +54,10 @@ app.get("/health", async (c) => {
});
});

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("/session", session);
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);
});
app.route("/auth", auth);

const GET = handle(app);
const POST = handle(app);
Expand Down
21 changes: 21 additions & 0 deletions apps/api/app/api/[[...route]]/session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Hono } from "hono";
import { auth } from "@repo/auth";
const app = new Hono();
app.get("/", async (c) => {
const session = await auth.api.getSession({ headers: c.req.raw.headers });

if (!session) return c.json({ message: "no session found" }, 401);

return c.json({
session,
});
});

app.get("/all", async (c) => {
const res = await auth.api.listDeviceSessions({
headers: c.req.raw.headers,
});
return c.json(res);
});

export default app;
26 changes: 13 additions & 13 deletions apps/www/app/auth/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -287,21 +287,21 @@ export default function Auth() {
</CardDescription>
</CardHeader>
<CardContent className="p-2">
<div className="flex flex-row items-center justify-between">
<Button>
<IconBrandGoogle className="size-5" /> Google
</Button>
<Button>
{" "}
<IconBrandGithub className="size-5" /> GitHub
</Button>
<Button>
{" "}
<IconBrandDiscord className="size-5" /> Discord
</Button>
</div>
<Form {...SignUpform}>
<form onSubmit={SignUpform.handleSubmit(handleSignUp)}>
<div className="flex flex-row items-center justify-between">
<Button>
<IconBrandGoogle className="size-5" /> Google
</Button>
<Button>
{" "}
<IconBrandGithub className="size-5" /> GitHub
</Button>
<Button>
{" "}
<IconBrandDiscord className="size-5" /> Discord
</Button>
</div>
<div className="bg-gradient-to-r from-transparent via-neutral-300 dark:via-neutral-700 to-transparent my-6 h-[1px] w-full" />
<div className="flex flex-col w-full gap-2 items-center justify-between">
<div className="flex flex-row items-start justify-between gap-2">
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {
"@repo/typescript-config": "workspace:*",
"@repo/db": "workspace:*",
"better-auth": "0.8.5-beta.2",
"better-auth": "0.8.6-beta.3",
"better-call": "0.2.14-beta.3",
"oslo": "^1.2.1"
}
Expand Down
5 changes: 3 additions & 2 deletions packages/database/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
"exports": {
".": "./src/index.ts"
},
"scripts": {
"scripts": {
"db:generate": "prisma generate",
"db:push": "prisma db push --skip-generate"
},
"dependencies": {
"@prisma/client": "^5.22.0"
"@prisma/client": "^5.22.0",
"@prisma/extension-accelerate": "^1.2.1"
},
"devDependencies": {
"prisma": "^5.22.0"
Expand Down
1 change: 1 addition & 0 deletions packages/database/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ generator client {
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("DIRECT_DATABASE_URL")
}

model User {
Expand Down
17 changes: 11 additions & 6 deletions packages/database/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { PrismaClient } from "@prisma/client";
import { PrismaClient } from "@prisma/client/edge";
import { withAccelerate } from "@prisma/extension-accelerate";

declare global {
var prisma: PrismaClient | undefined;
}
const prismaClientSingleton = () => {
return new PrismaClient().$extends(withAccelerate());
};

export const prisma = global.prisma || new PrismaClient();
declare const globalThis: {
prismaGlobal: ReturnType<typeof prismaClientSingleton>;
} & typeof global;

if (process.env.NODE_ENV !== "production") global.prisma = prisma;
export const prisma = globalThis.prismaGlobal ?? prismaClientSingleton();


if (process.env.NODE_ENV !== "production") globalThis.prismaGlobal = prisma;
export * from "@prisma/client";
31 changes: 22 additions & 9 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 0b93d50

Please sign in to comment.