export async function processSignin()

in gemini/autocal/frontend/libs/auth/auth.ts [62:111]


export async function processSignin(code: string): Promise<string> {
  const cookieStore = cookies();

  const { tokens } = await oAuth2Client.getToken(code);
  if (!tokens.id_token || !tokens.refresh_token || !tokens.access_token) {
    throw new Error("Invalid ID Token");
  }
  // Get the user details
  const ticket = await oAuth2Client.verifyIdToken({
    idToken: tokens.id_token,
  });
  const payload = ticket.getPayload();

  if (!payload || !payload.sub) {
    throw new Error("Unable to validate login token");
  }

  // Encrypt and store tokens in Firestore
  const [refresh, access] = await Promise.all([
    encrypt(tokens.refresh_token, process.env.ENCRYPTION_KEY!),
    encrypt(tokens.access_token, process.env.ENCRYPTION_KEY!),
  ]);

  const userRef = db.collection("users").doc(payload.sub);
  try {
    userRef.set(
      {
        refresh_token: refresh,
        access_token: access,
        expires: new Date(tokens.expiry_date || 0),
      },
      { merge: true },
    );
  } catch (e) {
    console.error(e);
    throw e;
  }

  // Store the ID token as a http only cookie (cannot be read by client-side JavaScript)
  (await cookieStore).set({
    name: "id_token",
    value: tokens.id_token,
    httpOnly: true,
    path: "/",
    secure: true,
    expires: new Date().setSeconds(payload.exp),
  });

  return tokens.id_token;
}