Andreea@Webclix's avatar

User logging out automaticly

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

0 likes
29 replies
Andreea@Webclix's avatar

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's avatar

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

1 like
Mittensoff's avatar

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

1 like
Andreea@Webclix's avatar

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's avatar

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.

Andreea@Webclix's avatar

JS isn't refreshing the page. Controllers all look normal, never have a logout function or redirect to logout

Mittensoff's avatar

Probably an issue with session then.

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

Andreea@Webclix's avatar

I have multiple auth. Customers use laravel's Auth, for admin I made my own.

Talky's avatar

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

Mittensoff's avatar

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's avatar

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

Andreea@Webclix's avatar

@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's avatar

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

Andreea@Webclix's avatar

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;
}

}

Talky's avatar

This still does not show us anything about authentication. What's in your admin.auth.login file?

Andreea@Webclix's avatar
    </div>
      <p class="login-box-msg" style="font-weight: 800; color:black;">Administrator login.<br />Gewone gebruiker? Klik <a href="/{{LaravelLocalization::getCurrentLocale()}}/{{ trans('routes.login') }}" title="gebruiker login">hier</a>!</p>
      <form role="form" method="POST" action="{{ url('/webclix-admin/login') }}">
            {{ csrf_field() }}
        <div class="form-group has-feedback{{ $errors->has('email') ? ' has-error' : '' }}">
          <input type="email" name="email" class="form-control" placeholder="Email" />
          @if ($errors->has('email'))
                      <span class="help-block">
                          <strong>{{ $errors->first('email') }}</strong>
                      </span>
                  @endif
          <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
        </div>
        <div class="form-group has-feedback{{ $errors->has('password') ? ' has-error' : '' }}">
          <input type="password" name="password" class="form-control" placeholder="Wachtwoord" />
          @if ($errors->has('password'))
                      <span class="help-block">
                          <strong>{{ $errors->first('password') }}</strong>
                      </span>
                  @endif
          <span class="glyphicon glyphicon-lock form-control-feedback"></span>
        </div>
        <div class="row">

          <div class="col-xs-12">
            <button type="submit" class="btn btn-primary btn-block btn-flat">Inloggen </button>
          </div>
          <div class="col-xs-12" style="padding-top:15px;">
            {{-- <small><a href="{{ url('password/reset') }}" class="form-link">Wachtwoord vergeten?</a></small> --}}
            <small>Wachtwoord vergeten? Neem contact op met uw beherder - <a href="mailto:[email protected]?subject=Webclix Admin - Ik ben mijn wachtwoord vergeten.">[email protected]</a></small>
          </div>
        </div>
      </form>
    </div>
  </div>
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.min.js"></script>
Talky's avatar

Everything seems to be perfectly fine for now. What about '/webclix-admin/login' POST route? find us that method and show it, please

Andreea@Webclix's avatar

Route::post('webclix-admin/login', 'AdminAuthController@login')->name('login');

Talky's avatar

but show us the method responsible for this route, you did not attach it on the previous page :(

PS: Yay, just spammed my way through the first thousand points!

Andreea@Webclix's avatar

I didn't make that controller myself actually, the AdminAuthController doesn't seem to have a login methode.

Just a guess but I think the 'use AuthenticatesAndRegistersUsers' and 'use ThrottlesLogins' take care of the loggin in itself.

Talky's avatar

look for this method then. It's also worth checking middleware. I'm leaving work now but I'll be tomorrow and will check on this thread. Hope you'll solve it till then!

Andreea@Webclix's avatar

The AuthenticatesAndRegistersUsers points to AuthenticesUsers. This has a login method.

public function login(Request $request) { $this->validateLogin($request);

    // If the class is using the ThrottlesLogins trait, we can automatically throttle
    // the login attempts for this application. We'll key this by the username and
    // the IP address of the client making these requests into this application.
    $throttles = $this->isUsingThrottlesLoginsTrait();

    if ($throttles && $lockedOut = $this->hasTooManyLoginAttempts($request)) {
        $this->fireLockoutEvent($request);

        return $this->sendLockoutResponse($request);
    }

    $credentials = $this->getCredentials($request);

    if (Auth::guard($this->getGuard())->attempt($credentials, $request->has('remember'))) {
        return $this->handleUserWasAuthenticated($request, $throttles);
    }

    // If the login attempt was unsuccessful we will increment the number of attempts
    // to login and redirect the user back to the login form. Of course, when this
    // user surpasses their maximum number of attempts they will get locked out.
    if ($throttles && ! $lockedOut) {
        $this->incrementLoginAttempts($request);
    }

    return $this->sendFailedLoginResponse($request);
}
Andreea@Webclix's avatar

My problem is still not solved, does anyone have another solution?

joyal's avatar
joyal
Best Answer
Level 1

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"

vijayrana's avatar

Late in the discussion but I fixed my same issue with creating storage symlink. I ran the following command it stopped logging me out frequently.

php artisan storage:link

1 like

Please or to participate in this conversation.