@codesandbox/api
Version:
The CodeSandbox API
106 lines (105 loc) • 3.54 kB
TypeScript
import type { PickState } from "class-states";
import { States } from "class-states";
import type { AuthenticationProvider, User } from "./RESTTypes";
import type { RESTRequest } from "./types";
export type SessionApiOptions = {
apiRequest: RESTRequest;
rootRequest: RESTRequest;
rootApiRequest: RESTRequest;
baseUrl: string;
apiVersion: string;
useCliAuthentication: boolean;
user?: User;
};
export type SessionState = {
state: "UNAUTHENTICATED";
error?: string;
transitionState?: {
state: "AUTHENTICATING";
transition: Promise<PickState<SessionState, "AUTHENTICATED" | "UNAUTHENTICATED">>;
} | {
state: "SIGNING_IN";
transition: Promise<PickState<SessionState, "AUTHENTICATED" | "UNAUTHENTICATED">>;
};
} | {
state: "AUTHENTICATED";
user: User;
transitionState?: {
state: "SIGNING_OUT";
transition: Promise<PickState<SessionState, "AUTHENTICATED" | "UNAUTHENTICATED">>;
} | {
state: "SIGNING_IN";
transition: Promise<PickState<SessionState, "AUTHENTICATED" | "UNAUTHENTICATED">>;
};
};
export declare class SessionApi {
state: States<SessionState>;
private apiRequest;
private rootApiRequest;
private rootRequest;
onSessionChange: (cb: (evt: {
session: SessionState;
prevSession: SessionState;
}) => void) => () => void;
/**
* A user can log in using a local JWT stored in a cookie or local storage. This token is sent as a
* bearer token on API calls. The API needs to be configured with "useCliAuthentication" to take advantage of this
*/
bearerToken: string | undefined;
private baseUrl;
private cookies;
private useCliAuthentication;
constructor(options: SessionApiOptions);
/**
* This is only used for the development flow, we NEVER set this cookie outside the context
* of doing a development sign in
*/
private setDevBearerToken;
private clearSignedIn;
/**
* Indicates if the user has signed in and we should authenticate
*/
private hasSignedIn;
private authenticate;
private getPendingUser;
private finalizeSignUp;
private showProviderPopup;
private signInWithPopup;
/**
* If we receive an unauthorized response we want to use that information to ensure you are UNAUTHENTICATED
*/
unauthorizedResponseReceived(): Promise<void>;
authenticateWithJwt(jwt: string): Promise<{
state: "UNAUTHENTICATED";
error?: string | undefined;
transitionState?: {
state: "AUTHENTICATING";
transition: Promise<SessionState>;
} | {
state: "SIGNING_IN";
transition: Promise<SessionState>;
} | undefined;
} | {
state: "AUTHENTICATED";
user: User;
transitionState?: {
state: "SIGNING_OUT";
transition: Promise<SessionState>;
} | {
state: "SIGNING_IN";
transition: Promise<SessionState>;
} | undefined;
} | {
state: "AUTHENTICATED";
user: User;
}>;
signIn(provider?: AuthenticationProvider): Promise<User | null>;
signOut(): Promise<void>;
/**
* Used for requests that can not rely on the HTTP only cookie, for example GQL subscription requests
*/
getGuardianToken(): Promise<string>;
revokeToken(token: string): Promise<unknown>;
getAuthToken(): Promise<unknown>;
verifyToken(token: string): Promise<unknown>;
}