laravel / sanctum

Laravel Sanctum provides a featherweight authentication system for SPAs and simple APIs.
https://laravel.com/docs/sanctum
MIT License
2.76k stars 298 forks source link

Method Illuminate\\Auth\\RequestGuard::loginUsingId does not exist. #282

Closed Rasool-deldar closed 3 years ago

Rasool-deldar commented 3 years ago

Description:

I want to use Laravel Synctum for multiple authentication ( spa ) when I use the sanctum drive.

These are my codes

// auth.php
 'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'admin' => [
            'driver' => 'sanctum',
            'provider' => 'admins',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],
    ],
// controller
 public function verification(Request $request) {
        // check verification user
        $checkVerification = $this->checkVerification($request);

        // get user & login
        $admin = $this->adminRepository->firstWithSelect([
            'mobile' => $checkVerification->mobile,
        ], ['id']);

        Auth::guard('admin')->loginUsingId($admin, true);

        // auth()->guard('admin')->loginUsingId($admin, true);

        $this->userVerificationRepository->update([
            'code_verification' => $request->post('code'),
            'guard_type' => 'admin',
        ], [
            'code_verification' => 0,
        ]);

        $request->session()->regenerate();

        // return data
        return response()->notification('successful');
    }
// api.php
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});

But when I put the drive on my sync, it gives the following error

 "message": "Method Illuminate\\Auth\\RequestGuard::loginUsingId does not exist.",
    "exception": "BadMethodCallException",
    "file": "C:\\Dev\\Web\\Laragon\\www\\digikala\\digikala-api\\vendor\\laravel\\framework\\src\\Illuminate\\Macroable\\Traits\\Macroable.php",
    "line": 103,
    "trace": [
        {
            "file": "C:\\Dev\\Web\\Laragon\\www\\digikala\\digikala-api\\Modules\\Admin\\Http\\Controllers\\AuthController.php",
            "line": 101,
            "function": "__call",
            "class": "Illuminate\\Auth\\RequestGuard",
            "type": "->"
        },
        {
            "file": "C:\\Dev\\Web\\Laragon\\www\\digikala\\digikala-api\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Controller.php",
            "line": 54,
            "function": "verification",
            "class": "Modules\\Admin\\Http\\Controllers\\AuthController",
            "type": "->"
        },
        {
            "file": "C:\\Dev\\Web\\Laragon\\www\\digikala\\digikala-api\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\ControllerDispatcher.php",
            "line": 45,
            "function": "callAction",
            "class": "Illuminate\\Routing\\Controller",
            "type": "->"
        },
   ]

When I drive the session, it gives the following error.

{
    "message": "Unauthenticated."
}
driesvints commented 3 years ago

This method indeed doesn't exists on the Sanctum guard. If you need this you'd probably want a session guard instead.