Answer the question
In order to leave comments, you need to log in
How to auto login a user with BasicAuth enabled?
I need that after the user is correctly authorized, he remains logged in not only until the browser is closed, but also after. In other words, so that after the browser is closed (but after a small amount of time has passed), when you log in again, the alert does not pop up with inputs for the login and password.
Authentication code in behaviors:
'authenticator' => [
'class' => \yii\filters\auth\HttpBasicAuth::class,
'auth' => function ($username, $password) {
$user = User::find()->where(['username' => $username])->one();
if ($user && Yii::$app->getSecurity()->validatePassword($password, $user->password_hash)) {
if ($user->verification_token) {
return $user;
} else {
$token = Yii::$app->getSecurity()->generateRandomString();
$user->verification_token = $token;
$user->save();
$this->serializer['token'] = $token;
return $user;
}
} else {
return null;
}
},
],
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'enableSession' => false,
'identityCookie' => ['name' => '_identity-backend', 'httpOnly' => true],
],
let username = 'admin';
let password = '!QAZ2wsx';
let headers = new Headers();
headers.set('Authorization', 'Basic ' + btoa(username + ":" + password));
fetch('http://back.ru/menu?tree=1', {
credentials: "include",
headers: {
headers
},
})
.then(response => response.json()).then(data => { context.commit('SET_LIST', data) });
Answer the question
In order to leave comments, you need to log in
As a result, I found out that the server does not see the headers if they were sent through the new Headers() object
, and when I added the headers manually, like this:
headers: {
'Authorization': 'Basic ' + btoa(username + ":" + password);
},
then the server has already begun to see the sent data.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question