diff --git a/lib/auth.js b/lib/auth.js index 77b2687..12f9a1a 100644 --- a/lib/auth.js +++ b/lib/auth.js @@ -32,7 +32,7 @@ const authClient = new Model({ }; try { - const response = await makeCredentialHTTPRequest('POST', url, data, config.jsonHeaders) + const response = await makeCredentialHTTPRequest('POST', url, data, config.jsonHeaders); const token = this._handleNewBearerToken(response); console.info('Got bearer token', token.slice(-6)); return token; @@ -102,7 +102,7 @@ const authClient = new Model({ async _getSession() { console.log('Getting session'); try { - const [user] = await apiClient.get('/me') + const [user] = await apiClient.get('/me'); console.info('Got session', user.login, user.id); return user; } catch (error) { @@ -114,7 +114,7 @@ const authClient = new Model({ async _makeRegistrationRequest(data) { try { const url = config.host + '/users'; - await makeCredentialHTTPRequest('POST', url, data, config.jsonHeaders) + await makeCredentialHTTPRequest('POST', url, data, config.jsonHeaders); await this._getBearerToken(); const user = await this._getSession(); console.info('Registered account', user.login, user.id); @@ -128,7 +128,7 @@ const authClient = new Model({ async register(given) { const user = await this.checkCurrent(); if (user) { - await this.signOut() + await this.signOut(); return this.register(given); } else { const token = await getCSRFToken(config.host); @@ -148,6 +148,7 @@ const authClient = new Model({ const registrationRequest = this._makeRegistrationRequest(data); this._currentUserPromise = registrationRequest.catch(() => null); + await this._currentUserPromise; this.emit('change', this._currentUserPromise); return registrationRequest; @@ -169,6 +170,7 @@ const authClient = new Model({ if (!this._currentUserPromise) { console.log('Checking current user'); this._currentUserPromise = this._getUser(); + await this._currentUserPromise; this.emit('change', this._currentUserPromise); } @@ -188,9 +190,9 @@ const authClient = new Model({ async _makeSignInRequest(data) { try { const url = config.host + '/users/sign_in'; - await makeCredentialHTTPRequest('POST', url, data, config.jsonHeaders) - await this._getBearerToken() - const user = await this._getSession() + await makeCredentialHTTPRequest('POST', url, data, config.jsonHeaders); + await this._getBearerToken(); + const user = await this._getSession(); console.info('Signed in', user.login, user.id); return user; } catch (error) { @@ -200,7 +202,7 @@ const authClient = new Model({ }, async signIn(credentials) { - const user = await this.checkCurrent() + const user = await this.checkCurrent(); if (user) { await this.signOut(); return this.signIn(credentials); @@ -218,6 +220,7 @@ const authClient = new Model({ const signInRequest = this._makeSignInRequest(data); this._currentUserPromise = signInRequest.catch(() => null); + await this._currentUserPromise; this.emit('change', this._currentUserPromise); return signInRequest; @@ -285,6 +288,7 @@ const authClient = new Model({ await user.delete(); this._deleteBearerToken(); this._currentUserPromise = Promise.resolve(null); + await this._currentUserPromise; this.emit('change', this._currentUserPromise); console.info('Disabled account'); return null; @@ -311,6 +315,7 @@ const authClient = new Model({ makeCredentialHTTPRequest('DELETE', url, null, deleteHeaders); this._deleteBearerToken(); this._currentUserPromise = Promise.resolve(null); + await this._currentUserPromise; this.emit('change', this._currentUserPromise); console.info('Signed out'); return null; @@ -324,7 +329,7 @@ const authClient = new Model({ }, async unsubscribeEmail(given) { - const token = await getCSRFToken(config.host) + const token = await getCSRFToken(config.host); const url = config.host + '/unsubscribe'; const data = {