繁体   English   中英

我想用 codeigniter 中的登录时间更新 jwt 令牌

[英]I want to update jwt token with login time in codeigniter

我想在登录时更新 jwt 令牌,并将 jwt 令牌设置到我的数据库用户表中。 我希望你一定会帮助我。 谢谢

这是我的用户 controller

public function canLogin()
    {
        $method = $_SERVER['REQUEST_METHOD'];
        if ($method == 'GET') {
            json_output(404, array('message' => 'Method must be a POST'));
        } else {
            $params = (array) json_decode(file_get_contents('php://input'), true);
            $email = $params['email'];
            $password = $params['password'];
            // $tokenData = $this->authorization_token->generateToken($email, $password);
            // $final = array();
            // $final['token'] = $tokenData;
            // json_output(200, array('code' => 200, 'data' => $final));
            if ($email && $password == true) {
                $data = $this->Users_Model->canLogin($email);
                if ($data != null) {
                    $check_password = password_verify($password, $data[0]['password']);
                    if ($check_password) {
                        json_output(200, array('code' => 200, 'message' => "Successfully Login", "data" => $data));
                    } else {
                        json_output(200, array('code' => 400, 'message' => "Invalid Password"));
                    }
                } else {
                    json_output(200, array('code' => 400, 'message' => "Email not Found"));
                }
            } else {
                json_output(200, array('code' => 400, 'message' => "Email or Password Shouldn't be null"));
            }
        }
    }

这是我的用户 model function:

public function canLogin($email)
    {
        $this->db->select('*');
        $this->db->where('email', $email);
        // $query  =   $this->db->get($this->users);
        $query = $this->db->get('users');
        // $row = $query->row();
        return $query->result_array();

我自己解决了我的问题,并在有人登录时从 API 得到响应,它会生成一个唯一令牌并将其更新到数据库中。

public function canLogin()
    {
        $method = $_SERVER['REQUEST_METHOD'];
        if ($method == 'GET') {
            json_output(404, array('message' => 'Method must be a POST'));
        } else {
            $params = (array) json_decode(file_get_contents('php://input'), true);
            $email = $params['email'];
            $password = $params['password'];
            if ($email && $password == true) {
                $data = $this->Users_Model->canLogin($email);
                if ($data != null) {
                    $check_password = password_verify($password, $data[0]['password']);
                    if ($check_password) {
                        $email_data = array('email' => $email, 'password' => $password);
                        $tokenData = $this->authorization_token->generateToken($email_data);
                        $jwt_token = $this->Users_Model->tokenUpdate($email, $tokenData);
                        if ($jwt_token) {
                            json_output(200, array('code' => 200, 'data' => $data, 'token' => $tokenData));
                        } else {
                            json_output(200, array('code' => 200, 'message' => 'Something Went Wrong'));
                        }
                    } else {
                        json_output(200, array('code' => 400, 'message' => "Invalid Password"));
                    }
                } else {
                    json_output(200, array('code' => 400, 'message' => "Email not Found"));
                }
            } else {
                json_output(200, array('code' => 400, 'message' => "Email or Password Shouldn't be null"));
            }
        }
    }

我的 Model:

public function tokenUpdate($email, $token)
    {
        $this->db->where('email', $email);
        $this->db->update('users',array('token' => $token));
        return true;
    }

最后,得到令牌作为回应......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM