loopbackio / loopback-next

LoopBack makes it easy to build modern API applications that require complex integrations.
https://loopback.io
Other
4.95k stars 1.07k forks source link

how we can add login method in usercontroller class in loopback4 API? needs more clarification on this #5407

Closed kanusoni closed 4 years ago

kanusoni commented 4 years ago

Hello, I have created User controller in loopback4 Explorer but i need login method for JWT authentication. Whatever is written in loopback.io tutorial is bit confusing so do you have any other tutorial for the clarification.

dougal83 commented 4 years ago

Please find a working example below:

  @post('/users/login', {
    responses: {
      '200': {
        description: 'Token',
        content: {
          'application/json': {
            schema: {
              type: 'object',
              properties: {
                token: {
                  type: 'string',
                },
              },
            },
          },
        },
      },
    },
  })
  async login(
    @requestBody(CredentialsRequestBody) credentials: Credentials,
  ): Promise<{token: string}> {
    // ensure the user exists, and the password is correct
    const user = await this.userService.verifyCredentials(credentials);

    // convert a User object into a UserProfile object (reduced set of properties)
    const userProfile = this.userService.convertToUserProfile(user);

    // create a JSON Web Token based on the user profile
    const token = await this.jwtService.generateToken(userProfile);

    return {token};
  }
}

From example shopping. Hope that helps. Please explain what is confusing and we'll see what can be done to improve docs.

kanusoni commented 4 years ago

yes, i have tried this...but i am getting errors. The variables that you are using like (Credentials, userservice, jwtservice) where we need to define this in our application

Error is like:- Parameter 'credentials' of public method from exported class has or is using private name 'Credentials'.

Property 'userService' does not exist on type 'UserController'.

dougal83 commented 4 years ago

@kanusoni Please take a closer look at the Shopping example and follow the breadcrumbs (look at Constructor that instantiates services and follow the imports to find their implementation. It takes time to familiarise yourself but it is worth it if the tutorial does not suit you.

dhmlau commented 4 years ago

@kanusoni, we recently published an extension called @loopback/authentication-jwt, this might make adding jwt authentication eaiser: https://github.com/strongloop/loopback-next/tree/master/extensions/authentication-jwt.

While learning how to use this extension, I created this blog: https://medium.com/@MobileDiana/add-jwt-authentication-in-your-loopback-4-application-325137642c2c. Hope this helps.

kanusoni commented 4 years ago

@dhmlau Thanks Diana for you reply. The blog which has been mentioned in above comment section is really helpful. now i am able to create both login and signup method in usercontroller class. Thanks Again for publishing this blog.

kanusoni commented 4 years ago

@dhmlau can you help me out in the below issue:- while i am signing in loopback4 explorer with credentials email: foo@bar.com and password: foobar i am getting error which is shown in screenshot.

Screenshot (2)

because of this issue JWT token is also not generating. your help will be appreciated. Thanks.

deepakrkris commented 4 years ago

@kanusoni please provide a sample project or sandbox to check your code

dhmlau commented 4 years ago

@kanusoni please provide a sample project or sandbox to check your code

+1. From the error in the screen shot, it seems like the password was not specified. If it was specified and still got this error, it's the best if you can share your app. Thanks.

kanusoni commented 4 years ago

newlbapp.zip https://drive.google.com/file/d/1ydc8jzw3VRHhaQ_PGn1drz5FJZ3_qsaB/view?usp=drive_web

On Fri, May 15, 2020 at 5:28 AM Diana Lau notifications@github.com wrote:

@kanusoni https://github.com/kanusoni please provide a sample project or sandbox to check your code

+1. From the error in the screen shot, it seems like the password was not specified. If it was specified and still got this error, it's the best if you can share your app. Thanks.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/strongloop/loopback-next/issues/5407#issuecomment-628946969, or unsubscribe https://github.com/notifications/unsubscribe-auth/APSACQH3LNNDATC3TP6QOLDRRSASDANCNFSM4M7OK7VQ .

kanusoni commented 4 years ago

@deepakrkris @dhmlau sent App to you. please check the code and let me know the solution. Thanks.

dougal83 commented 4 years ago

@kanusoni FYI it is much easier to use github to share your project. See guide on making a repo from existing project. That gives helpful souls a single command line option to pull your project. Best of luck.

kanusoni commented 4 years ago

@dougal83 shared the code in github repo.

dougal83 commented 4 years ago

@kanusoni I'm a little unsure how you've arrived at your implementation, it seems a little confused. Are you following a particular tutorial? It may be worth starting afresh following the tutorial mentioned above by Diana or use Shopping Example as a reference to patch up your current project and methodically inspecting models/controllers at this point.

That being said there are several issues: Firstly you may wish to proceed to implement the credentials model repository as the password will not be stored following the advice below...

I would highly recommend using the Shopping Example as a reference to patch up your work further.

Good luck. Hope that helps a bit.

kanusoni commented 4 years ago

@dougal83 i am following this Shopping example tutorial only. And After changing the above mentioned lines, its not working fine yet. Thanks

kanusoni commented 4 years ago

@dhmlau any update from your side on the code which i sent to you?

jannyHou commented 4 years ago

@kanusoni I had a quick look of your repo, seems you don't secure any of your endpoints with @authenticate(), that's probably the issue.

FYI - https://github.com/strongloop/loopback-next/pull/5463 is the latest working example that demos JWT AUTH only. The shopping example shows a more complicated scenario so it has noisy code for other stuff.

And that example's corresponding tutorial doc https://github.com/strongloop/loopback-next/pull/5421.

Let me know if the latest example and tutorial still confuse you.

HrithikMittal commented 4 years ago

Hey @jannyHou Here my example can close this issue please do it if possible https://github.com/strongloop/loopback-next/issues/5573 https://github.com/strongloop/loopback-next/issues/5595

jannyHou commented 4 years ago

@HrithikMittal thanks! I think this issue is more like a question, and I think my comment above https://github.com/strongloop/loopback-next/issues/5407#issuecomment-632857905 points out the problem and provides a much easier tutorial to begin with authentication.

I am closing it. Feel free to reopen the issue if you still have problem :)

Arathy-sivan commented 3 years ago

src/controllers/ctr-users.controller.ts:75:18 - error TS2345: Argument of type '{ description: string; required: boolean; content: { 'application/json': { schema: { type: string; required: string[]; properties: { login: { type: string; }; password: { type: string; minLength: number; }; }; }; }; }; }' is not assignable to parameter of type 'Partial'. Types of property 'content' are incompatible. Type '{ 'application/json': { schema: { type: string; required: string[]; properties: { login: { type: string; }; password: { type: string; minLength: number; }; }; }; }; }' is not assignable to type 'ContentObject'. Property ''application/json'' is incompatible with index signature. Type '{ schema: { type: string; required: string[]; properties: { login: { type: string; }; password: { type: string; minLength: number; }; }; }; }' is not assignable to type 'MediaTypeObject'. Types of property 'schema' are incompatible. Type '{ type: string; required: string[]; properties: { login: { type: string; }; password: { type: string; minLength: number; }; }; }' is not assignable to type 'ReferenceObject | SchemaObject | undefined'. Type '{ type: string; required: string[]; properties: { login: { type: string; }; password: { type: string; minLength: number; }; }; }' is not assignable to type 'SchemaObject'. Types of property 'type' are incompatible. Type 'string' is not assignable to type '"string" | "number" | "boolean" | "object" | "integer" | "null" | "array" | undefined'.

75 @requestBody(CredentialsRequestBody) credentials: Credentials,



How to solve this error ??
achrinza commented 3 years ago

@Arathy-sivan Please ask in the Slack channel, StackOverflow with #loopback4 tag, or open a new GitHub issue. That way, it would be easier to discover and respond.

raymondfeng commented 3 years ago

See https://loopback.io/doc/en/lb4/changelog.openapi-v3.html#breaking-changes