Search code examples
phplaraveleloquentlaravel-passport

Laravel Passport Trying to get property 'secret' of non-object


I'm using laravel and trying to make an Authenthication with Laravel passport. So I've done it by looking on the docs and youtube but I got this error. this is my AuthController that I requested and the error.

AuthController.php

public function register(Request $request) 
    {
        $validatedData = $request->validate([
            'name'=>'required|max:55',
            'email'=>'email|required|unique:users',
            'password'=>'required|confirmed',
            'who'=>'required'
        ]);

        $validatedData['password'] = bcrypt($request->password);

        $user = User::create($validatedData);

        // Get access token
        $accessToken = $user->createToken('authToken')->accessToken;

        return response(['user' => $user, 'access_token' => $accessToken]);
    }

ErrorsException

{
    "message": "Trying to get property 'secret' of non-object",
    "exception": "ErrorException",
    "file": "C:\\Panji\\xampp\\htdocs\\papa\\vendor\\laravel\\passport\\src\\PersonalAccessTokenFactory.php",
    "line": 96,
    "trace": [
        {
            "file": "C:\\Panji\\xampp\\htdocs\\papa\\vendor\\laravel\\passport\\src\\PersonalAccessTokenFactory.php",
            "line": 96,
            "function": "handleError",
            "class": "Illuminate\\Foundation\\Bootstrap\\HandleExceptions",
            "type": "->"
        },
        {
            "file": "C:\\Panji\\xampp\\htdocs\\papa\\vendor\\laravel\\passport\\src\\PersonalAccessTokenFactory.php",
            "line": 71,
            "function": "createRequest",
            "class": "Laravel\\Passport\\PersonalAccessTokenFactory",
            "type": "->"
        },
        {
            "file": "C:\\Panji\\xampp\\htdocs\\papa\\vendor\\laravel\\passport\\src\\HasApiTokens.php",
            "line": 67,
            "function": "make",
            "class": "Laravel\\Passport\\PersonalAccessTokenFactory",
            "type": "->"
        },
        {
            "file": "C:\\Panji\\xampp\\htdocs\\papa\\app\\Http\\Controllers\\Api\\AuthController.php",
            "line": 26,
            "function": "createToken",
            "class": "App\\User",
            "type": "->"
        },
        {
            "function": "register",
            "class": "App\\Http\\Controllers\\Api\\AuthController",
            "type": "->"
        },
        {
            "file": "C:\\Panji\\xampp\\htdocs\\papa\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Controller.php",
            "line": 54,
            "function": "call_user_func_array"
        },
        {
            "file": "C:\\Panji\\xampp\\htdocs\\papa\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\ControllerDispatcher.php",
            "line": 45,
            "function": "callAction",
            "class": "Illuminate\\Routing\\Controller",
            "type": "->"
        },
        {
            "file": "C:\\Panji\\xampp\\htdocs\\papa\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Route.php",
            "line": 225,
            "function": "dispatch",
            "class": "Illuminate\\Routing\\ControllerDispatcher",
            "type": "->"
        },
.
.
.
}

I've searched it on google but I can't found anything that mention Trying to get property 'secret' of non-object. I've tried php artisan passport:install so I got the personal_access_clients, but nothing's work.

NB

it's actually registered the user, but got this error response.


Solution

  • THIS IS FOR LARAVEL 7 AND LARAVEL/PASSPORT 9.0

    The first thing is to run the php artisan passport:client --personal.

    Inside your database table oauth_clients, under column name, look for Laravel Personal Access Client.

    Copy the secret beside Laravel Personal Access Client.

    Open AuthServiceProvider, then paste the secret inside the boot method where the CLIENT_SECRET is below:

    Passport::personalAccessClientSecret(config('CLIENT_SECRET'));
    

    And don't forget to also add the ID of the secret from your database.

    Passport::personalAccessClientId(config('ID'));
    

    Note: Use the quote along with the ID and CLIENT_SECRET as config() is expected to get a string.