Is there a particular reason for rolling your own authentication instead of using the one provided by Laravel?
Aug 13, 2024
5
Level 1
Consuming APIs for authentication in laravel auth
hello fellow artisans
as i am trying to authenticate use using my own made APIs using laravel i find it difficult. and i dont see where i did it wrong
public function store(LoginRequest $request): RedirectResponse { $credentials = $request->only('email', 'password'); $response = $this->authApiService->login($credentials);
if ($response->successful() && $response->json('status') === 'success') {
$apiResponse = $response->json('data');
$token = $apiResponse['token'];
$userData = $apiResponse['user'];
// Optionally store user data in session if needed
Session::put('api_token', $token);
Session::put('user', $userData);
$request->session()->regenerate();
// Manually log in the user without creating/updating in local database
Auth::loginUsingId($userData['id']);
// Check if the email is verified
if (is_null($userData['verified_at'])) {
// Redirect to the email verification notice page
return redirect()->route('verification.notice')->with('message', 'Please verify your email address.');
}
return redirect()->intended(route('dashboard', absolute: false));
}
return back()->withErrors([
'email' => 'The provided credentials do not match our records.',
]);
}
Please or to participate in this conversation.