@happykit/auth-email
Version:
- A `useAuth` hook which returns the current user - An optional `getServerSideAuth` for server-side rendering - HappyAuth is tiny - it adds only 4.6 kB to the first load JS - it adds less than 0.04 kB if you're transitioning from another page - Extremely
224 lines (223 loc) • 6.42 kB
TypeScript
import * as React from "react";
/**
* Wrap your application in <AuthProvider /> by configuring it in _app.js / _app.ts.
*
* This provider makes sure that you're only using one useAuth hook per page.
*/
export declare const AuthProvider: React.FunctionComponent;
export declare enum Provider {
email = "email"
}
export declare enum AccountStatus {
unconfirmed = "unconfirmed",
confirmed = "confirmed"
}
/**
* Shape of your token data.
*
* You can extend this interface to configure your own shape, in case you
* are using serverConfig.triggers.fetchAdditionalTokenContent.
*
* Keep in mind that some users might still have an old token when they sign in,
* so there is no guarantee that the shape of your additional data actually
* matches the token you're receiving. You can change the serverConfig.tokenSecret
* and thereby force a logout of all your users when you change the token contents.
*/
export interface BaseTokenData {
accountStatus: AccountStatus;
provider: Provider;
userId: string;
}
interface AuthContext<T extends BaseTokenData> {
tokenData: T | null;
error: string | null;
}
export declare type AuthEvent<T extends BaseTokenData> = {
type: "SIGN_IN_SUCCESS";
tokenData: T;
} | {
type: "SIGN_IN_FAILURE";
error: string;
} | {
type: "SIGN_IN_ERROR";
error: string;
};
export declare type BaseAuthState<T extends BaseTokenData> = {
value: "authenticating";
context: AuthContext<T> & {
tokenData: null;
error: null;
};
} | {
value: "signedIn";
context: AuthContext<T> & {
tokenData: T;
error: null;
};
} | {
value: "signedOut";
context: AuthContext<T> & {
tokenData: null;
error: null;
};
} | {
value: "signInError";
context: AuthContext<T> & {
tokenData: null;
error: string;
};
};
export declare type AuthApi = {
/**
* Signs a user in using their email and password.
*/
signIn: (email: string, password: string, rememberMe?: boolean) => Promise<HappyApiResponse<{
ok: true;
}>>;
/**
* Signs a user out.
*/
signOut: (redirectTo?: string) => Promise<void>;
/**
* Confirms an unconfirmed account.
*
* The token is sent to users via email after they sign up. You can configure
* the email using serverConfig.triggers.sendConfirmAccountMail.
*/
confirmAccount: (token: string) => Promise<HappyApiResponse<{
ok: true;
}>>;
/**
* Reset a users password.
*
* You can start this flow by calling auth.forgotPassword().
*/
resetPassword: (token: string, password: string) => Promise<HappyApiResponse<{
ok: true;
}>>;
/**
* Register a new user.
*/
signUp: (email: string, password: string) => Promise<HappyApiResponse<{
ok: true;
}>>;
/**
* Change a users password.
*/
changePassword: (currentPassword: string, newPassword: string) => Promise<HappyApiResponse<{
ok: true;
}>>;
/**
* Request a password reset link for a user.
*
* The link including a password-reset token will be sent to the users email
* address using serverConfig.triggers.sendForgotPasswordMail.
*
* You can then call auh.resetPassword() with that token to reset the password.
*/
forgotPassword: (email: string) => Promise<HappyApiResponse<{
ok: true;
}>>;
};
export declare type Auth<T extends BaseTokenData> = {
state: "authenticating";
tokenData: null;
error: null;
signIn: null;
signUp: null;
signOut: null;
changePassword: null;
confirmAccount: null;
forgotPassword: null;
resetPassword: null;
} | {
state: "signedIn";
tokenData: T;
error: null;
signIn: null;
signUp: null;
signOut: AuthApi["signOut"];
changePassword: AuthApi["changePassword"];
confirmAccount: null;
forgotPassword: null;
resetPassword: null;
} | {
state: "signedOut";
tokenData: null;
error: null;
signIn: AuthApi["signIn"];
signUp: AuthApi["signUp"];
signOut: null;
changePassword: null;
confirmAccount: AuthApi["confirmAccount"];
forgotPassword: AuthApi["forgotPassword"];
resetPassword: AuthApi["resetPassword"];
} | {
state: "signInError";
tokenData: null;
error: string;
signIn: null;
signUp: null;
signOut: null;
changePassword: null;
confirmAccount: null;
forgotPassword: null;
resetPassword: null;
};
/**
* Returns a preconfigured useAuth hook
*
* Note that you may only use one useAuth hook per page. Pass the hook data
* down to each component which needs it, or create your custom provider.
*
* Every page can decide whether it wants to prefetch the auth data on the server,
* or whether it wants to have the client handle that. That way you can mix
* server-side rendering and static generation as you need.
*/
export declare function createUseAuth<T extends BaseTokenData>(publicConfig: PublicConfig): (initialUserState?: BaseAuthState<T>) => Auth<T>;
export declare type PublicConfig = {
/**
* URL of your application (without the trailing slash).
*
* Example: https://example.now.sh
*
* This is used when generating links for emails, OAuth and redirects.
* Set it to http://localhost:3000 for local development.
*/
baseUrl: string;
/**
* A list of identity providers for OAuth logins and signups.
*
* Pass a map of keys and their names, like { github: { name: 'GitHub' } }
* The passed names will be shown as the labels of the OAuth buttons.
*
* This is the frontend part of the configuration. You'll also need to configure `serverConfig.identityProviders`.
*/
identityProviders: {
[key: string]: {
name: string;
};
};
/**
* Configure where users are redirected to after certain actions.
*/
redirects?: {
afterConfirmAccount?: string;
afterResetPassword?: string;
afterSignIn?: string;
afterSignOut?: string;
afterChangePassword?: string;
};
};
export declare type HappyError = {
code: string;
message?: string;
};
export declare type HappyApiResponse<D extends any> = {
error: undefined;
data: D;
} | {
error: HappyError;
data: undefined;
};
export {};