I have followed and set up via @loopback/authentication as described here: https://github.com/strongloop/loopback-next/blob/master/packages/authentication/README.md
Error while accessing any route
application.ts
import {BootMixin, Binding, Booter} from '@loopback/boot';
import {RestApplication, RestServer, RestBindings} from '@loopback/rest';
import {
AuthenticationComponent,
AuthenticationBindings,
} from '@loopback/authentication';
import {MyAuthStrategyProvider} from './providers/auth-strategy.provider';
import {MySequence} from './sequence';
import {ApplicationConfig} from '@loopback/core';
export class VanityApplication extends BootMixin(RestApplication) {
constructor(options?: ApplicationConfig) {
super(options);
this.projectRoot = __dirname;
this.component(AuthenticationComponent);
this.bind(AuthenticationBindings.STRATEGY).toProvider(
MyAuthStrategyProvider,
);
this.sequence(MySequence);
}
async start() {
await super.start();
const server = await this.getServer(RestServer);
const port = await server.get(RestBindings.PORT);
console.log(`REST server running on port: ${port}`);
}
}
sequence.ts
import {
RestBindings,
SequenceHandler,
FindRoute,
ParseParams,
InvokeMethod,
Send,
Reject,
RequestContext,
} from '@loopback/rest';
import {inject} from '@loopback/context';
import {AuthenticationBindings, AuthenticateFn} from '@loopback/authentication';
const SequenceActions = RestBindings.SequenceActions;
export class MySequence implements SequenceHandler {
constructor(
@inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute,
@inject(SequenceActions.PARSE_PARAMS) protected parseParams: ParseParams,
@inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod,
@inject(SequenceActions.SEND) protected send: Send,
@inject(SequenceActions.REJECT) protected reject: Reject,
@inject(AuthenticationBindings.AUTH_ACTION)
protected authenticateRequest: AuthenticateFn,
) {}
async handle(context: RequestContext) {
try {
const {request, response} = context;
const route = this.findRoute(request);
// This is the important line added to the default sequence implementation
await this.authenticateRequest(request);
const args = await this.parseParams(request, route);
const result = await this.invoke(route, args);
this.send(response, result);
} catch (error) {
this.reject(context, error);
}
}
}
auth-strategy.provider.ts
import {Provider, inject, ValueOrPromise} from '@loopback/context';
import {Strategy} from 'passport';
import {
AuthenticationBindings,
AuthenticationMetadata,
UserProfile,
} from '@loopback/authentication';
import {BasicStrategy} from 'passport-http';
export class MyAuthStrategyProvider implements Provider<Strategy | undefined> {
constructor(
@inject(AuthenticationBindings.METADATA)
private metadata: AuthenticationMetadata,
) {}
value(): ValueOrPromise<Strategy | undefined> {
// The function was not decorated, so we shouldn't attempt authentication
if (!this.metadata) {
return undefined;
}
const name = this.metadata.strategy;
if (name === 'BasicStrategy') {
return new BasicStrategy(this.verify);
} else {
return Promise.reject(`The strategy ${name} is not available.`);
}
}
verify(
username: string,
password: string,
cb: (err: Error | null, user?: UserProfile | false) => void,
) {
// find user by name & password
// call cb(null, false) when user not found
// call cb(null, user) when user is authenticated
}
}
Unhandled error in GET /ping: 500 Error: Cannot resolve injected arguments for AuthMetadataProvider.[0]: The arguments[0] is not decorated for dependency injection, but a value is not supplied
at value_promise_1.resolveList (C:\myApp\node_modules\@loopback\context\dist\src\resolver.js:139:23)
at Object.resolveList (C:\myApp\node_modules\@loopback\context\dist\src\value-promise.js:135:32)
at resolveInjectedArguments (C:\myApp\node_modules\@loopback\context\dist\src\resolver.js:128:28)
at Object.instantiateClass (C:\myApp\node_modules\@loopback\context\dist\src\resolver.js:37:27)
at Binding._getValue (C:\myApp\node_modules\@loopback\context\dist\src\binding.js:338:50)
at resolution_session_1.ResolutionSession.runWithBinding.s (C:\myApp\node_modules\@loopback\context\dist\src\binding.js:189:90)
at value_promise_1.tryWithFinally (C:\myApp\node_modules\@loopback\context\dist\src\resolution-session.js:69:53)
at Object.tryWithFinally (C:\myApp\node_modules\@loopback\context\dist\src\value-promise.js:162:18)
at Function.runWithBinding (C:\myApp\node_modules\@loopback\context\dist\src\resolution-session.js:69:32)
at Binding.getValue (C:\myApp\node_modules\@loopback\context\dist\src\binding.js:189:65)
at RequestContext.getValueOrPromise (C:\myApp\node_modules\@loopback\context\dist\src\context.js:238:36)
at resolution_session_1.ResolutionSession.runWithInjection.s (C:\myApp\node_modules\@loopback\context\dist\src\resolver.js:73:24)
at value_promise_1.tryWithFinally (C:\myApp\node_modules\@loopback\context\dist\src\resolution-session.js:89:53)
at Object.tryWithFinally (C:\myApp\node_modules\@loopback\context\dist\src\value-promise.js:162:18)
at Function.runWithInjection (C:\myApp\node_modules\@loopback\context\dist\src\resolution-session.js:89:32)
at resolve (C:\myApp\node_modules\@loopback\context\dist\src\resolver.js:66:59)
@TommyBez How is your controller function decorated with @authenticate()? Looks like you didn't specify a strategy name as the first parameter, the correct syntax is sth like @authenticate('jwt').
@jannyHou Actually it is the controller provided in the example from the documentation.
Anyway I get the error for any route/controller.
Debugging the application, I've found out it's not calling MyAuthStrategyProvider at all.
I've tried to delete AuthenticationMetadata injection and always return BasicStrategy as value and it works.
It seems the issue is happening at dependency resolver level.
I found a similar issue using the latest version of @loopback/context 1.7.0 , when you do an npm link from a component folder to your application project I receive a similar issue.
Error: Cannot resolve injected arguments for LogActionProvider.[0]: The arguments[0] is not decorated for dependency injection, but a value is not supplied
@TommyBez try to fix the npm versions , remove the ^ for all the loopback packages, after that it will work.
Can you try to run npm ls reflect-metadata to see if there are multiple versions?
@TommyBez Can you share a github repo to reproduce the problem?
@raymondfeng this is the result running npm ls reflect-metadata:
+-- @loopback/[email protected]
| `-- @loopback/[email protected]
| `-- [email protected]
+-- @loopback/[email protected]
| `-- @loopback/[email protected]
| `-- [email protected] deduped
`-- @loopback/[email protected]
`-- [email protected]
`-- @fimbul/[email protected]
`-- @fimbul/[email protected]
`-- [email protected] deduped
I don't have a github repo to share, anyway you can easily reproduce the scenario with a simple Rest application with the authentication example representation.
Here below my current package.json dependencies:
"dependencies": {
"@loopback/authentication": "^1.0.15",
"@loopback/boot": "^1.0.11",
"@loopback/context": "^1.5.0",
"@loopback/core": "^1.1.5",
"@loopback/openapi-v3": "^1.2.0",
"@loopback/repository": "^1.1.4",
"@loopback/rest": "^1.5.4",
"@loopback/rest-explorer": "^1.1.7",
"@loopback/service-proxy": "^1.0.7",
"@types/bcrypt": "^3.0.0",
"@types/jsonwebtoken": "^8.3.2",
"bcrypt": "^3.0.4",
"fs": "0.0.1-security",
"loopback-connector-postgresql": "^3.6.0",
"passport": "^0.4.0",
"passport-http": "^0.3.0"
},
"devDependencies": {
"@loopback/build": "^1.2.1",
"@loopback/testlab": "^1.0.5",
"@loopback/tslint-config": "^2.0.0",
"@types/node": "^10.11.2",
"@types/passport": "^1.0.0",
"@types/passport-http": "^0.3.7",
"tslint": "^5.12.0",
"typescript": "^3.2.2"
}
@JamilOmar I've tried removing ^ and reinstalling all loopback packages but unfortunately it didn't fix it.
@JamilOmar Could you tell me the exact @loopback/context and @loopback/authentication versions you are using? Thank you.
@TommyBez
"dependencies": {
"@loopback/boot": "1.0.12",
"@loopback/context": "1.6.0",
"@loopback/core": "1.1.6",
"@loopback/openapi-v3": "1.2.1",
"@loopback/repository": "1.1.5",
"@loopback/rest": "1.5.5",
"@loopback/rest-explorer": "1.1.8",
"@loopback/service-proxy": "1.0.8",
"memory": "0.0.3"
},
"devDependencies": {
"@loopback/build": "1.3.0",
"@loopback/testlab": "1.0.6",
"@loopback/tslint-config": "2.0.0",
"@types/node": "^10.11.2",
"tslint": "^5.12.0",
"typescript": "^3.3.1"
},
Test that , I was able to run it on my local.
Hi, I can reproduce the problem now. It turns out you have mixed versions of @loopback/context. Please run npm ls @loopback/context and make sure only one consistent version is used. There was a breaking change introduced in @loopback/[email protected] and we didn't anticipate the mixed versions of this module are used.
You can use lb4 --version command to see a list of compatible versions of @loopback/* modules.
BTW, if you install the latest version of @loopback/cli and run lb4 model or other commands in your old project, it will prompt you something like:
lb4 model
The project has dependencies with incompatible versions required by the CLI:
- typescript: 3.2.2 (cli ^3.3.1)
- @loopback/boot: 1.0.11 (cli ^1.1.0)
- @loopback/build: 1.2.1 (cli ^1.3.2)
- @loopback/context: 1.5.0 (cli ^1.7.0)
- @loopback/core: 1.1.5 (cli ^1.1.8)
- @loopback/openapi-v3: 1.2.0 (cli ^1.3.0)
- @loopback/repository: 1.1.4 (cli ^1.1.8)
- @loopback/rest: 1.5.4 (cli ^1.8.0)
- @loopback/testlab: 1.0.5 (cli ^1.1.0)
- @loopback/service-proxy: 1.0.7 (cli ^1.0.10)
- @loopback/rest-explorer: 1.1.7 (cli ^1.1.11)
- @loopback/tslint-config: 2.0.0 (cli ^2.0.2)
I have a similar problem which started also from trying to implement the authentication,.
The "metadata" is not inserted in the strategy and I have no idea how to fix it.
From the previous I understand that probably this is related to different "context" versions.
npm ls @loopback/context
```โโโฌ @loopback/[email protected]
โ โโโ @loopback/[email protected]
โ โโโฌ @loopback/[email protected]
โ โ โโโ @loopback/[email protected] deduped
โ โโโฌ @loopback/[email protected]
โ โ โโโ @loopback/[email protected] deduped
โ โ โโโฌ @loopback/[email protected]
โ โ โโโ @loopback/[email protected] deduped
โ โ โโโฌ @loopback/[email protected]
โ โ โโโ @loopback/[email protected] deduped
โ โโโฌ @loopback/[email protected]
โ โโโ @loopback/[email protected] deduped
โโโฌ @loopback/[email protected]
โ โโโ @loopback/[email protected] deduped
โโโ @loopback/[email protected]
โโโฌ @loopback/[email protected]
โ โโโ @loopback/[email protected] deduped
โโโฌ @loopback/[email protected]
โ โโโ @loopback/[email protected] deduped
โ โโโฌ @loopback/[email protected]
โ โโโ @loopback/[email protected] deduped
โโโฌ @loopback/[email protected]
โ โโโ @loopback/[email protected] deduped
โโโฌ @loopback/[email protected]
โ โโโ @loopback/[email protected] deduped
โโโฌ @loopback/[email protected]
โ โโโ @loopback/[email protected] deduped
โโโฌ @loopback/[email protected]
โโโ @loopback/[email protected] deduped
**lb4 --version**
```@loopback/cli version: 1.8.2
@loopback/* dependencies:
- @loopback/authentication: ^1.0.15
- @loopback/boot: ^1.1.0
- @loopback/build: ^1.3.2
- @loopback/context: ^1.7.0
- @loopback/core: ^1.1.8
- @loopback/metadata: ^1.0.8
- @loopback/openapi-spec-builder: ^1.1.0
- @loopback/openapi-v3-types: ^1.0.8
- @loopback/openapi-v3: ^1.3.0
- @loopback/repository-json-schema: ^1.3.4
- @loopback/repository: ^1.1.8
- @loopback/rest: ^1.8.0
- @loopback/testlab: ^1.1.0
- @loopback/docs: ^1.10.0
- @loopback/example-hello-world: ^1.1.6
- @loopback/example-log-extension: ^1.1.6
- @loopback/example-rpc-server: ^1.1.5
- @loopback/example-todo: ^1.5.1
- @loopback/example-soap-calculator: ^1.4.1
- @loopback/service-proxy: ^1.0.10
- @loopback/http-caching-proxy: ^1.0.9
- @loopback/http-server: ^1.1.8
- @loopback/example-todo-list: ^1.5.1
- @loopback/dist-util: ^0.4.0
- @loopback/rest-explorer: ^1.1.11
- @loopback/tslint-config: ^2.0.2
- @loopback/example-express-composition: ^1.2.1
Adding answer to my own problem after I found it.
I changed the dependencies to latest. Even that I was using "theoretically" the latest it didn't work.
When I changed manually to :
"dependencies": {
"@loopback/authentication": "^1.0.16",
"@loopback/boot": "^1.1.2",
"@loopback/context": "^1.8.1",
"@loopback/core": "^1.2.1",
"@loopback/openapi-v3": "^1.3.2",
"@loopback/repository": "^1.2.1",
"@loopback/rest": "^1.9.1",
"@loopback/rest-explorer": "^1.1.13",
"@loopback/service-proxy": "^1.1.1",
"loopback-connector-postgresql": "^3.6.0",
"passport": "^0.4.0",
"passport-http": "^0.3.0"
started to work
@raymondfeng I have a similar issue and packages version doesn't seems to be the cause.
The problem started to arise in our migrate script when we added a request context bound value (that we set in a sequence action) to our repository classes. At first I get
Cannot migrate database schema Error: The key 'authentication.currentUser' is not bound to any value in context application
fine, lets bind it our migrate script (hoping I'll will not have too many of those), and then I get
Cannot migrate database schema Error: Cannot resolve injected arguments for RepositoryBase.[0]: The arguments[0] is not decorated for dependency injection, but a value is not supplied
And now I'm lost.
I'm using a similar way of setting the CURRENT_USER as in the Authentication package,
For reference, packages versions:
+-- @loopback/[email protected]
| `-- @loopback/[email protected] deduped
+-- @loopback/[email protected]
+-- @loopback/[email protected]
| `-- @loopback/[email protected] deduped
+-- @loopback/[email protected]
| +-- @loopback/[email protected] deduped
| `-- @loopback/[email protected]
| `-- @loopback/[email protected] deduped
+-- @loopback/[email protected]
| `-- @loopback/[email protected] deduped
+-- @loopback/[email protected]
| `-- @loopback/[email protected] deduped
+-- @loopback/[email protected]
| `-- @loopback/[email protected] deduped
`-- @loopback/[email protected]
`-- @loopback/[email protected] deduped
Try to run npm update.
@raymondfeng I've removed the node_modules folder, cleared npm cache, updated manually @loopback deps version and reinstalled everything with the latest version and I'm still having the exact same issue.
+-- @loopback/[email protected]
| `-- @loopback/[email protected] deduped
+-- @loopback/[email protected]
+-- @loopback/[email protected]
| `-- @loopback/[email protected] deduped
+-- @loopback/[email protected]
| +-- @loopback/[email protected] deduped
| `-- @loopback/[email protected]
| `-- @loopback/[email protected] deduped
+-- @loopback/[email protected]
| `-- @loopback/[email protected] deduped
+-- @loopback/[email protected]
| `-- @loopback/[email protected] deduped
+-- @loopback/[email protected]
| `-- @loopback/[email protected] deduped
`-- @loopback/[email protected]
`-- @loopback/[email protected] deduped
Is it a duplicate of https://github.com/strongloop/loopback-next/issues/2541?
@raymondfeng I seem to have the same issue. Using the example code results in the following error
src/application.ts:20:60 - error TS2345: Argument of type 'typeof MyAuthStrategyProvider' is not assignable to parameter of type 'Constructor
Type 'MyAuthStrategyProvider' is not assignable to type 'Provider
Types of property 'value' are incompatible.
Type '() => ValueOrPromise
Type 'ValueOrPromise
Type 'Strategy' is not assignable to type 'ValueOrPromise
Type 'Strategy' is not assignable to type 'AuthenticationStrategy'.
Types of property 'name' are incompatible.
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
20 this.bind(AuthenticationBindings.STRATEGY).toProvider( MyAuthStrategyProvider );
~~~~~~
Using the versions specified by @danysz it seems to work
just upgrading @loopback/authentication only to 2.0.2 results in the same error.
here are some details on combinations I have tried so far.
My environment: Global lb4 --version
@loopback/cli version: 1.12.1
@loopback/* dependencies:
Output of -----npm ls @loopback/context
[email protected] /home/vagrant/test/agctm
โโโฌ @loopback/[email protected] -------- upgrading this to 2.0.2 results in above error
โ โโโ @loopback/[email protected] deduped
โโโฌ @loopback/[email protected]
โ โโโ @loopback/[email protected] deduped
โโโ @loopback/[email protected]
โโโฌ @loopback/[email protected]
โ โโโ @loopback/[email protected] deduped
โโโฌ @loopback/[email protected]
โ โโโ @loopback/[email protected] deduped
โ โโโฌ @loopback/[email protected]
โ โโโ @loopback/[email protected] deduped
โโโฌ @loopback/[email protected]
โ โโโ @loopback/[email protected] deduped
โโโฌ @loopback/[email protected]
โ โโโ @loopback/[email protected] deduped
โโโฌ @loopback/[email protected]
โ โโโ @loopback/[email protected] deduped
โโโฌ @loopback/[email protected]
โโโ @loopback/[email protected] deduped
curl -X GET "http://localhost:3000/whoami" -H "accept: /"
Error: Unauthorized.
{
"error": {
"statusCode": 401,
"name": "UnauthorizedError",
"message": "Basic realm=\"Users\""
}
}
Response headers
access-control-allow-credentials: true access-control-allow-origin: * connection: keep-alive content-length: 89 content-type: application/json; charset=utf-8 date: Mon, 13 May 2019 14:54:47 GMT x-content-type-options: nosniff x-powered-by: Express
Also tried to update all the packages to the latest versions ---- npm ls @loopback/context
[email protected] /home/vagrant/test/agctm
โโโฌ @loopback/[email protected]
โ โโโ @loopback/[email protected] deduped
โโโฌ @loopback/[email protected]
โ โโโ @loopback/[email protected] deduped
โโโ @loopback/[email protected]
โโโฌ @loopback/[email protected]
โ โโโ @loopback/[email protected] deduped
โโโฌ @loopback/[email protected]
โ โโโ @loopback/[email protected] deduped
โ โโโฌ @loopback/[email protected]
โ โโโ @loopback/[email protected] deduped
โโโฌ @loopback/[email protected]
โ โโโ @loopback/[email protected] deduped
โโโฌ @loopback/[email protected]
โ โโโ @loopback/[email protected] deduped
โโโฌ @loopback/[email protected]
โ โโโ @loopback/[email protected] deduped
โโโฌ @loopback/[email protected]
โโโ @loopback/[email protected] deduped
Please let me know if you need more info.
Looking forward to some pointers in getting this resolved.
Thanks
try to change the version of your loopback-authentificate.
like tthat "@loopback/authentication": "^1.0.16",
@raymondfeng - Thx for the pointer.
@bajtos
I do have a couple of question for you.
Is there an example available for Angular - JWT user authentication that one could look at?
Any plans to support loopback-sdk-angular or any alternatives available one could investigate?
@DimitriEmman Thx, I will give 1.0.16 a try.
Is there an example available for Angular - JWT user authentication that one could look at?
If LB4 authentication is implemented I could put together an example for that with ng. It is essentially using something like auth0/angular-jwt to do leg work. I'll take a look next week if you like, dependant on the state of LB4 auth implementation.
Any plans to support loopback-sdk-angular or any alternatives available one could investigate?
Something like openapitools/openapi-generator-cli? I've been experimenting with it, it's fairly decent. There will be other tools that support openapi v3.
Is there an example available for Angular - JWT user authentication that one could look at?
If LB4 authentication is implemented I could put together an example for that with ng. It is essentially using something like auth0/angular-jwt to do leg work. I'll take a look next week if you like, dependant on the state of LB4 auth implementation.
@dougal83 Yes, a working example with loopback-authentication v1.x would be greatly appreciated.
Any plans to support loopback-sdk-angular or any alternatives available one could investigate?
Something like openapitools/openapi-generator-cli? I've been experimenting with it, it's fairly decent. There will be other tools that support openapi v3.
@dougal83 Thanks for the pointer, I will look into it.
@argupta23 Hey, I found time to put rough working example together. I think the lb4 auth is still in development, so will need to update but it works as intended. I've implemented lb4-ng combo, openapi-generator and JWT auth. Tried to put reasonable effort into readme. Let me know if there are issues that need resolving.
@argupta23 Hey, I found time to put rough working example together. I think the lb4 auth is still in development, so will need to update but it works as intended. I've implemented lb4-ng combo, openapi-generator and JWT auth. Tried to put reasonable effort into readme. Let me know if there are issues that need resolving.
@dougal83. This is great, I tested it and it works. I used mongodb instead.
One quick note, please add "isemail" package to the server side package.json file.
Would it be possible for me to contact you directly? this way I am not hijacking this thread.
Thanks
@argupta23 I've added email to my git profile. Anything regarding that repo though, discus on issues there incase it helps others. Will add dependency cheers.
@argupta23 I've added email to my git profile. Anything regarding that repo though, discus on issues there incase it helps others. Will add dependency cheers.
@dougal83, I have sent you an email. thx.
@raymondfeng related to my comment above https://github.com/strongloop/loopback-next/issues/2582#issuecomment-491085786, I found the cause of my problem.
This is because I have a custom RepositoryBase and it was named base.repository.ts which causes this file to be automatically bound and instanciated by LB, renaming the file solved the issue. I'm actually surprised to see all repo being instanciated even if they are not injected in any other class.
update everything to latest version and auth is broken again ...
this is driving crazy.
"@loopback/authentication": "^2.1.0",
"@loopback/boot": "^1.3.0",
"@loopback/cli": "^1.15.1",
"@loopback/context": "^1.18.0",
"@loopback/core": "^1.8.0",
"@loopback/openapi-v3": "^1.6.0",
"@loopback/repository": "^1.6.0",
"@loopback/rest": "^1.14.0",
"@loopback/rest-explorer": "^1.2.0",
"@loopback/service-proxy": "^1.2.0",
"firebase-admin": "^8.0.0",
"loopback-connector-postgresql": "^3.6.1",
"passport": "^0.4.0",
"passport-firebase-auth": "0.0.0-beta.1",
"passport-http": "^0.3.0",
"uuid": "^3.3.2"
any idea ?
Please see the discussion in https://github.com/strongloop/loopback-next/issues/2886 .
At the moment the passport support for @loopback/authentication 2.x is not available, but is being worked on.
loopback4-example-shopping was updated to use @loopback/authentication 2.x (but doesn't use passport).
In my case, I had a problem like this one using an abstract class for SoftCrudRepository and SoftDeleteEntity.
rm -rf dist && npm run clean && npm run build
solved
Most helpful comment
Adding answer to my own problem after I found it.
I changed the dependencies to latest. Even that I was using "theoretically" the latest it didn't work.
When I changed manually to :
"dependencies": { "@loopback/authentication": "^1.0.16", "@loopback/boot": "^1.1.2", "@loopback/context": "^1.8.1", "@loopback/core": "^1.2.1", "@loopback/openapi-v3": "^1.3.2", "@loopback/repository": "^1.2.1", "@loopback/rest": "^1.9.1", "@loopback/rest-explorer": "^1.1.13", "@loopback/service-proxy": "^1.1.1", "loopback-connector-postgresql": "^3.6.0", "passport": "^0.4.0", "passport-http": "^0.3.0"started to work