-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
fengfei
committed
Mar 29, 2018
1 parent
0719d63
commit 04e2916
Showing
2 changed files
with
22 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |