AuthPlatform/src/service/auth/auth.lua

78 lines
2.2 KiB
Lua
Raw Normal View History

---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by admin.
--- DateTime: 2025/10/28 11:09
---
local resp = require("util.response")
local authDao = require("dao.auth")
local jwt = require("resty.jwt")
local conf = require("config")
local validatorJson = require("validator.system.auth")
local _M = {}
--设置JWT的有效载荷
local obj = {
header = {typ="JWT", alg="HS256"},
payload = { -- 自定义数据
username = "",
role = "",
--iss = "your_issuer", -- 签发者
--sub = "1234567890", -- 主题
exp = os.time() + 3600, -- 过期时间(例如:当前时间+1小时
iat = os.time() -- 签发时间
}
}
--用户登录业务逻辑处理
function _M.login()
--读取请求体的数据
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合json
local ok = validatorJson.validatorJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
resp:send(result)
return
end
--ngx.say(body_data)
local code, ret = authDao.login(body_data)
--读取数据错误
if code ~= 0 or table.getn(ret) < 0 then
local result = resp:json(0x000001)
resp:send(result)
return
end
--获取的登陆的用户信息返回tocken
obj.payload.username = body_data["name"]
obj.payload.role = ""
local jwt_token = jwt:sign(conf.secret_key, obj)
ngx.say(jwt_token)
local result = resp:json(code, ret)
resp:send(result)
end
--用户登出业务逻辑处理
function _M.logout()
--读取请求体的数据
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合json
local ok = validatorJson.validatorJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
resp:send(result)
return
end
--ngx.say(body_data)
local code, ret = authDao.logout(body_data)
local result = resp:json(code, ret)
resp:send(result)
end
return _M