User logging out automaticly

Published 1 week ago by [email protected]

I've made a few websites in Laravel but with one of them I get logged out automaticly for no reason. It just happens randomly. I checked the log files but there is nothing logged.

Does anyone have any ideas about what could be wrong?

I'm using laravel 5.2

Best Answer (As Selected By [email protected])
laradaily

changing you session_driver from file to database may fix this issue.

Step 1: Edit you .env file, change SESSION_DRIVER=file to SESSION_DRIVER=database.

Step 2: create a session migration run "php artisan session:table"

Step 3: run "composer dump-autoload"

Step 4: run "php artisan migrate"

Andreea@Webclix

I'll try that. What I think is strange is that it sometimes happens after seconds of being logged in, sometimes after more than 15 minutes, even when I am constantly using the site. It happens while updating, deleting or even browsing from one page to another. It happend that I log in, change page and I am logged out again.

shakti
shakti
1 week ago (15,530 XP)

I still thinks its a session timeout problem

You can set long-time session. To do this you have to edit config/session.php and set lifetime to value you need in minutes. For example:

'lifetime' => 525600, // one year    1=1minute

After edit you have to clear config cache with command:

php artisan config:cache

Mittensoff

Perhaps you're not logging in in the first place, try checking about what happens after a successful login. Do some dd()-ing.

Andreea@Webclix

I do log in. After logging in I get redirected to the admin panel with is only accessable when logged in. Usually I can move around in the admin-panel and do some changes.

Mittensoff

I suspect a redirect happening somewhere, either in controllers or in javascript. Check your controllers and the JS files that the admin page is referencing.

Mittensoff

Probably an issue with session then.

Are you using Laravel's Auth or you made your own?

Talky
Talky
1 week ago (1,110 XP)

and do you get logged out as a Customer (L5 Auth) or as an admin (your own auth)?

Mittensoff

Check session.php and the lifetime parameter. Also check if it happens in other browsers if you've put logout on browser close.

I'm just spouting out ideas here, I can't be sure without the code.

Snapey
Snapey
1 week ago (523,835 XP)

Are you specifying SESSION_DOMAIN in .env or changed the same in config/session.php (session problems may be associated with this)

Andreea@Webclix

@Talky I get logged out as admin

@Mittensoff lifetime parameter was 120, changed it to 1200 but didn't have time to check if that changed anything. I'll do some testing right now.

What part of the code you like to see?

Mittensoff

Basically how you do the auth part for the admin and where you retrieve/put data in Session.

Andreea@Webclix

It's not browser specific. Just tested on other browser, same problem. this is my Admin Controller

class AdminAuthController extends Controller {

use AuthenticatesAndRegistersUsers, ThrottlesLogins;

protected $redirectTo = "/webclix-admin";

public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);

}

public function showLogin(){
  return view('admin.auth.login');
}

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|confirmed|min:6',
    ]);
}

protected function create(array $data)
{
    $create = User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);

    $user = User::find($create->id);

     $role = Role::where('name', '=', 'client')->firstOrFail();

     $user->roles()->attach($role->id);

     return $create;
}

}

Sign In or create a forum account to participate in this discussion.