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 0cefa38
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
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 0cefa38

Please sign in to comment.