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

The token in issueToken function always return null even though the body has its content #10

Open
eternalBlast opened this issue Mar 6, 2020 · 1 comment

Comments

@eternalBlast
Copy link

Here is CustomerTokenAuthController@issueToken function

public function issueToken(ServerRequestInterface $request)
     {
         $body = (parent::issueToken($request)); // got the response header with its content of access and refresh tokens
         $token = json_decode($body, true); //return null
        
         if (array_key_exists('error', $token)) {
             return response()->json([
                 'error' => $token['error'],
                 'status_code' => 401
             ], 401);
         }
         
        $data = $request->getParsedBody();
        
        $email = $data['username'];  
           
        switch ($data['provider']) {
            case 'customers';
                
                try {
                
                 $user = Customer::where('username', $email)->firstOrFail();
                 
                } catch (ModelNotFoundException $e) {
                  return response()->json([
                      'error' => $e->getMessage(),
                      'status_code' => 401
                  ], 401);
                }
            
                break;
            
            default :
            
                try {
                
                 $user = User::where('email', $email)->firstOrFail();
                 
                } catch (ModelNotFoundException $e) {
                  return response()->json([
                      'error' => $e->getMessage(),
                      'status_code' => 401
                  ], 401);
                }        
        }
@eternalBlast
Copy link
Author

eternalBlast commented Mar 7, 2020

Got a simple hack. But, the solution is not very elegant. At least, we can get the token object at the end.
These are the steps:

  1. Convert $body toString.
    $body = (parent::issueToken($request))->__toString();

  2. Manipulate the string by removing blank and /r characters, then retrieve position start of open curly brackets.

$res=preg_replace('/\s+/', '',$body);
$res = substr($res, strpos($res, "{"), strpos($res, "}")); //start from position of open curly bracket till the end of curly bracket , you can trim the third param if you want. It's just for semantic purpose. :) 
$token = json_decode( $res, true );

If someone can give a greater solution, maybe can post here.

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

No branches or pull requests

1 participant