Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
fengfei committed Mar 29, 2018
1 parent 0719d63 commit 04e2916
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion html/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function api_invoke(uri, params, callback) {
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log(XMLHttpRequest);
if (XMLHttpRequest.status == "302") {
if (40100 == XMLHttpRequest.responseJSON.errno) {
location.href = "/login.html";
return;
}
Expand Down
31 changes: 21 additions & 10 deletions src/manage/access.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
local _M = {}
local cjson = require "cjson"

function _M.checkLogin()
local uri = ngx.var.uri
if uri ~= "/api/v1/login" and uri ~= "/login.html" then
local cache = ngx.shared.cache
local token = cache:get("login-token")
local ck = require "resty.cookie"
local cookie, err = ck:new()
if token == nil or token ~= cookie:get("token") then
return ngx.redirect("/login.html")
end
end
local uri = ngx.var.uri
if uri ~= "/api/v1/login" and uri ~= "/login.html" then
local cache = ngx.shared.cache
local token = cache:get("login-token")
local ck = require "resty.cookie"
local cookie, err = ck:new()
local isApi = string.find(uri,"/api/v1/");
if isApi and (token == nil or token ~= cookie:get("token")) then
local response = {}
response["status"] = 401
response["errno"] = 40100
response["msg"] = "Authentication required"
ngx.header.content_type = 'application/json;charset=UTF-8';
ngx.say(cjson.encode(response))
return;
end
if token == nil or token ~= cookie:get("token") then
return ngx.redirect("/login.html")
end
end
end

return _M

0 comments on commit 04e2916

Please sign in to comment.