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

11

6

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.

Fisc answered 11/5, 2020 at 4:30 Comment(3)
Did you find the solution??Iz
yes, but i forgot. sorry broFisc
I solved it, ran this example github.com/leandrouti/laravel-passport-sample and compared it with my code, did some changes and it works. Is Laravel 5 but it is adaptable.Iz
B
9

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.

Bohaty answered 6/8, 2020 at 23:37 Comment(1)
IF you want to use only personal tokens, run => php artisan passport:client --personalGosh
N
5

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

Nephritic answered 18/6, 2020 at 5:39 Comment(0)
N
2

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.

Northeastward answered 10/12, 2020 at 9:17 Comment(0)
H
2

Run the command: php artisan passport:install --force The output will be something like:

Encryption keys generated successfully. Personal access client created successfully.
Client ID: 24332432-2343-3423-8c52-2342333
Client secret: dfgdfgfdgfdgdfdfBvV3esUczLMkWsJ
Password grant client created successfully.
Client ID: 92342333-4233-4343-9234-32423
Client secret: D3FD5fd5f5dmfmdDmffDmffmdDmfdmdn

Open the .env file and paste the client-id and the client secret from the password-access-client lines above like this:

PASSPORT_PERSONAL_ACCESS_CLIENT_ID=24332432-2343-3423-8c52-2342333 PASSPORT_PERSONAL_ACCESS_CLIENT_SECRET=dfgdfgfdgfdgdfdfBvV3esUczLMkWsJ

Handclasp answered 28/1, 2023 at 15:3 Comment(0)
G
1

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"
Guardroom answered 15/12, 2020 at 5:15 Comment(1)
In fact, that was it for me. I set up a unit-test scenario with a fresh db, called php artisan passport:install but since the old CLIENT-info was still in .env it did not work until I removed it.Centripetal
K
0

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.

Kozhikode answered 11/5, 2020 at 4:39 Comment(2)
yes it wasn't empty, there are 2 Laravel_personal_access_client thing.Fisc
I took a look at my tables and found that in oauth_clients I have 1 row, but the user_id appears nullIz
T
0

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

Tayler answered 11/5, 2020 at 4:51 Comment(2)
wait, I'll try it. so does that mean using password grant in laravel passport is a must?Fisc
It doesn't work. I think it doesn't have anything to do with grant password because I've seen it on youtube it works fine without grantFisc
P
0

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
Phytography answered 8/11, 2021 at 7:56 Comment(0)
S
0

I just had a similar error with Laravel ^10.15 / Passport ^11.8. I received an 'Attempt to read property "secret" on null' error when trying to generate a token. The user would be created, but the token generation would fail. After digging in, I found a mismatch of client id values in the DB between the oauth_clients and oauth_personal_access_clients tables. Once I ensured the oauth_clients record id column matched the oauth_personal_access_clients client_id value, the error resolved.

In my case, this defect occurred from manually updating keys and only updating the record in one table.

Spurious answered 23/2 at 22:9 Comment(0)
L
0

You can't create $accessToken = $user->createToken('authToken')->accessToken; if you are trying to create a password client. Instead, try to do this.

run:

php artisan passport:keys

this command going to create to files at your storage. oauth-private.key and oauth-public.key

in your AppServiceProvider.php file add:

/**
 * Bootstrap any application services.
 */
public function boot(): void
{
    Passport::enablePasswordGrant();
    Passport::loadKeysFrom(__DIR__.'/../../secrets/oauth'); // I moved the files from storage to screts/oauth foldes that I created at the root.
}

In your controller:

use Illuminate\Support\Facades\Http;

 $response = Http::asForm()->post('http://api.localhost/oauth/token', [
        'grant_type' => 'password',
        'client_id' => 11, // grab this from oauth_clients table
        'client_secret' => 'xQnAFRLaemPSAXn8Zud1YP8WoDEf3K9WxG3THiof', // also this
        'username' => $credentials["email"],
        'password' => $credentials["password"],
        'scope' => '',
    ]);

    return $response->json();

if everthing is ok than it will return something like this:

{
    "token_type": "Bearer",
    "expires_in": 31536000,
    "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxMSIsImp0aSI6IjYzMGM2MjAyNWRiODVkZmE4ODhlMzE3ZjE0OWY0NjZiODM4NmQxOTcxMmVkMDIwZmJmZTk1MzNmMzlhOTE5YWRiNDFjNDQyMTQ1MGEzYTcyIiwiaWF0IjoxNzE5NjAxNzU3LjAzMjIxOSwibmJmIjoxNzE5NjAxNzU3LjAzMjIyMSwiZXhwIjoxNzUxMTM3NzU3LjAxNzY2OCwic3ViIjoiMSIsInNjb3BlcyI6W119.Mjv4juMKlYJUvaB-UOQP1nm5aAuA9OLLV_m4hXW9LKL3hFjELC24nnsGzkCzOO-NzfqkU9kGffuT1Kd7M3P5RQ9TO0-7rqkG6vGgA7q1034C_HZi-io-umLzs9-eB-Rbf0bChTfQI7pDycpoRcb2r2AuoeaDoPjthQQ4AbWuz0XaC47H-8Jz-01NEQwW1H43bvD-ci4-v1RYiVRhRxLiS4LrHRBXV6Dfr_8FNcJqi3WUH2jsSLg5b1iXwwQ6OssEY0UR5eoj7Wwfkt9ZsyUgChSkxQ-9Bk2zOydG0KlFFsRz0Bf3e471eICAS5MXVAeYmGWbMvO80RK0qFNg41al-dwe2F6e-IBsxnIeP1l1StA7sBD-JZfTRkZMUhF926x5-H6sPzyTzXCIbLW-WTLX4LTg6d43tTyMibakLZpKxbIy91enG0-AWFcAwHoP5X41xPlS3Da8-RA7L1C8OTqKpd7JOxHOTooyd4RqJ8040a4eT3cemDkNK5S-8kdfXC_T4IinSZczjU8S_T-6gl1f5PMsL3w82xaekAM78sBWrvse2JVUODTci--7x0ApzumkOB08GIVpCWAezbXRQCcWZcX8s33MPI-hAFKWCM20TpmPjnscRLo5sogddNBCHvSVk7guYXuUtinCoxN_u2ih7WxP0dQVUXfMnGV5Iyjrzhk",
    "refresh_token": "def502002642ef65cb7da4fae2dd0f813337abe1402942ad9bb2c6e397e1060c170794f0a093d3af6efe6b07c3bcf055a9ada7e41574433af02c5d138237ca257c23ffc3ef0449cca2a8eef9ee611f6899ef7909c8040d51ba634e662500602f66ca475e38beb0a39d6ca9180e2293dbd871896f5c0344a3aa738b889820c92cac689bb46c20610b07b86723a82838fade6bed6add9aa682901f7667a1b05db824c5b7ec49457fa2cb46758e23934a210d25e638bd5910e891a6623967b56bd77351e04843360cfa9c1a3236fdcfedde494854951543f3b6923c8133dd2c601bf010a50c7a657e22c301b1edf8a8359a50b138abd18121377fda61e96bfb1e03646e13832db0ba8ce5ce34151044f59aaef486ee2f52e2b254b54d3ed0505d4166f24d4f0cf13d74212744759eb8afd9ed802c6d345472ae730026874052e5f22b3192b3fc6774eee97d310c4b7e509ec5f80cb3c3fcee3667bf8450051e41f89d6b"
}
Lynsey answered 28/6 at 19:25 Comment(0)
D
0

If the above solutions doesn't work, try running the following commands in the terminal within the code folder:


    php artisan config:clear
    php artisan config:cache
    php artisan cache:clear

Daric answered 11/8 at 20:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.