John Davidson

php - authentification doesn't work in Laravel 8 with the @auth

0 comments
Message:


So I have a Laravel application, I login and get redirected to my home page where the header is suppose to be changed but it isn't.


        <ul>
<a href="{{URL:: route( 'login' )}}" id="logo">
<img src="{{asset('assets/img/rentalstallLogo.jpg')}}" alt="Logo">
</a>
<li><a >Qui somme-nous</a> </li>
<li><a >Événements & concours </a></li>
@auth
<li>
<form action="{{ route('signout')}}" method="GET">
@csrf
<button type="submit" id='logoutButton'> logout </button>
</form>
</li>
<li><a id="connexion-link" href={{route( 'login' )}}>account</a> </li>
@endauth
@guest
<li><a href={{route( 'register-user' )}} >Inscriptions</a> </li>
<li><a id="connexion-link" href={{route( 'login' )}}>Connexion</a> </li>
@endguest
<li><a id="language-link" href="#">fr</a> </li>
</ul>

i also tried if (Auth::check()) and if (Auth->check())


here is my log in function :


            'email' => 'required',
'password' => 'required'
]);

$credentials = $request->except(['_token']);

$user = User::where('email',$request->email)->first();

if (auth()->attempt($credentials)) {

return redirect()->intended('/');


}else{
session()->flash('message', 'Invalid credentials');
return redirect()->back();
}


My Answer:
It looks like you are using the correct syntax for checking if a user is authenticated in your blade template with `@auth` and `@endauth`.

However, the issue might be with how you are handling the authentication in your login function.

Instead of using `auth()->attempt($credentials)`, you can try using `Auth::attempt($credentials)` like this:

php
if (Auth::attempt($credentials)) {
return redirect()->intended('/');
} else {
session()->flash('message', 'Invalid credentials');
return redirect()->back();
}


Make sure you have imported the Auth facade at the top of your controller:

php
use Illuminate\Support\Facades\Auth;


Also, ensure that your User model is using the `Authenticatable` trait:

php
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
// Your User model code here
}


By making these changes, your authentication should work correctly in Laravel 8.

Rate this post

4 of 5 based on 1715 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us