Skip to content

Commit

Permalink
Add lazyInit option
Browse files Browse the repository at this point in the history
  • Loading branch information
awinograd committed Mar 31, 2020
1 parent 3d81178 commit 1a5f95a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 64 deletions.
62 changes: 4 additions & 58 deletions build/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Full documentation for the "identitytoolkit" API can be found here:
* https://cloud.google.com/identity-platform/docs/reference/rest/v1/accounts
*/
import { ActionCodeInfo, UserInfo as FBUser } from '@firebase/auth-types';
import { UserInfo as FBUser } from '@firebase/auth-types';
declare type User = FBUser & {
tokenManager: {
idToken: string;
Expand All @@ -22,6 +22,7 @@ declare type AsyncStorage = {
};
declare type AuthOptions = {
apiKey: string;
lazyInit?: boolean;
name?: string;
providers?: Array<Provider | string>;
redirectUri?: string;
Expand Down Expand Up @@ -65,8 +66,8 @@ export default class Auth {
user: User | null;
storage: AsyncStorage;
initialized: boolean;
constructor({ name, apiKey, redirectUri, providers, storage }: AuthOptions);
_initUser(): Promise<void>;
constructor({ name, apiKey, redirectUri, providers, storage, lazyInit }: AuthOptions);
initUser(): Promise<void>;
get currentUser(): User | null;
/**
* Emits an event and triggers all of the listeners.
Expand Down Expand Up @@ -121,28 +122,6 @@ export default class Auth {
* @param {string} token The custom token.
*/
signInWithCustomToken(token: string): Promise<void>;
/**
* Start auth flow of a federated Id provider.
* Will redirect the page to the federated login page.
* @param {oauthFlowOptions|string} options An options object, or a string with the name of the provider.
*/
signInWithProvider(options: string | {
provider: string;
context: unknown;
linkAccount: boolean;
}): Promise<void>;
/**
* Signs in or signs up a user using credentials from an Identity Provider (IdP) after a redirect.
* Will fail silently if the URL doesn't have a "code" search param.
* @param {string} [requestUri] The request URI with the authorization code, state etc. from the IdP.
* @private
*/
finishProviderSignIn(requestUri?: string): Promise<any>;
/**
* Handles all sign in flows that complete via redirects.
* Fails silently if no redirect was detected.
*/
handleSignInRedirect(): Promise<any>;
/**
* Signs up with email and password or anonymously when no arguments are passed.
* Automatically signs the user in on completion.
Expand All @@ -156,28 +135,6 @@ export default class Auth {
* @param {string} password
*/
signIn(email: string, password: string): Promise<void>;
/**
* Sends an out-of-band confirmation code for an account.
* Can be used to reset a password, to verify an email address and send a Sign-in email link.
* The `email` argument is not needed only when verifying an email(In that case it will be completely ignored, even if specified), otherwise it is required.
* @param {'PASSWORD_RESET'|'VERIFY_EMAIL'|'EMAIL_SIGNIN'} requestType The type of out-of-band (OOB) code to send.
* @param {string} [email] When the `requestType` is `PASSWORD_RESET` you need to provide an email address, else it will be ignored.
* @returns {Promise}
*/
sendOobCode(requestType: keyof typeof ActionCodeInfo.Operation, email?: string): Promise<undefined>;
/**
* Sets a new password by using a reset code.
* Can also be used to very oobCode by not passing a password.
* @param {string} code
* @returns {string} The email of the account to which the code was issued.
*/
resetPassword(oobCode: string, newPassword: string): Promise<any>;
/**
* Returns info about all providers associated with a specified email.
* @param {string} email The user's email address.
* @returns {ProvidersForEmailResponse}
*/
fetchProvidersForEmail(email: string): Promise<any>;
/**
* Gets the user data from the server, and updates the local caches.
* @param {Object} [tokenManager] Only when not logged in.
Expand All @@ -188,16 +145,5 @@ export default class Auth {
refreshToken: string;
expiresAt: number;
} | null): Promise<void>;
/**
* Update user's profile.
* @param {Object} newData An object with the new data to overwrite.
* @throws Will throw if the user is not signed in.
*/
updateProfile(newData: User): Promise<void>;
/**
* Deletes the currently logged in account and logs out.
* @throws Will throw if the user is not signed in.
*/
deleteAccount(): Promise<void>;
}
export {};
8 changes: 5 additions & 3 deletions build/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const localStorageAdapter = {
* @param {Array.<ProviderOptions|string>} options.providers Array of arguments that will be passed to the addProvider method.
*/
export default class Auth {
constructor({ name = 'default', apiKey, redirectUri, providers = [], storage = localStorageAdapter }) {
constructor({ name = 'default', apiKey, redirectUri, providers = [], storage = localStorageAdapter, lazyInit }) {
this.refreshTokenRequest = null;
this.initialized = false;
if (!apiKey)
Expand All @@ -63,9 +63,11 @@ export default class Auth {
const { name, scope } = typeof options === 'string' ? { name: options, scope: undefined } : options;
this.providers[name] = scope;
}
this._initUser();
if (!lazyInit) {
this.initUser();
}
}
async _initUser() {
async initUser() {
/**
* User data if the user is logged in, else its null.
* @type {Object|null}
Expand Down
9 changes: 6 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type AsyncStorage = {

type AuthOptions = {
apiKey: string;
lazyInit?: boolean;
name?: string;
providers?: Array<Provider | string>;
redirectUri?: string;
Expand Down Expand Up @@ -82,7 +83,7 @@ export default class Auth {
storage: AsyncStorage;
initialized: boolean = false;

constructor({ name = 'default', apiKey, redirectUri, providers = [], storage = localStorageAdapter }: AuthOptions) {
constructor({ name = 'default', apiKey, redirectUri, providers = [], storage = localStorageAdapter, lazyInit }: AuthOptions) {
if (!apiKey) throw Error('The argument "apiKey" is required');
if (!Array.isArray(providers)) throw Error('The argument "providers" must be an array');

Expand All @@ -107,10 +108,12 @@ export default class Auth {
this.providers[name] = scope;
}

this._initUser();
if (!lazyInit) {
this.initUser();
}
}

async _initUser() {
async initUser() {
/**
* User data if the user is logged in, else its null.
* @type {Object|null}
Expand Down

0 comments on commit 1a5f95a

Please sign in to comment.