Skip to content
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

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

Closed
kanusoni opened this issue May 13, 2020 · 21 comments
Assignees

Comments

@kanusoni
Copy link

kanusoni commented May 13, 2020

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.

@kanusoni kanusoni added the bug label May 13, 2020
@achrinza achrinza added question and removed bug labels May 13, 2020
@dougal83
Copy link
Contributor

dougal83 commented May 13, 2020

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
Copy link
Author

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
Copy link
Contributor

dougal83 commented May 13, 2020

@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
Copy link
Member

dhmlau commented May 13, 2020

@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
Copy link
Author

@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
Copy link
Author

@dhmlau can you help me out in the below issue:-
while i am signing in loopback4 explorer with credentials email: [email protected] 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 deepakrkris self-assigned this May 14, 2020
@deepakrkris
Copy link
Contributor

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

@dhmlau
Copy link
Member

dhmlau commented May 14, 2020

@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
Copy link
Author

kanusoni commented May 15, 2020 via email

@kanusoni
Copy link
Author

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

@dougal83
Copy link
Contributor

@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.

@loopbackio loopbackio deleted a comment from kanusoni May 15, 2020
@kanusoni
Copy link
Author

kanusoni commented May 15, 2020

@dougal83 shared the code in github repo.

@dougal83
Copy link
Contributor

@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...

          schema: getModelSchemaRef(NewUserRequest, {
            title: 'NewUser',
            exclude: ['id','role']
          }),

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
Copy link
Author

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

@kanusoni
Copy link
Author

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

@jannyHou
Copy link
Contributor

@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 - #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 #5421.

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

@HrithikMittal
Copy link
Contributor

Hey @jannyHou Here my example can close this issue please do it if possible #5573
#5595

@dhmlau dhmlau assigned jannyHou and unassigned deepakrkris Jun 4, 2020
@jannyHou
Copy link
Contributor

@HrithikMittal thanks! I think this issue is more like a question, and I think my comment above #5407 (comment) 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
Copy link

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
Copy link
Member

achrinza commented Dec 25, 2020

@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
Copy link
Contributor

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

No branches or pull requests

9 participants