Answer the question
In order to leave comments, you need to log in
Yii2 authorization not working why?
Hello, I can’t understand why the authorization fails ....
So I enter data into the form ... I send the data
var loginis = (function () {
var psw = $("#password2").val();
var email = $("#mail2").val();
$.ajax({
type: 'POST',
url: "site/loginis",
data: {'login':email, 'password':psw,},
dataType: "json",
success: function(data){
if (data.success == true){
window.location.href = data.redirect_uri;
} else alert('Ошибка');
},
error: function(e) {
console.log(e);
}
});
});
public function actionLoginis(){
if (Yii::$app->request->isAjax) {
//ищем пользователя с таким psw and login
$login = Yii::$app->request->post('login');
$psw = Yii::$app->request->post('password');
if($data = Users::findUserByAouth(array('login'=>$login, 'password'=>$psw, ))){
//вот тут как раз и проблема не знаю - как авторизоваться
$user = Users::findByEAuth($data);
return json_encode(array("success" => true, "redirect_uri" => 'index'));
} else {
echo json_encode(array('success'=>false));
}
}
}
//findByEAuth() - тут пытаюсь авторизоваться
public static function findByEAuth($data) {
if (empty($data)) {
throw new ErrorException('');
}
$id = 'servis-'.$data['id'];
$attributes = array(
'id' => $data['id'],
'username' => $data['name'],
'authKey' => md5($data['id']),
);
$attributes['profile'] = $data;
$attributes['profile']['service'] = 'servis';
Yii::$app->getSession()->set('user-'.$id, $attributes);
return new self($attributes);
}
Answer the question
In order to leave comments, you need to log in
You would at least write - how exactly it does not work, what is happening?
In general, I see at least two problems:
1. You send a POST request with Ajax without a CSRF token. Have you disabled the CSRF token check in the controller?
2. You are making a dubious bike, although Yii already has normal means for authentication (yes, what you are doing is not called authorization, but authentication).
Use the standard tools: https://github.com/yiisoft/yii2/blob/master/docs/g...
Don't write bikes.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question