doutan3371 2016-04-21 09:08
浏览 51
已采纳

令牌认证休息API会话

I am using the Slim Framework to create a stateless REST API. Before using this I created a SESSION on server side with session check on every page. But now, I don't know how to control it.

I have an api_key in my database for each user. After a user signin, I respond with a api_key and redirect the user to index.php. But the api_key is not retained. How can I pass the api_key to each page with Javascript? Reason being if someone wants data from my REST API, they have to send me an api_key and also if the user logged in before I don't want to show login page again.

Here is my REST API part:

$app->post('/userlogin', function() use ($app) {
    verifyRequiredParams(array('email', 'password'));
    $email = $app->request->post('email');
    $password = $app->request->post('password');

    $objUserRegLog = new UserRegistrationLogin;
    $result = $objUserRegLog->getUserByEmailAndPassword($email, $password);
    if (!$result) {
        $response["error"] = true;
        $response["message"] = "Error! Invalid e-mail address or password.";
    } else {
        $response["error"] = false;
        $response["id"] = $result["id"];
        $response["email"] = $result["email"];
        $response["api_key"] = $result["api_key"];
    }
    echoResponse(200, $response);
});

$app->get('/students', 'authenticateStudent', function() use ($app) {
    $objStd = new Students;
    $result = $objCases->getAllStudents();
    if (!$result) {
        $response["error"] = true;
        $response["error_msg"] = "An error occured.";
        $status_code = 404;
    } else {
        $response["error"] = false;
        $response["cases"] = $result;
        $status_code = 200;
    }
    echoResponse($status_code, $response);
});

function authenticateStudent(\Slim\Route $route) {
    $headers = apache_request_headers();
    $response = array();
    $app = \Slim\Slim::getInstance();

    if (isset($headers['Authorization'])) {
        $db = new DbOperation();
        $api_key = $headers['Authorization'];
        if (!$db->isValidStudent($api_key)) {
            $response["error"] = true;
            $response["message"] = "Access Denied. Invalid Api key";
            echoResponse(401, $response);
            $app->stop();
        }
    } else {
        $response["error"] = true;
        $response["message"] = "Api key is misssing";
        echoResponse(400, $response);
        $app->stop();
    }
}

And the call with AJAX :

$.signin = function() {
    var inputVals = $("#form_signin").serialize();
    $.ajax({
        url : "api/v1/userlogin",
        data : inputVals,
        dataType : "json",
        type : "post",
        success : function(response) {
            if (response.error) {
                $(".popup").trigger("click");
                $(".modal-title").html(response.message_title);
                $(".modal-body").html(response.message);
            } else {
                window.location.href = "index.php";
            }
            console.log(response);
        }
    });
    return false;
}
  • 写回答

2条回答 默认 最新

  • douyin6188 2016-04-21 09:34
    关注

    Well, you need to understand that every request the client sends to your server is independent, so you need to put a variable (the token) in the client system in order to let him to send it in every request, so you know who is talking to your server all the time. Start reading this: http://www.w3schools.com/php/php_cookies.asp

    Once you understand what are and how cookies work, try to read further about authentication and authorization topics.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3