prostgles-client
Version:
Reactive client for Postgres
44 lines • 2.52 kB
TypeScript
import type { AuthRequest, AuthResponse, AuthSocketSchema, IdentityProvider, LocalLoginMode, UserLike } from "prostgles-types";
import { type InitOptions } from "./prostgles";
import type { Socket } from "socket.io-client";
type OptionalToUndefined<T> = {
[K in {} & keyof T]: T[K];
};
type Args = Pick<OptionalToUndefined<InitOptions>, "credentials" | "redirect"> & {
socket: Socket;
authData: AuthSocketSchema | undefined;
onReload: VoidFunction | undefined;
endpoint: string | undefined;
};
type WithProviderLogin = Partial<Record<IdentityProvider, VoidFunction>>;
type ClientAuthSuccess<T> = T & {
/**
* This is a client-only property that is obtained from server redirect response
*/
redirect_url?: string;
};
export type PasswordLoginResponse = ClientAuthSuccess<AuthResponse.PasswordLoginFailure | AuthResponse.PasswordLoginSuccess | AuthResponse.MagicLinkAuthSuccess | AuthResponse.MagicLinkAuthFailure | AuthResponse.OAuthRegisterSuccess | AuthResponse.OAuthRegisterFailure>;
export type PasswordRegisterResponse = ClientAuthSuccess<AuthResponse.PasswordRegisterSuccess | AuthResponse.PasswordRegisterFailure>;
export type PasswordRegister = (params: AuthRequest.RegisterData) => Promise<PasswordRegisterResponse>;
export type PasswordLogin = (params: AuthRequest.LoginData) => Promise<PasswordLoginResponse>;
type LoginSignupOptions = Pick<AuthSocketSchema, "providers" | "preferredLogin"> & {
loginWithProvider: undefined | WithProviderLogin;
login: undefined | PasswordLogin;
loginType: LocalLoginMode | undefined;
signupWithEmailAndPassword: undefined | PasswordRegister;
confirmEmail: undefined | ((data: AuthRequest.ConfirmEmailData) => Promise<PasswordRegisterResponse>);
};
type AuthStateLoggedOut = LoginSignupOptions & {
isLoggedin: false;
user?: undefined;
};
type AuthStateLoggedIn<U extends UserLike = UserLike> = LoginSignupOptions & {
isLoggedin: true;
user: U;
logout: () => Promise<any>;
};
export type AuthHandler<U extends UserLike = UserLike> = AuthStateLoggedOut | AuthStateLoggedIn<U>;
export declare const getAuthHandler: ({ authData: authConfig, socket, onReload, endpoint, ...authOpts }: Args) => AuthHandler;
export declare const authRequest: <T extends PasswordRegisterResponse | PasswordLoginResponse>(path: string, data: object, method: "GET" | "POST", { credentials, redirect }: Pick<Partial<Request>, "credentials" | "redirect">) => Promise<T>;
export {};
//# sourceMappingURL=getAuthHandler.d.ts.map