Skip to content

Commit

Permalink
fixup!: fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jannyHou committed Apr 17, 2020
1 parent 9db549a commit cc6ae89
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
33 changes: 23 additions & 10 deletions extensions/authentication-jwt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ _All the jwt authentication related code are marked with comment "- enable jwt
auth -", you can search for it to find all the related code you need to enable
the entire jwt authentication in a LoopBack 4 application._

## Add Endpoint in Controller
## Adding Endpoint in Controller

After mounting the component, you can call token and user services to perform
login, then decorate endpoints with `@authentication('jwt')` to inject the
Expand Down Expand Up @@ -178,17 +178,30 @@ The code snippet for whoAmI function:
The complete file is in
[user.controller.ts](https://github.com/strongloop/loopback-next/tree/master/extensions/authentication-jwt/src/__tests__/fixtures/controllers/user.controller.ts)

## Customize Model
## Customization

1. Create your own user model and repository by `lb4 model` and
`lb4 repository`.
As a prototype implementation this module provides basic functionalities in each
service. You can customize and re-bind any element provided in the
[component](https://github.com/strongloop/loopback-next/tree/master/extensions/authentication-jwt/src/jwt-authentication-component.ts)
with your own one.

2. The user service requires the user model, to provide your own one, you need
to copy the
[`user.service.ts`](https://github.com/strongloop/loopback-next/blob/master/extensions/authentication-jwt/src/services/user.service.ts)
file into your application, e.g. copy to file
`myapp/src/services/custom.user.service.ts`, and replace the default `User`,
`UserRepository` with `MyUser`, `MyUserRepository`:
Replacing the `User` model is a bit more complicated because it's not injected
but imported directly in related files. The sub-section covers the steps to
provide your own `User` model and repository.

### Customizing Model

1. Create your own user model and repository by running the `lb4 model` and
`lb4 repository` commands.

2. The user service requires the user model and repository, to provide your own
ones, you can create a custom `UserService` and bind it to
`UserServiceBindings.USER_SERVICE`. Take a look at
[the default user service](https://github.com/strongloop/loopback-next/blob/master/extensions/authentication-jwt/src/services/user.service.ts)
for an example of `UserService` implementation.

For convenience, here is the code in `user.service.ts`. You can replace the
`User` and `UserRepository` with `MyUser`, `MyUserRepository`:

<details>
<summary><strong>Check The Code</strong></summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class UserRepository extends DefaultCrudRepository<
userId: typeof User.prototype.id,
): Promise<UserCredentials | undefined> {
try {
return await this.userCredentials(userId).get();
return this.userCredentials(userId).get();
} catch (err) {
if (err.code === 'ENTITY_NOT_FOUND') {
return undefined;
Expand Down

0 comments on commit cc6ae89

Please sign in to comment.