Skip to content

Commit 76aeee1

Browse files
committed
auth setup
1 parent 5bc8281 commit 76aeee1

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

backend/src/index.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import Google from "@auth/core/providers/google";
2+
import { accounts, sessions, verificationTokens } from "./db/schema/auth";
13
import { cors } from "hono/cors";
24
import { db } from "./db/index";
5+
import { DrizzleAdapter } from "@auth/drizzle-adapter";
36
import { env } from "./env/env";
47
import { Hono } from "hono";
8+
import { initAuthConfig, verifyAuth, authHandler } from "@hono/auth-js";
59
import { users } from "./db/schema/users";
610

711
const app = new Hono<{ Variables: { db: typeof db } }>();
@@ -19,10 +23,35 @@ app.use(
1923
allowHeaders: ["Content-Type", "Authorization"],
2024
allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
2125
credentials: true,
22-
origin: "*",
23-
}),
26+
origin: "*", // TODO: Update me!
27+
})
2428
);
2529

30+
// Initialize auth config
31+
app.use(
32+
"*",
33+
initAuthConfig(() => ({
34+
adapter: DrizzleAdapter(db, {
35+
accountsTable: accounts,
36+
sessionsTable: sessions,
37+
usersTable: users,
38+
verificationTokensTable: verificationTokens,
39+
}),
40+
// ? - Manually add new providers here
41+
providers: [
42+
Google({
43+
clientId: env.GOOGLE_CLIENT_ID,
44+
clientSecret: env.GOOGLE_CLIENT_SECRET,
45+
}),
46+
],
47+
secret: env.AUTH_SECRET,
48+
session: { strategy: "database" },
49+
}))
50+
);
51+
52+
// Set up protected routes
53+
app.use("/api/protected/*", verifyAuth());
54+
2655
app.get("/", (c) => {
2756
return c.json({ message: "Hello from Hono!" });
2857
});

0 commit comments

Comments
 (0)