Closed kanusoni closed 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.
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'.
@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.
@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.
@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.
@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.
because of this issue JWT token is also not generating. your help will be appreciated. Thanks.
@kanusoni please provide a sample project or sandbox to check your code
@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.
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 .
@deepakrkris @dhmlau sent App to you. please check the code and let me know the solution. Thanks.
@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.
@dougal83 shared the code in github repo.
@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.
@dougal83 i am following this Shopping example tutorial only. And After changing the above mentioned lines, its not working fine yet. Thanks
@dhmlau any update from your side on the code which i sent to you?
@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.
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
@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 :)
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
75 @requestBody(CredentialsRequestBody) credentials: Credentials,
How to solve this error ??
@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.
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.