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
REST API JWT login
-
- User
- Posts: 408
REST API JWT login
-
- User
- Posts: 9405
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.
-
- User
- Posts: 408
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 ?
-
- User
- Posts: 9405
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.