Skip to content

Commit af5b435

Browse files
authored
Merge pull request #75 from js-accounts/fix-login
Fixed logging in state on failed login
2 parents e55c0b6 + 5e4cd6c commit af5b435

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

packages/client/src/AccountsClient.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,19 +196,20 @@ export class AccountsClient {
196196
this.store.dispatch(loggingIn(true));
197197
try {
198198
const res : LoginReturnType = await this.transport.loginWithPassword(user, password);
199+
this.store.dispatch(loggingIn(false));
199200
await this.storeTokens(res);
200201
this.store.dispatch(setUser(res.user));
201202
this.options.onSignedInHook();
202203
if (callback && isFunction(callback)) {
203204
callback();
204205
}
205206
} catch (err) {
207+
this.store.dispatch(loggingIn(false));
206208
if (callback && isFunction(callback)) {
207209
callback(err);
208210
}
209211
throw new AccountsError(err.message);
210212
}
211-
this.store.dispatch(loggingIn(false), user);
212213
}
213214

214215
loggingIn(): boolean {

packages/client/src/AccountsClient.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ describe('Accounts', () => {
212212
const callback = jest.fn();
213213
await Accounts.loginWithPassword('username', 'password', callback);
214214
expect(callback.mock.calls.length).toEqual(1);
215+
expect(Accounts.loggingIn()).toBe(false);
215216
});
216217
it('calls transport', async () => {
217218
const loginWithPassword = jest.fn(() => Promise.resolve(loggedInUser));
@@ -238,6 +239,18 @@ describe('Accounts', () => {
238239
expect(callback.mock.calls[0][0]).toEqual('error');
239240
}
240241
});
242+
it('sets loggingIn flag to false on failed login', async () => {
243+
const transport = {
244+
loginWithPassword: () => Promise.reject('error'),
245+
};
246+
Accounts.config({ history }, transport);
247+
try {
248+
await Accounts.loginWithPassword('username', 'password');
249+
throw new Error();
250+
} catch (err) {
251+
expect(Accounts.loggingIn()).toBe(false);
252+
}
253+
});
241254
it('stores tokens in local storage', async () => {
242255
const transport = {
243256
loginWithPassword: () => Promise.resolve(loggedInUser),

0 commit comments

Comments
 (0)