prudhvi259
2 years ago

Session is expiring automatically in laravel 5.2

Posted 2 years ago by prudhvi259

I am using larvel 5.2 for my application, i didn't used laravel authentication and wrote my own code, at the time of successful login i am storing user name in a session variable and checking this for every request after login. I didn't changed any session settings. My problem is session is expiring in lessthan 3mins and sometimes while login it is showing

TokenMismatchException in VerifyCsrfToken.php line 67:

remember only sometimes. After refresh the page everything become normal and after login within 3mins session is expiring. My code in routes is as follows,

Route::get('/', function () {
    if(Session::has('username') && Session::get('username') != ""){
        return view('index');
    }
    else{
        return redirect('login');
    }
});
Route::get('login', function () {
    if(Session::has('username') && Session::get('username') != ""){
        return redirect('/');
    }
    else{
        return view('login');
    }
});

My login and session storing as,

public function checkme(Request $request){
  
    $this->validate($request, [
        'username' => 'required',
        'password' => 'required'
    ]);

    //return $request->all();
    $username = $request->username;
    $password = md5($request->password);

    $admin = Admin::where('username', $username)
                ->where('password', $password)
                ->where('status', 'active')
                ->first();
                
    if(is_null($admin)){

        Session::flash('Invalid','Invalid Credentials..!');
        return redirect('login');
    }
    else{

        Session::put('username',$admin->username);
        return redirect('/');
    }

}

My login form code,

<form class="login" method="post" action="login">
                    <input type="hidden" name="_token" value="<% csrf_token() %>">
                    <input type="text" placeholder="Username" name="username" required="true" />
                    <input type="password" name="password" placeholder="Password" required="true" />
                    <input type="submit" value="Login" class="btn btn-info btn-sm" />
                </form>

Please sign in or create an account to participate in this conversation.