-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Inject repository in auth provider #1835
Comments
You probably don't have the repository bound to the context when the |
@ericalves can you try to inject a repository getter instead of the repository class? I don't remember the implementation details of export class MyAuthStrategyProvider implements Provider<Strategy | undefined> {
constructor(
@repository.getter(AccesstokenRepository)
public getAccesstokenRepository: Getter<AccesstokenRepository>,
@inject(AuthenticationBindings.METADATA) private metadata: AuthenticationMetadata,
) { }
// ...
verify2(
req: any,
username: string,
password: string,
done: (error: any, user?: any) => void,
) {
const repo = this.getAccesstokenRepository();
// etc.
}
} |
I have the same problem as @ericalves. When I have tried your last solution, the problem has not disappeared. I have checked the value of 'this' variable and I have this value: "BasicStrategy { success: [Function], fail: [Function], error: [Function] }". I think that the context inside of 'Verify' function is not the same of the main class 'MyAuthStrategyProvider'. |
If one of you can create a sample repo to reproduce the problem, we can help troubleshoot. |
I have a model (login.model.ts) with the structure data of users and the repository class (login.repository.ts) with this definition:
In the strategy provider class i have this:
I follow the instructions defined in the readme of '@loopback/authentication': https://github.com/strongloop/loopback-next/tree/master/packages/authentication |
@lolocr Thank you for the information. I would appreciate if you can create a github repo with a LB4 app and your code to show the problem. It would be very helpful for me to check out the repo and debug it. |
You can find the repo here: https://github.com/lolocr/lb4auth |
@lolocr I'll take a look |
@ericalves I think the root cause is the following: return new BasicStrategy({
realm: '',
passReqToCallback: true
}, this.verify2); Please note the Can you try the following fix? return new BasicStrategy({
realm: '',
passReqToCallback: true
}, this.verify2.bind(this)); |
BTW, I confirmed in debugger that LB4 dependency injection of the repository works correctly in the |
It works perfectly! |
@raymondfeng thank you man |
Related: #1826
I am trying to validate if the token passed in the header of the request is valid, for this I need to find in the database.
I'm trying to inject the repository, but the same gets as undefined. How should I proceed?
It's in my controller and it works:
The text was updated successfully, but these errors were encountered: