My registration page works well. As I registered a new account, it then redirects me to the "home" page. However, when I tried to login, it redirects me to a blank white page. I tried to check the route, the action="login" on the form, at the controller, it all corrects. I tried different way of writing the code such as Auth::Attempt as well as auth()->attempt(). I compared it with the register page that I've created. It should be fine. I really have no idea where I did wrong. Please help. I need to complete a project by next week tuesday. I'm in a very tight schedule right now.
Route code
Route::get('/', function () {
return view('home');
});
Route::get('/record', function () {
return view('record');
});
Route::get('/profile', function () {
return view('profile');
});
Route::get('register', [RegisterController::class, 'create'])->middleware('guest');
Route::post('register', [RegisterController::class, 'store'])->middleware('guest');
Route::get('login', [SessionsController::class, 'create'])->middleware('guest');
Route::post('session', [SessionsController::class, 'store'])->middleware('guest');
Route::post('logout', [SessionsController::class, 'destroy'])->middleware('auth');
Controller page
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Illuminate\Http\Request;
class SessionsController extends Controller
{
public function create()
{
return view('sessions.create');
}
public function store()
{
$attributes = request()->validate([
'email' => 'required|email',
'password' => 'required'
]);
if (Auth::Attempt($attributes)) {
return redirect('/')->with('success', 'Your account has been created.');
}
}
public function destroy()
{
auth()->logout();
redirect('/')->with('success', 'Goodbye');
}
}
Home page
<x-layout>
<div id="sidebar" >
<div >
<form method="GET" action="/">
<input type="text" name="search" placeholder="Search Staff" value="{{ request('search') }}">
</form>
</div>
<div >
<x-category-dropdown />
</div>
</div>
</x-layout>
CodePudding user response:
The likely cause of this is that your Auth::attempt()
is failing. You have no fallback/default response if it fails:
// redirect ONLY IF authentication succeeds
if (Auth::Attempt($attributes)) {
return redirect('/')->with('success', 'Your account has been created.');
}
// we get here if authentication fails, but do nothing
Add in a fallback redirect for instances where authentication fails:
if (Auth::Attempt($attributes)) {
return redirect('/')->with('success', 'Your account has been created.');
}
return redirect('/')->with('errors', 'Authentication failed.');