Skip to content

Commit

Permalink
fix(typeorm): make the services attribute consistent with mongodb
Browse files Browse the repository at this point in the history
* Updated the `getServices` method on the `User` entity to construct
objects for the top level fields
* Added special handling for `email.verificationTokens` and
`password.reset`
* Updated tests to match the mongodb tests

Fixes: accounts-js#1110
  • Loading branch information
blessanabraham committed Feb 5, 2021
1 parent 2450f33 commit a9e36df
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
24 changes: 10 additions & 14 deletions packages/database-typeorm/__tests__/typeorm-password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,9 @@ describe('TypeormServicePassword', () => {
},
],
services: {
password: [
{
bcrypt: 'toto',
},
],
password: {
bcrypt: 'toto',
},
},
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
Expand All @@ -81,12 +79,10 @@ describe('TypeormServicePassword', () => {
});
const ret = await accountsTypeorm.findUserById(userId);
expect(userId).toBeTruthy();
expect(ret!.services).toMatchObject({
password: [
{
bcrypt: 'toto',
},
],
expect(ret!.services).toEqual({
password: {
bcrypt: 'toto',
},
});
});

Expand Down Expand Up @@ -270,7 +266,7 @@ describe('TypeormServicePassword', () => {
const ret = await accountsTypeorm.findPasswordHash(userId);
const services: any = retUser!.services;
expect(ret).toBeTruthy();
expect(ret).toEqual(services.password[0].bcrypt);
expect(ret).toEqual(services.password.bcrypt);
});
});

Expand Down Expand Up @@ -409,8 +405,8 @@ describe('TypeormServicePassword', () => {
await accountsTypeorm.setPassword(userId, newPassword);
const retUser = await accountsTypeorm.findUserById(userId);
const services: any = retUser!.services;
expect(services.password[0].bcrypt).toBeTruthy();
expect(services.password[0].bcrypt).toEqual(newPassword);
expect(services.password.bcrypt).toBeTruthy();
expect(services.password.bcrypt).toEqual(newPassword);
expect(retUser!.createdAt).not.toEqual(retUser!.updatedAt);
});
});
Expand Down
15 changes: 11 additions & 4 deletions packages/database-typeorm/src/entity/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,17 @@ export class User {
@AfterLoad()
public async getServices() {
this.services = (this.allServices || []).reduce((acc, service) => {
set(acc, service.name, [
...[].concat(get(acc, service.name, [])),
{ ...(service.token ? { token: service.token } : {}), ...service.options },
]);
if (['password.reset', 'email.verificationTokens'].includes(service.name)) {
set(acc, service.name, [
...[].concat(get(acc, service.name, [])),
{ ...(service.token ? { token: service.token } : {}), ...service.options },
]);
} else {
set(acc, service.name, {
...(service.token ? { token: service.token } : {}),
...service.options,
});
}
return acc;
}, this.services);
}
Expand Down

0 comments on commit a9e36df

Please sign in to comment.