身份验证不起作用(vuejs / api php)

时间:2019-11-18 10:57:17

标签: php vue.js axios

我有一个简单的登录表单,该表单在本地运行良好,但是当我尝试在部署模式下测试auth函数时,这给我带来了一些麻烦(和令人头疼)。

我的后端是带有php的api,在邮递员上工作非常好。这是我的身份验证功能:

$login = $_POST['login'];
$password = $_POST['password'];

if (empty($login) && empty($password)) {
    response(400, "Authentification obligatoire! Paramètres non fournis.", NULL);
}

try {
    $requete = $connection->prepare("SELECT login, password FROM user WHERE login = ?");
    $requete->execute([$login]);
    $user = $requete->fetch();
} catch (PDOException $e) {
    response(500, $e->getMessage(), NULL);
}

if (empty($user) || $password != $user['password']) {
    response(401, "Le login ou le mot de passe ne correspond pas", NULL);
}
response(200, "authentifié", $login);

在我的客户端上,我使用vuejs和vuex来存储状态(再次,这非常简单)。在我的方法中,我有一个功能:

connect() {
      const logForm = {
        login: this.login,
        password: this.password
      };
      console.log("log form", logForm);
      _axios
        .post("authentication", logForm, {
            headers: { 
              "Content-Type": "multipart/form-data"}
          })
        .then(response => {
          console.log("response dans auth", response);
          window.localStorage.setItem("login", response.data.data);

          this.$store.commit("setAuthentication", true);
          this.$router.push("/career-list");
        })
        .catch(error => {
          console.log(error);
        });
    }

有人可以帮助我吗?谢谢!

1 个答案:

答案 0 :(得分:0)

我可以使用它:我添加了formData()而不是logForm对象,所以我想它已关闭?