swadhinbiswas feat: OpenCodeHub v1.1.0 - Enterprise Features & Storage Adapters · 2/15/2026
719a1cb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { google } from "@/lib/oauth";
import { OAuth2RequestError } from "arctic";
import type { APIRoute } from "astro";
import { getDatabase, schema } from "@/db";
import { createSession } from "@/lib/auth";
import { logger } from "@/lib/logger";
import { and, eq } from "drizzle-orm";
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
interface GoogleUser {
sub: string;
name: string;
picture: string;
email: string;
email_verified: boolean;
}
export const GET: APIRoute = async ({ url, cookies, redirect }) => {
const code = url.searchParams.get("code");
const state = url.searchParams.get("state");
const storedState = cookies.get("google_oauth_state")?.value;
const codeVerifier = cookies.get("google_oauth_verifier")?.value;
// Clear state cookies
cookies.delete("google_oauth_state", { path: "/" });
cookies.delete("google_oauth_verifier", { path: "/" });
if (!code || !state || !storedState || !codeVerifier || state !== storedState) {
return redirect("/login?error=oauth_failed");
}
try {
const tokens = await google.validateAuthorizationCode(code, codeVerifier);
const accessToken = tokens.accessToken();
const response = await fetch("https://openidconnect.googleapis.com/v1/userinfo", {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
const googleUser: GoogleUser = await response.json();
const db = getDatabase() as NodePgDatabase<typeof schema>;
// Check if OAuth account exists
const existingOAuth = await db.query.oauthAccounts.findFirst({
where: and(
eq(schema.oauthAccounts.provider, "google"),
eq(schema.oauthAccounts.providerAccountId, googleUser.sub)
),
with: {
user: true,
},
});
let userId: string;
if (existingOAuth) {
await db
.update(schema.oauthAccounts)
.set({
accessToken,
})
.where(eq(schema.oauthAccounts.id, existingOAuth.id));
userId = existingOAuth.userId;
} else {
// Check email
const user = await db.query.users.findFirst({
where: eq(schema.users.email, googleUser.email),
});
if (user) {
// Link
await db.insert(schema.oauthAccounts).values({
id: crypto.randomUUID(),
userId: user.id,
provider: "google",
providerAccountId: googleUser.sub,
accessToken,
createdAt: new Date(),
});
userId = user.id;
} else {
// Create user
// Google doesn't give a "username", so we make one from email or name
let baseUsername = googleUser.email.split("@")[0];
let username = baseUsername;
const existingUsername = await db.query.users.findFirst({
where: eq(schema.users.username, username),
});
if (existingUsername) {
username = `${baseUsername}-${crypto.randomUUID().slice(0, 8)}`;
}
const newUserId = crypto.randomUUID();
await db.insert(schema.users).values({
id: newUserId,
username,
email: googleUser.email,
displayName: googleUser.name,
avatarUrl: googleUser.picture,
emailVerified: googleUser.email_verified,
createdAt: new Date(),
updatedAt: new Date(),
});
await db.insert(schema.oauthAccounts).values({
id: crypto.randomUUID(),
userId: newUserId,
provider: "google",
providerAccountId: googleUser.sub,
accessToken,
createdAt: new Date(),
});
userId = newUserId;
}
}
const session = await createSession(userId);
cookies.set("och_session", session.token, {
httpOnly: true,
secure: import.meta.env.PROD,
sameSite: "lax",
path: "/",
maxAge: 60 * 60 * 24 * 7, // 7 days
});
return redirect("/");
} catch (error) {
if (error instanceof OAuth2RequestError) {
return new Response(null, {
status: 400
});
}
logger.error({ error }, "Google OAuth error");
return redirect("/login?error=oauth_failed");
}
};