Page 1 of 1
REST API JWT login
Posted: Wed Nov 17, 2021 10:52 am
by vintoICT
I'm using the Rest Api with vue is. and axios. / register and /add are working well. But login is is not doing what I want. When I test with swagger , on success ,it only return token. I need the userid also. Something like response.userid .
Thanks
Re: Rest Api JWT login issue
Posted: Wed Nov 17, 2021 12:39 pm
by arbei
The login API action returns the JWT token so that you can make further requests. You need to create your own API action to get custom information. To check JWT and then return custom info you may add action with JwtMiddleware, e.g.
$app->get('/myaction', function ($request, $response, $args) { ... })->add(JwtMiddleware::class . ':create');
Alternatively, you may also create your own login action which returns additional info.
Re: Rest Api JWT login issue
Posted: Wed Nov 30, 2022 9:59 pm
by vintoICT
function Api_Action($app)
{
$app->get('/mylogin', function ($request, $response, $args) {
$response = $response->withJson(ExecuteScalar("SELECT userid FROM users WHERE username = ''. $rsnew["username"].'' ");); // Write to response
return $response; // Return Psr\Http\Message\ResponseInterface object
});
}
i'm trying to add where clause of username inputed . Is this the right approach ?
Re: REST Api JWT login issue
Posted: Thu Dec 01, 2022 9:46 am
by arbei
If you want to validate the user yourself, you may, but your code won't work because:
- Your code for the SQL is incorrect,
- Your login code does not return JWT token (if you don't need it, it is acceptable though),
- Your code won't work with the Advanced Security (User ID and User Level) (if you don't need it, it is acceptable though),
- The "userid" is only an integer or string, but you return it as JSON.
Re: REST API JWT login
Posted: Sat Dec 03, 2022 6:53 pm
by vintoICT
Thanks , What i need summarily is my /login (The default login api) request to return both token and user table values as json.
token: zskdkdk......
User id :1
Username : Ben
.....
}
I need my Api to return that.
Thank you
Re: REST API JWT login
Posted: Wed Dec 07, 2022 5:00 pm
by vintoICT
I understand this now . I have been approching it the wrong way . / login is to authenticate, my /customednpoint takes all logic of phpmaker. Thank you .