Skip to content

Argument of type '{ required: PermissionKeys[]; }' is not assignable to parameter of type 'string | AuthenticationMetadata' #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mathias22osterhagen22 opened this issue Nov 5, 2020 · 8 comments

Comments

@mathias22osterhagen22
Copy link
Contributor

Hello,
There is an issue with the last update of lb4, impossible to build.
Argument of type '{ required: PermissionKeys[]; }' is not assignable to parameter of type 'string | AuthenticationMetadata'

To test:

  1. Clone this repo
  2. lb4 update
  3. npm audit fix
  4. npm run clean; npm run build

image

@mathias22osterhagen22
Copy link
Contributor Author

This seems to be caused by the new declaration of the authenticate decorator:

Old one (this repos):
image

New one
image

@raulneitor
Copy link

did you solved?

@StphnLwnga
Copy link

StphnLwnga commented Dec 15, 2020

I managed to get it working by following the code in loopback4-example-shopping.

  • npm i @loopback/authorization.
  • lb4 service and named it basicAuthorizer. This function will be passed to the endpoints with @authorize decorator as authorization metadata and decides whether to grant or deny the user access. Copy the code here into ./services/basic-authorizer.service.ts.
  • Edit lines 24 - 26 accordingly, renaming roles to permissions, and any other values according to the properties in your User model. Edit line 28 accordingly, leaving the keys for currentUser as they are.
  • Import the basicAuthorization function into the affected controllers, as well as import {authorize} from '@loopback/authorization;'
  • Below all the @authenticate('jwt',...) decorators, add
    @authorize({ allowedRoles: [<PermissionN>,], voters: [basicAuthorization] })
  • Replacing <PermissionN> with the value of required from the 2nd parameter in the the associated @authenticate decorator.
  • Delete the 2nd parameter from the @authenticate decorator.

Screenshot from 2020-12-15 18-39-50

@parul147
Copy link

parul147 commented Aug 7, 2021

I tried the above method but in my case currentUser = {[securityId]: user.id, name: user.name, roles: user.permissions}; in this line roles is undefined, my name is valid I checked but this roles keep coming undefined

@Neeraj1005
Copy link

can anyone tell me how to resolve this?

@VijaySam1
Copy link

@authenticate({strategy: 'jwt', options: {reqired: [PermissionKey.Create]}})
try this ,it works for me

@b1n1yam
Copy link

b1n1yam commented Nov 4, 2023

A recent update for the library has changed the interface for AuthenticationMetadata which is now
export interface AuthenticationMetadata { /** * Name of the authentication strategy */ strategy: string; /** * Options for the authentication strategy */ options?: { [name: string]: any; }; /** * A flag to skip authentication */ skip?: boolean; }

What has worked for me was editing the authauthorize.interceptor.ts file
First, i did: changed metadata's type to a any

@inject(AuthenticationBindings.METADATA)
 public metadata: any // this one

then changed this line
const requriedPermissions = this.metadata[0].options.require as any

There might be a cleaner solution with a correct type of formating but this has worked for me

@HrithikMittal
Copy link
Owner

@b1n1yam It would be great if u create a PR I will merge it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants