MCPcopy Create free account
hub / github.com/pollinations/pollinations / onAfterSessionCreate

Function onAfterSessionCreate

enter.pollinations.ai/src/auth.ts:139–202  ·  view source on GitHub ↗

* Sync github_username on every login. * GitHub usernames are mutable — users can rename their account. * We fetch the current username from GitHub API using the immutable github_id * and update D1 if it changed. Non-blocking via waitUntil. * * GitHub is the only auth provider, so every user ro

(
    env: Cloudflare.Env,
    executionCtx?: ExecutionContext,
)

Source from the content-addressed store, hash-verified

137 * the sync defensively if it is ever missing.
138 */
139function onAfterSessionCreate(
140 env: Cloudflare.Env,
141 executionCtx?: ExecutionContext,
142) {
143 return async (
144 session: { userId: string },
145 _ctx?: GenericEndpointContext | null,
146 ) => {
147 executionCtx?.waitUntil(
148 (async () => {
149 try {
150 const db = drizzle(env.DB);
151 const [user] = await db
152 .select({
153 githubId: userTable.githubId,
154 githubUsername: userTable.githubUsername,
155 })
156 .from(userTable)
157 .where(eq(userTable.id, session.userId))
158 .limit(1);
159
160 const githubId = user?.githubId;
161 if (!githubId) return;
162
163 const headers: Record<string, string> = {
164 Accept: "application/vnd.github+json",
165 "User-Agent": "pollinations-enter",
166 };
167 // Use OAuth app credentials for 5,000 req/hr (vs 60 unauthenticated)
168 if (env.GITHUB_CLIENT_ID && env.GITHUB_CLIENT_SECRET) {
169 headers.Authorization = `Basic ${btoa(`${env.GITHUB_CLIENT_ID}:${env.GITHUB_CLIENT_SECRET}`)}`;
170 }
171 const res = await fetch(
172 `https://api.github.com/user/${githubId}`,
173 { headers },
174 );
175 if (!res.ok) {
176 console.error(
177 `[username-sync] GitHub API ${res.status} for user ${githubId}`,
178 );
179 return;
180 }
181
182 const profile = (await res.json()) as { login: string };
183 if (
184 profile.login &&
185 profile.login !== user?.githubUsername
186 ) {
187 await db
188 .update(userTable)
189 .set({ githubUsername: profile.login })
190 .where(eq(userTable.id, session.userId));
191 }
192 } catch (e) {
193 console.error(
194 "[username-sync] failed for session",
195 session.userId,
196 e,

Callers 1

tierPluginFunction · 0.85

Calls 7

limitMethod · 0.80
errorMethod · 0.80
jsonMethod · 0.80
updateMethod · 0.80
waitUntilMethod · 0.65
setMethod · 0.65
fetchFunction · 0.50

Tested by

no test coverage detected