Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

3.3 KB
Raw Blame
import { logger } from '@librechat/data-schemas';
import { ErrorTypes } from 'librechat-data-provider';
import type { AppConfig, UserRecord, NewUserData } from '@librechat/data-schemas';
import type {
  GetAppConfig,
  GetBalanceConfig,
  FindUserByFields,
  FindBalanceByUser,
  CreateUserIfAbsent,
  LoginUserResolution,
} from './provision';
import { createUserOnce } from './provision';

export type SocialUserLookup = {
  findUser: FindUserByFields;
  provider: string;
  providerId?: string;
  email?: string;
};

/** Finds a social login's account by provider id, then by email, whichever provider owns it. */
export async function findSocialUser({
  findUser,
  provider,
  providerId,
  email,
}: SocialUserLookup): Promise<UserRecord | null> {
  const providerKey = `${provider}Id`;
  if (providerId && typeof providerId === 'string') {
    const user = await findUser({ [providerKey]: providerId });
    if (user) return user;
  }

  const user = await findUser({ email: email?.trim() });
  if (user) {
    logger.warn(`[${provider}Login] User found by email: ${email} but not by ${providerKey}`);
  }
  return user;
}

/** The account a social first login recovers: an account owned by another provider fails it. */
export async function resolveSocialUser(lookup: SocialUserLookup): Promise<LoginUserResolution> {
  const user = await findSocialUser(lookup);
  if (!user || user.provider === lookup.provider) return { user, error: null };

  logger.info(
    `[${lookup.provider}Login] User ${lookup.email} already exists with provider ${user.provider}`,
  );
  return { user: null, error: ErrorTypes.AUTH_FAILED };
}

/**
 * Creates a social first-login user through `createUserOnce`, which admits it under the config of
 * the role it is created with. A new account goes through `finishNewUser` with that config; an
 * account a concurrent first login created is admitted like a found account and goes through
 * `refreshExistingUser` with the config its login continues under, as any account the login
 * finds does. A failure throws the coded `AUTH_FAILED` error `socialLogin` hands to its passport
 * callback, with the reason (`AUTH_FAILED` or the email-domain policy) as its message.
 */
export async function provisionSocialUser({
  lookup,
  newUser,
  getAppConfig,
  getBalanceConfig,
  createUserIfAbsent,
  findBalanceByUser,
  finishNewUser,
  refreshExistingUser,
}: {
  lookup: SocialUserLookup;
  newUser: NewUserData;
  getAppConfig: GetAppConfig;
  getBalanceConfig: GetBalanceConfig;
  createUserIfAbsent: CreateUserIfAbsent;
  findBalanceByUser: FindBalanceByUser;
  finishNewUser: (user: UserRecord, appConfig: AppConfig) => Promise<UserRecord>;
  refreshExistingUser: (user: UserRecord, appConfig: AppConfig) => Promise<void>;
}): Promise<UserRecord> {
  const result = await createUserOnce({
    newUser,
    email: newUser.email,
    getAppConfig,
    getBalanceConfig,
    strategyName: `${lookup.provider}Login`,
    lookup: () => resolveSocialUser(lookup),
    createUserIfAbsent,
    findBalanceByUser,
  });
  if (result.error !== null) {
    throw Object.assign(new Error(result.error), { code: ErrorTypes.AUTH_FAILED });
  }
  if (result.created) return finishNewUser(result.user, result.appConfig);

  await refreshExistingUser(result.user, result.appConfig);
  return result.user;
}