Reputation: 809
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.
Upvotes: 6
Views: 13959
Reputation: 51
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
Upvotes: 0
Reputation: 11
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"
}
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 1774
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
Upvotes: 3
Reputation: 910
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
Upvotes: 0
Reputation: 27
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"
Upvotes: 1
Reputation: 501
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.
Upvotes: 2
Reputation: 144
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.
Upvotes: 9
Reputation: 111
Run this command: php artisan passport:install --force
and then try again.
Upvotes: 5
Reputation: 45
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
Upvotes: 0
Reputation: 136
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.
Upvotes: 0