Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel Sanctum login issue

I am using laravel sanctum. I have created a login API and I am getting this error when i am creating Token

ArgumentCountError: Too few arguments to function App\Models\User::createToken(), 0 passed in project\app\Http\Controllers\API\AuthController.php on line 27 and at least 1 expected in file D:\laragon\www\laravel-8-react-js\vendor\laravel\sanctum\src\HasApiTokens.php on line 44

can anyone help me? Thank You

Here is my Code 
public function login(Request $request)
{
    $validator = Validator::make($request->all(), [
        'email' => 'required|email',
        'password' => 'required',
    ]);

    if ($validator->fails()) {
        return $this->handleError($validator->errors()->first());
    }

    $credentials = $request->only(['email', 'password']);
    if ($user = auth()->guard()->attempt($credentials)) {
        $auth = auth()->user();
        $result['token'] = $auth->createToken()->plainTextToken;
        $result['user'] = $auth;
        $res = [
            'bool' => true,
            'result' => $result,
            'message' => "Login Successful",
        ];
        return response()->json($res, 200);
    }
    return response()->json([
        'bool' => false,
        'message' => "Login Failed"
    ]);
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

the createtoken method requires input parameters. depending on the initial conditions. this is in the error.
Like this createToken($request->token_name)
Or with abilities

createToken('token-name', ['server:update'])

See doc sanctum token

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading