view src/login.html.luan @ 56:7ce54f6d93f2

add change name
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 28 Nov 2022 22:00:43 -0700
parents c57b84f461ae
children
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local Html = require "luan:Html.luan"
local url_encode = Html.url_encode or error()
local Io = require "luan:Io.luan"
local Http = require "luan:http/Http.luan"
local Shared = require "site:/lib/Shared.luan"
local head = Shared.head or error()
local header = Shared.header or error()
local footer = Shared.footer or error()
local Forum = require "site:/lib/Forum.luan"
local forum_title = Forum.title or error()
local User = require "site:/lib/User.luan"


local function page(contents)
	Io.stdout = Http.response.text_writer()
%>
<!doctype html>
<html>
	<head>
<%		head() %>
		<title><%=forum_title%> - Login</title>
		<script>
			function show(checkbox) {
				document.querySelector('input[name="password"]').type = checkbox.checked ? 'text' : 'password';
			}
		</script>
	</head>
	<body>
<%		header() %>
		<div content>
			<h1>Login</h1>
<%
			contents()
%>
		</div>
<%		footer() %>
	</body>
</html>
<%
end

return function()
	local email = Http.request.parameters.email
	local password = Http.request.parameters.password
	local error_message = nil
	if Http.request.method == "POST" then
		local user = User.get_by_email(email)
		if user == nil then
			error_message = "email not found"
		elseif user.password ~= password then
			error_message = "wrong password"
		end
		if error_message == nil then
			if user.name == nil then
				Http.response.send_redirect("/set_name.html?email="..url_encode(email).."&password="..password)
				return
			end
			user.login()
			page(function()
%>
				<p>You are now logged in.</p>
<%
			end)
			return
		end
	end
	page(function()
		if error_message ~= nil then %>
			<p error>Error: <%= error_message %></p>
<%		end %>
			<form action="login.html" method=post>
				<p>
					<label>Email address</label>
					<input type="email" name="email" value="<%= email or "" %>" autofocus required>
				</p>
				<p>
					<label>Password</label>
					<input type="password" name="password" value="<%= password or "" %>" required>
					<label clickable><input type=checkbox onclick="show(this)">Show</label>
				</p>
				<p><input type="submit" value="Login"></p>
			</form>
			<p><a href="/register.html">Get or change password</a></p>
<%
	end)
end