dimanche 15 août 2021

Laravel Auth logout not working -- Before posted Answer for this question are not working in my case

I have developed small application for Auth learning purpose where everything works fine Login Logout and Dashboard page. But

Case1 once after clicking on logout, try to access dashboard page by editing url in browser it goes to dashboard page which is wrong.

Case2 when i cleare cache and browsing data and try to access dashboard it works fine and redirect for login which is correct.

I am using Auth::logout for logged out Following is my code

web.php

Route::get('/', function () {
    return view('welcome');
});
Route::get('/login','AuthenticationController@login')->name('login');

Route::post('/login.submit','AuthenticationController@getLogIn')->
name('login.submit');

Route::get('/register','AuthenticationController@register')->name('register');

Route::post('/register.submit','AuthenticationController@addUser')->
name('register.submit');

Route::get('/logout','AuthenticationController@logout')->name('logout');

Route::group(['middleware'=>'auth:user'],function(){

    Route::get('/dashboard','AuthenticationController@dashboard')->
    name('dashboard');

});

config/auth.php

'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],


   
    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],

        'user' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
    ],

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User1::class,
        ],

       
    ]

AuthenticationController.php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Validator;
use App\Models\User1;
use Illuminate\Support\Facades\Auth; 
use Session;

class AuthenticationController extends Controller
{
    //
    public function __construct()
    {
        
    }


    public function login()
    {
        return view('login');
    }
    public function register()
    {
        return view('register');
    }
    public function addUser(Request $request)
    {
        $input=$request->all();

        
        $Validator=Validator::make($input,$this->getRegisterRules($input));
        if($Validator->fails())
        {
            return redirect()->back()->withInput()->withErrors($Validator->messages());
        }

        $input['password']=bcrypt($input['password']);
        $user=User1::Create($input);
        if($user->exists)
        {
            return redirect()->route('login')->with('success','User created successfully! Kindly login');
        }
    }
    private function getRegisterRules($input) {
        $return = array();
        $return['fname'] = 'required|min:4|max:20';
        $return['lname'] = 'required|min:4|max:20';
        $return['email'] = 'required|email';
        $return['password'] = 'required|min:4|max:20|confirmed';
        return $return;
    }
    private function messages() {
        return [
            // 'question.required'  => 'The question field is required.'
        ];
    }
    public function dashboard()
    {
        return view('dashboard');
    }
     public function getLogIn(Request $request)
    {
        $input=$request->all();

        $Validator=Validator::make($input,$this->getLoginRules($input));
        if($Validator->fails())
        {
            return redirect()->back()->withInput()->withErrors($Validator->messages());
        }
        
        if(Auth::guard('user')->attempt(['email'=>$request->email,
            'password'=>$request->password],$request->remember))
        {
            return redirect()->route('dashboard');
        }

        return redirect()->back()->withInput()->withErrors('Invalid user name or password!');

    }
    public function getLoginRules($input)
    {
        $return=array();

        $return['email']='required|email';
        $return['password']='required|min:4|max:20';
        return $return;
    } 
    public function logout()
    {
        Auth::logout();
        Session::flush();
        return redirect()->route('login')->with('success','You have logged out!');
    }  
}

App/Models/User1.php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User1 extends Authenticatable
{
    use HasFactory;
    protected $fillable=[
        'fname',
        'lname',
        'email',
        'password'
    ];

    protected $hidden = [
        'password',
        'remember_token',
    ];
}

login.blade.php

<!DOCTYPE html>
<html>
<head>
  <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<title>Login</title>
</head>
<body>


<div class="wrapper fadeInDown">
  <div id="formContent">
    <!-- Tabs Titles -->
    @if(session()->has('success'))
    <div class="alert-success">
      
    </div>
    @endif
  <!-- Login Form -->
    
    @if($errors->any())
      <div class="alert-danger">
      
      </div>
    @endif

    <form method="POST" action="">
      
      <input type="text" id="login" class="fadeIn second" name="email" placeholder="Username">
      @if($errors->has('email'))
      <div class="alert-danger">
      
      </div>
      @endif
      
      @csrf
      <input type="password" id="password" class="fadeIn third" 
      name="password" placeholder="Password">
      @if($errors->has('password'))
      <div class="alert-danger">
      
      </div>
      @endif
      
      <input type="submit" class="fadeIn fourth" value="Log In">
      
      <input type="checkbox" id="remember" name="remember"  value="1" checked="">&nbsp;&nbsp;&nbsp;Remember me
    </form>

    <!-- Remind Passowrd -->
    <div id="formFooter">
      <a class="underlineHover" href="#">Forgot Password?</a>&nbsp;&nbsp;&nbsp;
      <a class="underlineHover" href="">SignUp</a>
    </div>

  </div>
</div>
</body>
</html>

dashboard.blade.php

<!DOCTYPE html>
<html>
<head>
  <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
 <title>Dashboard</title>
</head>
<body>


<div class="wrapper fadeInDown">
  
  <div id="formContent">
    Welcome To Dashboard
  </div>
  <form action="" method="GET"> 
    <a href="">Logout</a>
    @csrf
  </form>
</div>
</body>
</html>

When i try to dd(session->all()) in dashboard.blade.php following is output

array:5 [▼
  "_token" => "Uz8bUpJry6majKAb3yW7FuYqgjppzqroJqjcMPq4"
  "url" => array:1 [▶]
  "_previous" => array:1 [▶]
  "_flash" => array:2 [▶]
  "login_user_59ba36addc2b2f9401580f014c7f58ea4e30989d" => 7
]


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2XyXkRD
via IFTTT

Aucun commentaire:

Enregistrer un commentaire