P
P
photosho2016-01-08 20:44:51
Laravel
photosho, 2016-01-08 20:44:51

Why is authorization removed after reboot?

Strange problem. Authorize the user via AJAX:

\Auth::attempt([
  'username' => $login,
  'password' => $password
], $remember);

Next, I immediately check the authorized user and return the result of the check:
if (\Auth::user()) $result = 'Вошел';
else $result = 'Не вошел';

If everything is entered correctly, then, as required, the result is positive. If I enter the login or password incorrectly, the phrase "Not logged in" is returned to the browser.
Only here is the problem: for some reason, after reloading the page, the user is not authorized. This is very strange, maybe someone knows what could be the problem? User Model:
namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
  use Authenticatable, CanResetPassword;
  protected $table = 'users';
  protected $hidden = ['id', 'password', 'remember_token'];
  protected $fillable = ['username', 'email', 'password'];		
}

Controller function responsible for AJAX login execution:
if ($request->ajax()) {
  $params = $request->input('params');
} else exit();

$result = \Auth::attempt([
  'username' => $params['login'],
  'password' => $params['password']
], $params['remember']);
if (\Auth::user()) $result = 'Вошел';
else $result = 'Не вошел';

echo json_encode($result);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrzej Wielski, 2016-01-08
@wielski

Let's take a closer look.
And so, in some controller you have something like this code:

if(Auth::attempt([
  'username' => $request->login,
  'password' => $request->password
], $request->remember)){
  return ['message' => 'success'];
}
return ['message' => 'error'];

You pass a token, username, and password with post to this function.
Then you check, and if message is equal to success, then reload the page.
In this case, when you reboot, the user is still not authorized?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question