Need a login lua script

Ask anything you want to know
Post Reply
User avatar
premkishor
Posts: 3
Joined: Sat Aug 30, 2025 7:26 am

Need a login lua script

Post by premkishor »

Hello @admin plz help i failed in create a login and register code in separate pages please help
User avatar
Lamismaxkria
Posts: 36
Joined: Wed Oct 02, 2024 12:49 pm
Location: Uruguay
Contact:

Re: Need a login lua script

Post by Lamismaxkria »

app:get("/login", function(ctx) local query = ctx.query or {} local errorMsg = query.error or "" local htmlContent = "<h2>Iniciar Sesión</h2>" if errorMsg ~= "" then local cleanError = tostring(errorMsg):gsub("%+", " ") htmlContent = htmlContent .. '<p style="color:#dc3545; background:#f8d7da; padding:10px; border-radius:6px; border:1px solid #f5c6cb; font-size:14px; text-align:center;">⚠️ ' .. cleanError .. '</p>' end htmlContent = htmlContent .. [[ <form action="/login" method="POST" style="margin-top:20px;"> <div style="margin-bottom:15px;"> <label style="display:block; margin-bottom:5px; font-weight:bold; font-size:14px;">Usuario o Email:</label> <input type="text" name="username" required style="width:100%; padding:10px; border:1px solid #ccc; border-radius:6px; box-sizing:border-box;"> </div> <div style="margin-bottom:20px;"> <label style="display:block; margin-bottom:5px; font-weight:bold; font-size:14px;">Contraseña:</label> <input type="password" name="password" required style="width:100%; padding:10px; border:1px solid #ccc; border-radius:6px; box-sizing:border-box;"> </div> <button type="submit" style="width:100%; background:#28a745; color:white; border:none; padding:12px; border-radius:8px; font-weight:bold; font-size:16px; cursor:pointer;">Entrar</button> </form> ]] local completo = [[<!DOCTYPE html><html lang="es"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Login - XKRIA-UY</title> <link rel="stylesheet" href="https://lamismaxkria.wapka.site/mi-css"> </head><body><div class="box">]] .. htmlContent .. [[<a href="/">🏠 Ir al Inicio</a></div></body></html>]] return ctx:html(completo)
end) app:get("/logout",function(ctx) if ctx.delete_cookie then ctx:delete_cookie("session_token") ctx:delete_cookie("w_user") ctx:delete_cookie("is_logged") ctx:delete_cookie("user_id") ctx:delete_cookie("username") ctx:delete_cookie("PHPSESSID") elseif ctx.set_cookie then ctx:set_cookie("session_token", "", { path = "/", max_age = 0 }) ctx:set_cookie("w_user", "", { path = "/", max_age = 0 }) ctx:set_cookie("is_logged", "", { path = "/", max_age = 0 }) ctx:set_cookie("user_id", "", { path = "/", max_age = 0 }) ctx:set_cookie("username", "", { path = "/", max_age = 0 }) end return ctx:redirect("/")
end)
app:post("/login",function(ctx) local raw_data = ctx.body or "" if (not raw_data or raw_data == "") and ctx.get_body then local okB, resB = pcall(function() return ctx:get_body() end) if okB and type(resB) == "string" then raw_data = resB end end local username = "" local password = "" if type(raw_data) == "string" and raw_data ~= "" then for pair in string.gmatch(raw_data, "[^&]+") do local key, value = string.match(pair, "([^=]+)=([^=]*)") if key and value then value = string.gsub(value, "+", " ") value = string.gsub(value, "%%(%x%x)", function(hex) return string.char(tonumber(hex, 16)) end) if key == "username" then username = value end if key == "password" then password = value end end end end username = string.gsub(username, "^%s*(.-)%s*$", "%1") password = string.gsub(password, "^%s*(.-)%s*$", "%1") if username == "" or password == "" then return ctx:redirect("/login?error=Por+favor,+completa+los+campos.") end local target_user = nil if api and api.users and api.users.list then local okL, response = pcall(function() return api.users.list({ limit = "100" }) end) if okL and response then local lista = response.data or response if type(lista) == "table" then for k, u in pairs(lista) do if type(u) == "table" then local uname = u.username or u.name or u.nick if uname and string.lower(uname) == string.lower(username) then target_user = u target_user.id = u.id or u.userId or k break end end end end end end if not target_user then return ctx:redirect("/login?error=El+usuario+no+esta+registrado.") end local sid = "sess_" .. tostring(target_user.id)
if hash and type(hash) == "table" then if hash.uuid then pcall(function() sid = hash.uuid() end) elseif hash.random then pcall(function() sid = hash.random(16) end) end end local session_data = { userId = tostring(target_user.id), username = tostring(target_user.username or username), login_time = os.time(), last_activity = os.time() } if api and api.auth and api.auth.session_create then pcall(function() api.auth.session_create(sid, session_data) end) end if ctx.set_cookie then ctx:set_cookie("session_token", sid, { path = "/", http_only = true }) ctx:set_cookie("w_user", username, { path = "/", http_only = true }) ctx:set_cookie("is_logged", "1", { path = "/", http_only = true }) ctx:set_cookie("user_id", tostring(target_user.id), { path = "/", http_only = true }) ctx:set_cookie("username", username, { path = "/", http_only = true }) ctx:set_cookie("PHPSESSID", sid, { path = "/" }) end return ctx:redirect("/")
end)
Post Reply