weightxreps-oauth
Version:
Module to let your javascript app get user's credentials of [weightxreps.net](https://weightxreps.net/). It only focus on obtaining a valid access token, you are then responsible of adding it to your request's headers when connecting to the GraphQL endpoi
93 lines (92 loc) • 2.91 kB
TypeScript
export type Options = {
fetch: typeof fetch;
endpoint: string;
asPopup: boolean;
redirectUri: string;
store: typeof localStorage;
scope: string;
};
export interface WxrUser {
id: number;
uname: string;
email?: string;
}
export declare class OAuthClient extends EventTarget {
readonly clientId: string;
static USER_CANCELED_AUTH_ERROR: string;
static USER_DECLINED_AUTH_ERROR: string;
static USER_MUST_LOGIN: string;
private static dicc;
static get: (clientId: any, options: any) => OAuthClient;
private options;
readonly instanceID: string;
private _pkceCodeVerifier;
private _token;
private store;
private _user;
private _onTokenUpdated;
private _isLoading;
private _error;
get onLogged(): import("./signal").ReadOnlySignal<WxrUser>;
get onLoading(): import("./signal").ReadOnlySignal<boolean>;
get onError(): import("./signal").ReadOnlySignal<string>;
private get pkceCodeVerifier();
private set pkceCodeVerifier(value);
private set token(value);
private get token();
getRequestHeadersAsync(loginIfNeeded?: boolean): Promise<{
Authorization: string;
}>;
getRequestHeadersSync(): {
Authorization: string;
};
constructor(clientId: string, options?: Options);
private setup;
/**
* Check if the querystring has a `code` param which means we landed here with an authorization code provided...
* @returns true if we are logged.
*/
private continueLoginFlow;
/**
* request an access token using this authorization code.
*/
private onAuthorizationCode;
/**
* Will activate in case the login opens a popup window and the popup will send us a message after login or error.
*/
private onWindowMessage;
/**
* @see https://github.com/node-oauth/node-oauth2-server/blob/6a1cf188d5e19faa7ae7569faed1dd51f191a802/lib/token-types/bearer-token-type.js#L33
*/
private onGetTokenResponse;
/**
* Fetchs a token. When a new token is fetched we also get the user info.
*/
private fetchToken;
/**
* Get the token that will allow us to act on behalf of the user!
*/
private getAccessToken;
private refreshToken;
private mustBeHTTPS;
/**
* Sends user to weigthxreps to login and grant us access so we can get an access token
*/
private redirectUserToLogin;
/**
* Gets the access token and makes sure it is not expired.
*
* @param {boolean} loginIfNeeded If true, it will ask user to login if necesary, else, it will throw error if a login is needed...
* @returns {string} a fresh access token
*/
private getFreshToken;
private getUser;
/**
* Connect with weightxreps user.
*/
login(): Promise<void>;
/**
* Logout user from this client.
*/
logout(): void;
}