REST API JWT login

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
vintoICT
User
Posts: 407

REST API JWT login

Post 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


arbei
User
Posts: 9355

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


vintoICT
User
Posts: 407

Post 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 ?


arbei
User
Posts: 9355

Post by arbei »

If you want to validate the user yourself, you may, but your code won't work because:

  1. Your code for the SQL is incorrect,
  2. Your login code does not return JWT token (if you don't need it, it is acceptable though),
  3. Your code won't work with the Advanced Security (User ID and User Level) (if you don't need it, it is acceptable though),
  4. The "userid" is only an integer or string, but you return it as JSON.

vintoICT
User
Posts: 407

Post 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


vintoICT
User
Posts: 407

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


Post Reply