Skip to content

Commit

Permalink
refactor api
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfdivyansh committed Nov 16, 2024
1 parent 833de7a commit 012bb02
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 47 deletions.
11 changes: 11 additions & 0 deletions apps/api/app/api/[[...route]]/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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
34 changes: 10 additions & 24 deletions apps/api/app/api/[[...route]]/route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
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",
Expand All @@ -13,10 +15,10 @@ const allowedOrigins = [

export const runtime = "edge";

const app = new Hono<{
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,12 @@ 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.route("/auth", auth);

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);
Expand All @@ -83,3 +68,4 @@ const DELETE = handle(app);
const OPTIONS = handle(app);

export { GET, PATCH, POST, DELETE, OPTIONS };

22 changes: 22 additions & 0 deletions apps/api/app/api/[[...route]]/session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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
18 changes: 9 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 012bb02

Please sign in to comment.