Home > Net >  Auth guard [api] is not defined
Auth guard [api] is not defined

Time:02-21

I checked several questions with the same error above I mentioned. But couldn't find the answer. I'm trying to turn my base64 string into an image file. Here is the code in my controller,

public function createImage(Request $request)
    {
      $user = auth('api')->user();
      if($request->photo){
         $name = time().'.'. explode('/', explode(':', substr($request->photo, 0, strpos
         ($request->photo, ';')))[1])[1];
         \Image::make($request->photo)->save(public_path('public/').$name);
        }
    }

When executing this using base64 encoded string via Postman below error appears,

"message": "Auth guard [api] is not defined.",
    "exception": "InvalidArgumentException",
    "file": "C:\\xampp\\htdocs\\konekt\\testmyusers-api\\vendor\\laravel\\framework\\src\\Illuminate\\Auth\\AuthManager.php",

I don't know my controller code is right or not. I used a youtube video as the source. Can I do this way.? or should I change whole my method. If anyone knows a solution please help me.

CodePudding user response:

I think you have not added guard api in config/auth.php. If not added then please add below guard with driver and provider details in config/auth.php.

'guards' => [
        'api' => [
            'driver' => 'passport',
            'provider' => 'user',
        ],
    ]

CodePudding user response:

My team had a lot of similar issues using the Passport library for API, so if you're using that, and don't need Passport's level of complexity of OAuth, I would recommend looking into Sanctum

  • Related