add users to loops post google sign in (#194)

This commit is contained in:
Ben Senescu 2026-05-13 21:44:34 -04:00 committed by GitHub
parent 701ace3dff
commit f926a0f78c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -56,22 +56,7 @@ function createAuth() {
});
},
afterEmailVerification: async (user) => {
try {
await upsertHostedVerifiedContact({
userId: user.id,
email: user.email,
name: user.name,
});
} catch (error) {
console.error(
"Failed to sync Loops profile after verification:",
{
userId: user.id,
email: user.email,
error,
},
);
}
await syncHostedVerifiedContact(user, "email verification");
},
},
socialProviders: getSocialProviders(),
@ -81,6 +66,17 @@ function createAuth() {
}),
plugins: [...baseAuthConfig.plugins, tanstackStartCookies()],
databaseHooks: {
user: {
create: {
after: async (user, context) => {
if (context?.path !== "/callback/google") {
return;
}
await syncHostedVerifiedContact(user, "Google sign up");
},
},
},
session: {
create: {
before: async (session) => {
@ -108,6 +104,25 @@ function createAuth() {
let authInstance: ReturnType<typeof createAuth> | null = null;
async function syncHostedVerifiedContact(
user: { id: string; email: string; name?: string | null },
context: string,
) {
try {
await upsertHostedVerifiedContact({
userId: user.id,
email: user.email,
name: user.name,
});
} catch (error) {
console.error(`Failed to sync Loops profile after ${context}:`, {
userId: user.id,
email: user.email,
error,
});
}
}
function getTrustedOrigins(baseUrl: string) {
const trustedOrigins = [baseUrl];
@ -148,21 +163,29 @@ function getHostedSecret() {
}
function getSocialProviders() {
return {
google: getGoogleSocialProviderConfig(),
};
}
function getGoogleSocialProviderConfig() {
const googleClientId = env.GOOGLE_CLIENT_ID?.trim();
const googleClientSecret = env.GOOGLE_CLIENT_SECRET?.trim();
if (!googleClientId || !googleClientSecret) {
return undefined;
if (!googleClientId) {
throw new Error("GOOGLE_CLIENT_ID is required in hosted mode");
}
if (!googleClientSecret) {
throw new Error("GOOGLE_CLIENT_SECRET is required in hosted mode");
}
return {
google: {
clientId: googleClientId,
clientSecret: googleClientSecret,
mapProfileToUser: (profile: { name?: string }) => ({
name: profile.name,
}),
},
};
}
@ -183,6 +206,7 @@ export function hasHostedAuthConfig() {
try {
getHostedBaseUrl();
getHostedSecret();
getGoogleSocialProviderConfig();
return (
Reflect.get(env, "BYPASS_EMAIL_VERIFICATION") === "true" ||
hasHostedAuthEmailConfig()