Skip to content

Commit cc4e767

Browse files
authored
fix(authentication-client): Do not cache authentication errors (#2892)
1 parent 4822c94 commit cc4e767

File tree

3 files changed

+103
-104
lines changed

3 files changed

+103
-104
lines changed

packages/authentication-client/src/core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export class AuthenticationClient {
134134
return type === 'logout' ? promise : promise.then(() => Promise.reject(error))
135135
}
136136

137-
return Promise.reject(error)
137+
return this.reset().then(() => Promise.reject(error))
138138
}
139139

140140
reAuthenticate(force = false, strategy?: string): Promise<AuthenticationResult> {

packages/authentication-client/test/index.test.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,29 @@ describe('@feathersjs/authentication-client', () => {
1717

1818
app.configure(client())
1919
app.use('/authentication', {
20-
create(data: any) {
20+
async create(data: any) {
2121
if (data.error) {
22-
return Promise.reject(new Error('Did not work'))
22+
throw new Error('Did not work')
2323
}
2424

25-
return Promise.resolve({
25+
return {
2626
accessToken,
2727
data,
2828
user
29-
})
29+
}
3030
},
3131

32-
remove(id) {
32+
async remove(id) {
3333
if (!app.get('authentication')) {
3434
throw new NotAuthenticated('Not logged in')
3535
}
3636

37-
return Promise.resolve({ id })
37+
return { id }
3838
}
3939
})
4040
app.use('dummy', {
41-
find(params) {
42-
return Promise.resolve(params)
41+
async find(params) {
42+
return params
4343
}
4444
})
4545
})
@@ -147,6 +147,16 @@ describe('@feathersjs/authentication-client', () => {
147147
assert.strictEqual(at, accessToken)
148148
})
149149

150+
it('resets after any error (#1947)', async () => {
151+
await assert.rejects(() => app.authenticate({ strategy: 'testing', error: true }), {
152+
message: 'Did not work'
153+
})
154+
155+
const found = await app.service('dummy').find()
156+
157+
assert.deepStrictEqual(found, {})
158+
})
159+
150160
it('logout when not logged in without error', async () => {
151161
const result = await app.logout()
152162

packages/authentication-client/test/integration/commons.ts

Lines changed: 84 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -11,119 +11,108 @@ export default (
1111
let client: Application
1212
let user: any
1313

14-
before(() =>
15-
getApp()
16-
.service('users')
17-
.create({
14+
before(
15+
async () =>
16+
(user = await getApp().service('users').create({
1817
email,
1918
password
20-
})
21-
.then((result: any) => {
22-
user = result
23-
})
19+
}))
2420
)
2521

2622
beforeEach(() => {
2723
client = getClient()
2824
})
2925

30-
it('authenticates with local strategy', () => {
31-
return client
32-
.authenticate({
33-
strategy: 'local',
34-
email,
35-
password
36-
})
37-
.then((result) => {
38-
assert.ok(result.accessToken)
39-
assert.strictEqual(result.authentication.strategy, 'local')
40-
assert.strictEqual(result.user.email, email)
41-
})
26+
after(async () => {
27+
await getApp().service('users').remove(user.id)
4228
})
4329

44-
it('authentication with wrong credentials fails, does not maintain state', () => {
45-
return client
46-
.authenticate({
47-
strategy: 'local',
48-
email,
49-
password: 'blabla'
50-
})
51-
.then(() => assert.fail('Should never get here'))
52-
.catch((error) => {
53-
assert.strictEqual(error.name, 'NotAuthenticated')
54-
assert.strictEqual(error.message, 'Invalid login')
55-
assert.ok(!client.get('authentication'), 'Reset client state')
56-
})
30+
it('authenticates with local strategy', async () => {
31+
const result = await client.authenticate({
32+
strategy: 'local',
33+
email,
34+
password
35+
})
36+
37+
assert.ok(result.accessToken)
38+
assert.strictEqual(result.authentication.strategy, 'local')
39+
assert.strictEqual(result.user.email, email)
5740
})
5841

59-
it('errors when not authenticated', () => {
60-
return client
61-
.service('dummy')
62-
.find()
63-
.then(() => assert.fail('Should never get here'))
64-
.catch((error: any) => {
65-
assert.strictEqual(error.name, 'NotAuthenticated')
66-
assert.strictEqual(error.code, 401)
67-
assert.strictEqual(error.message, 'Not authenticated')
68-
})
42+
it('authentication with wrong credentials fails, does not maintain state', async () => {
43+
await assert.rejects(
44+
() =>
45+
client.authenticate({
46+
strategy: 'local',
47+
email,
48+
password: 'blabla'
49+
}),
50+
{
51+
name: 'NotAuthenticated',
52+
message: 'Invalid login'
53+
}
54+
)
55+
assert.ok(!client.get('authentication'), 'Reset client state')
6956
})
7057

71-
it('authenticates and allows access', () => {
72-
return client
73-
.authenticate({
74-
strategy: 'local',
75-
email,
76-
password
77-
})
78-
.then(() => client.service('dummy').find())
79-
.then((result) => {
80-
assert.strictEqual(result.provider, provider)
81-
assert.ok(result.authentication)
82-
assert.ok(result.authentication.payload)
83-
assert.strictEqual(result.user.email, user.email)
84-
assert.strictEqual(result.user.id, user.id)
85-
})
58+
it('errors when not authenticated', async () => {
59+
await assert.rejects(() => client.service('dummy').find(), {
60+
name: 'NotAuthenticated',
61+
code: 401,
62+
message: 'Not authenticated'
63+
})
8664
})
8765

88-
it('re-authenticates', () => {
89-
return client
90-
.authenticate({
91-
strategy: 'local',
92-
email,
93-
password
94-
})
95-
.then(() => client.authentication.reset())
96-
.then(() => client.authenticate())
97-
.then(() => client.service('dummy').find())
98-
.then((result) => {
99-
assert.strictEqual(result.provider, provider)
100-
assert.ok(result.authentication)
101-
assert.ok(result.authentication.payload)
102-
assert.strictEqual(result.user.email, user.email)
103-
assert.strictEqual(result.user.id, user.id)
104-
})
66+
it('authenticates and allows access', async () => {
67+
await client.authenticate({
68+
strategy: 'local',
69+
email,
70+
password
71+
})
72+
const result = await client.service('dummy').find()
73+
74+
assert.strictEqual(result.provider, provider)
75+
assert.ok(result.authentication)
76+
assert.ok(result.authentication.payload)
77+
assert.strictEqual(result.user.email, user.email)
78+
assert.strictEqual(result.user.id, user.id)
10579
})
10680

107-
it('after logout does not allow subsequent access', () => {
108-
return client
109-
.authenticate({
110-
strategy: 'local',
111-
email,
112-
password
113-
})
114-
.then(() => client.logout())
115-
.then((result) => {
116-
assert.ok(result.accessToken)
117-
assert.ok(result.user)
118-
119-
return client.service('dummy').find()
120-
})
121-
.then(() => assert.fail('Should never get here'))
122-
.catch((error) => {
123-
assert.strictEqual(error.name, 'NotAuthenticated')
124-
assert.strictEqual(error.code, 401)
125-
assert.strictEqual(error.message, 'Not authenticated')
126-
})
81+
it('re-authenticates', async () => {
82+
await client.authenticate({
83+
strategy: 'local',
84+
email,
85+
password
86+
})
87+
88+
client.authentication.reset()
89+
client.authenticate()
90+
const result = await client.service('dummy').find()
91+
92+
assert.strictEqual(result.provider, provider)
93+
assert.ok(result.authentication)
94+
assert.ok(result.authentication.payload)
95+
assert.strictEqual(result.user.email, user.email)
96+
assert.strictEqual(result.user.id, user.id)
97+
})
98+
99+
it('after logout does not allow subsequent access', async () => {
100+
await client.authenticate({
101+
strategy: 'local',
102+
email,
103+
password
104+
})
105+
106+
const result = await client.logout()
107+
108+
assert.ok(result!.accessToken)
109+
assert.ok(result!.user)
110+
111+
assert.rejects(() => client.service('dummy').find(), {
112+
name: 'NotAuthenticated',
113+
code: 401,
114+
message: 'Not authenticated'
115+
})
127116
})
128117
})
129118
}

0 commit comments

Comments
 (0)