local citrine = require 'citrine'
local html = require 'html'
local M = {}
local function show_messages(messages)
if messages then
for _, m in ipairs(messages) do
citrine.errmsg(m)
end
end
end
function M.reasonable_string(str)
local len = utf8.len(str)
return len and len < 256 and len > 0 or false
end
local function input(type, id, label, value, submit)
html.p({class = 'field'}, function()
if label and type ~= 'checkbox' then
html.label({["for"] = id}, label)
end
html.input {
type = type, id = id, name = id,
required = type ~= 'checkbox' and "" or nil, maxlength = 256,
value = value
}
if label and type == 'checkbox' then
html.label({["for"] = id, class = 'checkbox-label'}, label)
html.text " "
end
if submit then
html.input {type = 'submit', value = submit}
end
end)
end
local function hidden(name, value)
html.input {type = 'hidden', name = name, value = value}
end
function M.login(action, messages, values)
local attrs = {method = "POST", action = action, class = 'login-form'}
local values = values or {}
html.form(attrs, function()
html.h2 "existing account"
show_messages(messages)
input('email', 'email', "email: ", values.email)
input('password', 'password', "password: ")
hidden('action', 'login')
input('submit', nil, nil, "log in")
end)
end
function M.register(action, messages, values)
local attrs = {method = "POST", action = action, class = 'register-form'}
local values = values or {}
html.form(attrs, function()
html.h2 "register"
show_messages(messages)
input('email', 'email', "email: ", values.email)
input('password', 'password', "password: ", values.password)
input('password', 'confirm_password', "confirm password: ")
input('text', 'username',
"username (may be changed later): ", values.username)
input('time', 'coinstar', "when's 8am? ")
html.p [[
tip: you should use a (offline) password manager to generate and
store your passwords.
]]
hidden('action', 'register')
input('submit', nil, nil, "register")
end)
end
function M.connect(uid, token, meta, endpoint)
local attrs = {method = "POST", action = endpoint, class = 'connect-form'}
html.form(attrs, function()
hidden('uid', uid)
hidden('token', token)
if meta then
hidden('meta', meta)
end
input('submit', nil, nil, "confirm")
end)
end
function M.user_settings(user, token, messages)
show_messages(messages)
html.form({method = "POST"}, function()
hidden('token', token)
input('text', 'username',
"username: ", user:get "username", "change")
end)
html.form({method = "POST"}, function()
hidden('token', token)
input('email', 'email',
"email: ", user:get "email", "change")
end)
html.p "username changes may take time to effect."
html.h3 "change password"
html.form({method = "POST"}, function()
input('password', 'password', "current password: ")
input('password', 'new_password', "new password: ")
input('password', 'confirm_password', "confirm password: ")
hidden('token', token)
input('submit', nil, nil, "change")
end)
html.h3 "log out"
html.form({method = "POST"}, function()
hidden('logout', 'yes')
hidden('token', token)
input('checkbox', 'everywhere',
"log out everywhere", nil, "log out")
end)
end
return M