lundi 27 mars 2023

clicking on login button and return only to the index page in laravel

i have a problem and it isnt related with validation , im making my own login and register page and im not using start kits like breeze or jet stream ... when i click on the login button it must go and check the database if there is the same password and email then do something . but other to do anything it only return me to the same page like only i refresh the page , even if the email and password correct or not , it only refresh the page. why ?? This is the login form

<form class="login-form" action="" method="post">
    @csrf
    <img
        class="logo"
        src="https://www.logomaker.ckhfZt.png"
        alt="logo"
    />
    <label for="email">Email address:</label>
    <input
        type="text"
        id="email"
        name="email"
        placeholder="Enter email address"
        required
    />

    <label for="password">Password:</label>
    <input
        type="password"
        id="password"
        name="password"
        placeholder="Enter password"
        required
    />
    @error('failed')
    <div class="alert alert-danger" role="alert">
        
    </div>
    @enderror

    <input type="submit" value="Login" />
</form>

and this is the web file

Route::controller(AuthController::class)->group(function () {


    /* Register Route */

    Route::get('/register','register');
    Route::post('/store','store')->name('store');


    /* Login Route */

    Route::get('/','login');
    Route::post('/','loginCheck')->name('login-check');


    /* Home Route */
    Route::get('/home','home');

});

and this is the controller

class AuthController extends Controller
{
    public function login(){
        return view('auth.login');
    }
    public function register(){
        return view('auth.register');
    }

    public function home(){
        return view('home');
    }


    public function store(StoreRegisterRequest $request){

    $existingUser = DB::table('users')->where('email',$request->email)->first();
    if(!$existingUser){
        DB::table('users')->insert([
            'name' => $request->name,
            'email' => $request->email,
            'password'=> Hash::make($request->password),
        ]);
        return redirect('/');
         }
    }


    public function loginCheck(StoreRegisterRequest $request)
    {
        $user = DB::table('users')->where('email', $request->email)->first();
    
        if ($user && Hash::check($request->password, $user->password)) {
            // Credentials are valid, so start the session
          return $user;
        }
    
        // Invalid credentials, so return an error message
        return back()->withErrors(['failed' => 'Invalid login credentials.']);
    }
    
    

}

what is the problem why i cant see the user variable or i cant return to any other path?

im expecting my problem to be solved



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire