Reputation: 279
I'm trying to develop my first webapp with Laravel so I can practice a bit but I've hit a roadblock right at the beginning. I didn't find anything on google regarding this problem, and I already tried to test this in a fresh install. I'm using Laravel 11 and passport auth. The user "artist" is created but I always get this 500 internal server error for some reason with the message "Attempt to read property "secret" on null". I can't really figure this out so I'm trying my luck here hoping someone has solved this issue.
Whole trace is here
AuthenticationController.php
public function register(Request $request)
{
$validatedData = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255',
'password' => 'required|string',
]);
// Hash the password before saving it to the database
$validatedData['password'] = bcrypt($request->password);
$artist = Artist::create($validatedData);
if (!$artist) {
return response()->json(['error' => 'Failed to create artist.'], 500);
}
$token = $artist->createToken('Token Name')->accessToken;
return response()->json(['token' => $token]);
}
api.php
Route::post('/register', [AuthenticationController::class, 'register'])->name('register');
ArtistController.php
public function store(Request $request)
{
$validatedData = $request->validate([
'name' => 'required|max:255',
'email' => 'required|email|unique:artists',
'password' => 'required',
'description' => 'nullable',
'genre' => 'nullable',
'url' => 'nullable',
'image' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048', // Add this line for image validation
'tags' => 'nullable',
]);
$imagePath = null;
if ($request->hasFile('image')) {
$image = $request->file('image');
if ($image->isValid()) {
$imageName = time() . '.' . $request->image->extension();
$imagePath = $request->image->storeAs('public/images', $imageName);
$imagePath = str_replace('public/', '', $imagePath);
} else {
return redirect()->back()->withErrors(['image' => 'The uploaded image is not valid.']);
}
}
$artist = new Artist();
$artist->name = $validatedData['name'];
$artist->description = $validatedData['description'] ?? '';
$artist->genre = $validatedData['genre'];
$artist->url = $validatedData['url'];
$artist->image = $imagePath;
$artist->tags = $validatedData['tags'];
$artist->save();
return redirect()->route('artists.index')
->with('success', 'Artist created successfully.');
}
PersonalAccessTokenFactory.php
protected function createRequest($client, $userId, array $scopes)
{
$secret = Passport::$hashesClientSecrets ? $this->clients->getPersonalAccessClientSecret() : $client->secret;
return (new ServerRequest('POST', 'not-important'))->withParsedBody([
'grant_type' => 'personal_access',
'client_id' => $client->getKey(),
'client_secret' => $secret,
'user_id' => $userId,
'scope' => implode(' ', $scopes),
]);
}
Response
"message": "Attempt to read property \"secret\" on null",
"exception": "ErrorException",
"file": "C:\\...\\PersonalAccessTokenFactory.php",
"line": 97,
Thanks in advance and please let me know if I can provide with some more code.
Upvotes: 0
Views: 304