summaryrefslogtreecommitdiff
path: root/forms.lua
blob: d4084a44d3901396c431007e618ab0e4048f8c31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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, messages)
	show_messages(messages)
	html.form({method = "POST"}, function()
		input('text', 'username',
			"username: ", user:get "username", "change")
	end)
	html.form({method = "POST"}, function()
		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: ")
		input('submit', nil, nil, "change")
	end)
	html.h3 "log out"
	html.form({method = "POST"}, function()
		hidden('logout', 'yes')
		input('checkbox', 'everywhere',
			"log out everywhere", nil, "log out")
	end)
end

return M