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

Viewed 7445

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.

7 Answers

Run this command: php artisan passport:install --force and then try again.

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.

Encountered a similar error while using Passport in the project. Fetching response while registration of user from mobile app - registration using OTP instead of password.

For me, the error arose due to doing a php artisan migrate:fresh as this was deleting all oauth data present in the DB.

So, just using php artisan passport:client --personal after running fresh-migrations, the error was solved.

The only code in Passport that uses a property by that name is the model Laravel\Passport\Client that maps to oauth_clients DB table, so I suggest checking if that table is created and accessible for your app.

if you only want to make an authenthication for users, the passport is overkill. however, if you want passport, use password grant in this case. you are using authorization_code grant type and the secret client key lost in your code
https://laravel-news.com/passport-grant-types

Removing these 2 lines from .env file works for me

PASSPORT_PERSONAL_ACCESS_CLIENT_ID="client-id-value"
PASSPORT_PERSONAL_ACCESS_CLIENT_SECRET="unhashed-client-secret-value"

i just added Passport Personal Access Client details in .env file

PASSPORT_PERSONAL_ACCESS_CLIENT_ID=94d36aec-fca4-46dd-b851-b4ca72a1cfe9
PASSPORT_PERSONAL_ACCESS_CLIENT_SECRET=C73SddSiirwKZzo3JxG0NcwNkASyOEynQq8bTQ3o
Related