Skip to content

Commit 73d5b48

Browse files
fix(provider): inject USER_MODEL key to user-authentication provider (#109)
inject USER_MODEL key to user-authentication provider update readme refactor test cases gh-107
1 parent 629c7dc commit 73d5b48

File tree

6 files changed

+87
-34
lines changed

6 files changed

+87
-34
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,12 @@ export class User extends Entity implements IAuthUser {
286286
}
287287
```
288288

289+
Now bind this model to USER_MODEL key in application.ts
290+
291+
```ts
292+
this.bind(AuthenticationBindings.USER_MODEL).to(User);
293+
```
294+
289295
Create CRUD repository for the above model. Use loopback CLI.
290296

291297
```sh
@@ -460,6 +466,12 @@ export class User extends Entity implements IAuthUser {
460466
}
461467
```
462468

469+
Now bind this model to USER_MODEL key in application.ts
470+
471+
```ts
472+
this.bind(AuthenticationBindings.USER_MODEL).to(User);
473+
```
474+
463475
Create CRUD repository for the above model. Use loopback CLI.
464476

465477
```sh
@@ -668,6 +680,12 @@ export class User extends Entity implements IAuthUser {
668680
}
669681
```
670682

683+
Now bind this model to USER_MODEL key in application.ts
684+
685+
```ts
686+
this.bind(AuthenticationBindings.USER_MODEL).to(User);
687+
```
688+
671689
Create CRUD repository for both of the above models. Use loopback CLI.
672690

673691
```sh
@@ -1064,6 +1082,12 @@ export class User extends Entity implements IAuthUser {
10641082
}
10651083
```
10661084

1085+
Now bind this model to USER_MODEL key in application.ts
1086+
1087+
```ts
1088+
this.bind(AuthenticationBindings.USER_MODEL).to(User);
1089+
```
1090+
10671091
Create CRUD repository for the above model. Use loopback CLI.
10681092

10691093
```sh
@@ -1371,6 +1395,12 @@ export class User extends Entity implements IAuthUser {
13711395
}
13721396
```
13731397

1398+
Now bind this model to USER_MODEL key in application.ts
1399+
1400+
```ts
1401+
this.bind(AuthenticationBindings.USER_MODEL).to(User);
1402+
```
1403+
13741404
Create CRUD repository for the above model. Use loopback CLI.
13751405

13761406
```sh
@@ -1676,6 +1706,12 @@ export class User extends Entity implements IAuthUser {
16761706
}
16771707
```
16781708

1709+
Now bind this model to USER_MODEL key in application.ts
1710+
1711+
```ts
1712+
this.bind(AuthenticationBindings.USER_MODEL).to(User);
1713+
```
1714+
16791715
Create CRUD repository for the above model. Use loopback CLI.
16801716

16811717
```sh
@@ -1983,6 +2019,12 @@ export class User extends Entity implements IAuthUser {
19832019
}
19842020
```
19852021

2022+
Now bind this model to USER_MODEL key in application.ts
2023+
2024+
```ts
2025+
this.bind(AuthenticationBindings.USER_MODEL).to(User);
2026+
```
2027+
19862028
Create CRUD repository for the above model. Use loopback CLI.
19872029

19882030
```sh
@@ -2288,6 +2330,12 @@ export class User extends Entity implements IAuthUser {
22882330
}
22892331
```
22902332

2333+
Now bind this model to USER_MODEL key in application.ts
2334+
2335+
```ts
2336+
this.bind(AuthenticationBindings.USER_MODEL).to(User);
2337+
```
2338+
22912339
Create CRUD repository for the above model. Use loopback CLI.
22922340

22932341
```sh
Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Entity, model, property} from '@loopback/repository';
22

33
@model({settings: {strict: false}})
4-
export class Authuser extends Entity {
4+
export class UserCred extends Entity {
55
@property({
66
type: 'string',
77
id: true,
@@ -16,19 +16,13 @@ export class Authuser extends Entity {
1616
})
1717
password: string;
1818

19-
// Define well-known properties here
20-
21-
// Indexer property to allow additional data
22-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
23-
[prop: string]: any;
24-
25-
constructor(data?: Partial<Authuser>) {
19+
constructor(data?: Partial<UserCred>) {
2620
super(data);
2721
}
2822
}
2923

30-
export interface AuthuserRelations {
24+
export interface UserCredRelations {
3125
// describe navigational properties here
3226
}
3327

34-
export type AuthuserWithRelations = Authuser & AuthuserRelations;
28+
export type AuthuserWithRelations = UserCred & UserCredRelations;

src/__tests__/integration/action-sequence/passport-local/local-passport.integration.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {Strategies} from '../../../../strategies/keys';
1010
import {LocalVerifyProvider} from '../../../fixtures/providers/local-password.provider';
1111
import {AuthenticationBindings} from '../../../../keys';
1212
import {IAuthUser} from '../../../../types';
13-
import {Authuser} from '../../../../models';
13+
import {UserCred} from '../../../fixtures/user-cred.model';
1414
/**
1515
* Testing overall flow of authentication with bearer strategy
1616
*/
@@ -25,7 +25,10 @@ describe('Local passport strategy', () => {
2525
class TestController {
2626
@post('/auth/local/no-user-data-passed')
2727
@authenticate(STRATEGY.LOCAL)
28-
test(@requestBody({required: true}) body: Authuser) {
28+
test(
29+
@requestBody({required: true})
30+
body: UserCred,
31+
) {
2932
return 'test successful';
3033
}
3134
}
@@ -43,7 +46,7 @@ describe('Local passport strategy', () => {
4346
@authenticate(STRATEGY.LOCAL)
4447
test(
4548
@requestBody()
46-
body: Authuser,
49+
body: UserCred,
4750
) {
4851
return 'test successful';
4952
}
@@ -66,7 +69,7 @@ describe('Local passport strategy', () => {
6669

6770
@post('/auth/local/no-options')
6871
@authenticate(STRATEGY.LOCAL)
69-
test(@requestBody() body: {username: string; password: string}) {
72+
test(@requestBody() body: UserCred) {
7073
return this.user;
7174
}
7275
}
@@ -94,7 +97,7 @@ describe('Local passport strategy', () => {
9497

9598
@post('/auth/local/pass-req-callback-true')
9699
@authenticate(STRATEGY.LOCAL, {passReqToCallback: true})
97-
async test(@requestBody() body: {username: string; password: string}) {
100+
async test(@requestBody() body: UserCred) {
98101
return this.user;
99102
}
100103
}
@@ -122,7 +125,7 @@ describe('Local passport strategy', () => {
122125

123126
@post('/auth/local/pass-req-callback-false')
124127
@authenticate(STRATEGY.LOCAL, {passReqToCallback: false})
125-
async test(@requestBody() body: {username: string; password: string}) {
128+
async test(@requestBody() body: UserCred) {
126129
return this.user;
127130
}
128131
}
@@ -144,7 +147,7 @@ describe('Local passport strategy', () => {
144147

145148
@post('/auth/local/null-user')
146149
@authenticate(STRATEGY.LOCAL)
147-
async test(@requestBody() body: {username: string; password: string}) {
150+
async test(@requestBody() body: UserCred) {
148151
return body;
149152
}
150153
}
@@ -192,7 +195,7 @@ describe('Local strategy with no verifier', () => {
192195

193196
@post('/auth/local/no-verifier')
194197
@authenticate(STRATEGY.LOCAL, {passReqToCallback: false})
195-
async test(@requestBody() body: {username: string; password: string}) {
198+
async test(@requestBody() body: UserCred) {
196199
return body;
197200
}
198201
}

src/__tests__/integration/middleware-sequence/passport-local/local-passport.integration.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {Strategies} from '../../../../strategies/keys';
1010
import {LocalVerifyProvider} from '../../../fixtures/providers/local-password.provider';
1111
import {AuthenticationBindings} from '../../../../keys';
1212
import {IAuthUser} from '../../../../types';
13-
import {Authuser} from '../../../../models';
13+
import {UserCred} from '../../../fixtures/user-cred.model';
1414
/**
1515
* Testing overall flow of authentication with bearer strategy
1616
*/
@@ -25,7 +25,10 @@ describe('Local passport strategy using Middleware Sequence', () => {
2525
class TestController {
2626
@post('/auth/local/no-user-data-passed')
2727
@authenticate(STRATEGY.LOCAL)
28-
test(@requestBody({required: true}) body: Authuser) {
28+
test(
29+
@requestBody({required: true})
30+
body: UserCred,
31+
) {
2932
return 'test successful';
3033
}
3134
}
@@ -43,7 +46,7 @@ describe('Local passport strategy using Middleware Sequence', () => {
4346
@authenticate(STRATEGY.LOCAL)
4447
test(
4548
@requestBody()
46-
body: Authuser,
49+
body: UserCred,
4750
) {
4851
return 'test successful';
4952
}
@@ -66,7 +69,7 @@ describe('Local passport strategy using Middleware Sequence', () => {
6669

6770
@post('/auth/local/no-options')
6871
@authenticate(STRATEGY.LOCAL)
69-
test(@requestBody() body: {username: string; password: string}) {
72+
test(@requestBody() body: UserCred) {
7073
return this.user;
7174
}
7275
}
@@ -94,7 +97,7 @@ describe('Local passport strategy using Middleware Sequence', () => {
9497

9598
@post('/auth/local/pass-req-callback-true')
9699
@authenticate(STRATEGY.LOCAL, {passReqToCallback: true})
97-
async test(@requestBody() body: {username: string; password: string}) {
100+
async test(@requestBody() body: UserCred) {
98101
return this.user;
99102
}
100103
}
@@ -122,7 +125,7 @@ describe('Local passport strategy using Middleware Sequence', () => {
122125

123126
@post('/auth/local/pass-req-callback-false')
124127
@authenticate(STRATEGY.LOCAL, {passReqToCallback: false})
125-
async test(@requestBody() body: {username: string; password: string}) {
128+
async test(@requestBody() body: UserCred) {
126129
return this.user;
127130
}
128131
}
@@ -144,7 +147,7 @@ describe('Local passport strategy using Middleware Sequence', () => {
144147

145148
@post('/auth/local/null-user')
146149
@authenticate(STRATEGY.LOCAL)
147-
async test(@requestBody() body: {username: string; password: string}) {
150+
async test(@requestBody() body: UserCred) {
148151
return body;
149152
}
150153
}
@@ -192,7 +195,7 @@ describe('Local strategy with no verifier using Middleware Sequence', () => {
192195

193196
@post('/auth/local/no-verifier')
194197
@authenticate(STRATEGY.LOCAL, {passReqToCallback: false})
195-
async test(@requestBody() body: {username: string; password: string}) {
198+
async test(@requestBody() body: UserCred) {
196199
return body;
197200
}
198201
}

src/models/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/providers/user-authentication.provider.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
import {Getter, inject, Provider, Setter} from '@loopback/context';
1+
import {Constructor, Getter, inject, Provider, Setter} from '@loopback/context';
22
import {HttpErrors, Request, Response} from '@loopback/rest';
33
import {Strategy} from 'passport';
44

55
import {AuthErrorKeys} from '../error-keys';
66
import {AuthenticationBindings} from '../keys';
77
import {StrategyAdapter} from '../strategy-adapter';
8-
import {AuthenticateFn, IAuthUser, AuthenticationMetadata} from '../types';
8+
import {
9+
AuthenticateFn,
10+
IAuthUser,
11+
AuthenticationMetadata,
12+
EntityWithIdentifier,
13+
} from '../types';
914

1015
export class AuthenticateActionProvider
1116
implements Provider<AuthenticateFn<IAuthUser | undefined>>
@@ -17,6 +22,8 @@ export class AuthenticateActionProvider
1722
private readonly getMetadata: Getter<AuthenticationMetadata>,
1823
@inject.setter(AuthenticationBindings.CURRENT_USER)
1924
readonly setCurrentUser: Setter<IAuthUser | undefined>,
25+
@inject(AuthenticationBindings.USER_MODEL, {optional: true})
26+
public authUserModel?: Constructor<EntityWithIdentifier & IAuthUser>,
2027
) {}
2128

2229
value(): AuthenticateFn<IAuthUser | undefined> {
@@ -45,12 +52,11 @@ export class AuthenticateActionProvider
4552
authOpts = metadata.authOptions(request);
4653
}
4754
const strategyAdapter = new StrategyAdapter<IAuthUser>(strategy);
48-
const user = await strategyAdapter.authenticate(
49-
request,
50-
response,
51-
authOpts,
52-
);
55+
let user = await strategyAdapter.authenticate(request, response, authOpts);
5356
if (user) {
57+
if (this.authUserModel) {
58+
user = new this.authUserModel(user);
59+
}
5460
this.setCurrentUser(user);
5561
return user;
5662
}

0 commit comments

Comments
 (0)