Skip to content

Commit

Permalink
Auth: defer change events (#212)
Browse files Browse the repository at this point in the history
Wait for API promises to resolve before emitting 'change' events.
  • Loading branch information
eatyourgreens committed Jul 3, 2023
1 parent 8fd48a3 commit 9f1a75d
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions lib/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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);
}

Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 = {
Expand Down

0 comments on commit 9f1a75d

Please sign in to comment.