Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions app/src/lib/auth/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ export function needsOnboarding(user: AuthenticatedUser): boolean {
export const authKeys = {
all: ["auth"] as const,
currentUser: () => [...authKeys.all, "current-user"] as const,
providers: () => [...authKeys.all, "providers"] as const,
/*
* Under `deployment`, not `auth`: what the sign-in screen offers is the deployment's, read as
* nobody, the same in every organization (query-client.ts). So it is neither dropped nor asked
* again when the sign-in screen forgets the organization the last person worked in.
*/
providers: () => ["deployment", "sign-in"] as const,
};

/** An identity provider this deployment can sign somebody in with. */
Expand All @@ -73,7 +78,11 @@ export type SignInOptions = {

async function signInOptions(): Promise<SignInOptions> {
// The API's `deployment`, which answers before anybody has signed in, and after in their own.
const body = await readDeployment({ fallback: "Could not load sign-in" });
// As nobody: this is what somebody signing in is offered, and just signed out, nobody is.
const body = await readDeployment({
fallback: "Could not load sign-in",
asNobody: true,
});
return {
providers: body.authProviders,
hosted: body.hostedSignIn === true,
Expand Down
13 changes: 11 additions & 2 deletions app/src/lib/deployment/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,21 @@ function showsOpenBotOnThisComputer(): Promise<boolean> {
* Inside OpenBot Desktop, on the OpenBot on this computer, it is asked with the page's cookies,
* which that OpenBot needs; the person is in `default` there. Told "not found" all the same, it is
* asked in one of their own.
*
* `asNobody` asks it as nobody whatever organization the app last knew: the sign-in screen's read.
* Just signed out, the app still knows the organization the person worked in (it is forgotten once
* the sign-in screen shows, never while their pages are still on screen), and asked there as
* somebody no longer signed in, the server answers that it can't find it.
*/
export async function readDeployment(options: {
export async function readDeployment({
asNobody = false,
...options
}: {
signal?: AbortSignal;
fallback: string;
asNobody?: boolean;
}): Promise<Deployment> {
const known = currentOrganizationId();
const known = asNobody ? undefined : currentOrganizationId();
if (known !== undefined) return getDeployment(known, options);
if (!(await showsOpenBotOnThisComputer()))
return getDeployment(DEPLOYMENT_ORGANIZATION, {
Expand Down
19 changes: 19 additions & 0 deletions app/src/query-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import {
currentOrganizationId,
onOrganizationChange,
setCurrentOrganization,
} from "@/lib/organizations/current";
import { inNoOrganization } from "@/lib/organizations/in-no-organization";

Expand Down Expand Up @@ -170,6 +171,24 @@ export const queryClient = new OpenBotQueryClient({

holdReadsWhileConnectionLost(queryClient);

/**
* Nobody is signed in: forget the organization the last person worked in, with everything the app
* held for it and their list of organizations, so the next person to sign in on this browser reads
* nothing the last one was shown, and nothing is asked in an organization nobody here is in.
*
* Only once no page of theirs is on screen (the sign-in screen calls it). Forgotten while their
* pages still show, those pages are drawn again in no organization and fail.
*/
export function forgetOrganization(): void {
if (currentOrganizationId() === undefined) return;
setCurrentOrganization(undefined);
queryClient.removeQueries({
predicate: (query) =>
belongsToOrganization(query.queryKey) ||
query.queryKey[0] === "organizations",
});
}

/**
* Moving from one organization to another drops everything the app held for the one left, in
* flight or answered, so a screen can only be drawn from what the new one answers. Becoming known
Expand Down
9 changes: 8 additions & 1 deletion app/src/routes/sign.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useQuery } from "@tanstack/react-query";
import { createFileRoute, redirect } from "@tanstack/react-router";
import { motion, useReducedMotion } from "motion/react";
import { useState } from "react";
import { useEffect, useState } from "react";
import AgentOrb from "@/components/agents/orb/agent-orb";
import { CopilotKitMark, ProviderLogo } from "@/components/auth/provider-logo";
import { Button, buttonVariants } from "@/components/ui/button";
Expand All @@ -18,6 +18,7 @@ import { appConfig } from "@/lib/generated/application-config";
import { inNoOrganization } from "@/lib/organizations/in-no-organization";
import { searchTextEntry } from "@/lib/search-text";
import { cn } from "@/lib/utils";
import { forgetOrganization } from "@/query-client";
import {
type AuthProviderId,
authProvidersQueryOptions,
Expand Down Expand Up @@ -87,6 +88,12 @@ function SignScreen() {
>(null);
const [error, setError] = useState<string | null>(null);
const { data: options } = useQuery(authProvidersQueryOptions());
/*
* Nobody is signed in here, so the organization the last person worked in is forgotten, with what
* the app held for it. Here, once the sign-in screen shows, never at Sign out itself: the pages of
* the app are still on screen then, and forgetting under them draws them again in no organization.
*/
useEffect(() => forgetOrganization(), []);
const providers = options?.providers ?? [];
const hosted = options?.hosted === true;
const [email, setEmail] = useState("");
Expand Down
48 changes: 48 additions & 0 deletions app/tests/connect-your-ai.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,26 @@ function organizationAi(personal: PersonalAiConnection) {
return GetAiConnectionResponse.parse(organization);
}

/** A refusal as the API sends one: problem details (api/openapi/common.yaml, `Problem`). */
function problem(
status: number,
code: string,
category: string,
detail: string,
) {
return {
type: `https://open.bot/errors/${code.toLowerCase().replaceAll("_", "-")}`,
title: detail,
status,
detail,
code,
category,
retryable: false,
requestId: "req_test",
traceId: "trace_test",
};
}

/** A second organization the person is in, beside `default`. */
const ACME = "/api/v2/organizations/acme";

Expand All @@ -157,6 +177,8 @@ class FakeServer {
signIns: unknown[] = [];
signInReads = 0;
signOuts = 0;
/** Whether the browser's session is signed in. Sign out ends it, as the server does. */
signedIn = true;

constructor(start: Start, onboarded = false) {
this.personal = personalFor(start);
Expand All @@ -173,6 +195,31 @@ class FakeServer {
const path = inAcme
? `${ORGANIZATION}${url.pathname.slice(ACME.length)}`
: url.pathname;
/*
* Signed out, as the real server answers: the deployment at `default` to anybody (the sign-in
* screen reads it), the deployment in any other organization not found, and everything else
* refused as unauthenticated.
*/
if (!this.signedIn && url.pathname !== `${ORGANIZATION}/deployment`)
return /^\/api\/v2\/organizations\/[^/]+\/deployment$/.test(url.pathname)
? {
status: 404,
json: problem(
404,
"ORGANIZATION_NOT_FOUND",
"not_found",
"Couldn't find that organization.",
),
}
: {
status: 401,
json: problem(
401,
"AUTH_UNAUTHENTICATED",
"auth",
"Authentication required.",
),
};
if (inAcme && this.acme && path === PERSONAL && method === "GET")
return { status: 200, json: this.acme };
if (inAcme && this.acme && path === `${ORGANIZATION}/ai-connection`)
Expand All @@ -197,6 +244,7 @@ class FakeServer {
}
if (url.pathname === "/api/auth/sign-out") {
this.signOuts += 1;
this.signedIn = false;
return { status: 200, json: { success: true } };
}
if (path === PERSONAL && method === "GET")
Expand Down
Loading