Mercurial Hosting > freedit
changeset 6:9166f6a14021
add email api
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 23 Jun 2022 23:05:28 -0600 |
parents | 2f20b11affdd |
children | 0472897e790d |
files | src/api/Api.luan src/api/forms.html src/api/login_email.json.luan src/lib/Forum.luan src/lib/Shared.luan src/login.html.luan |
diffstat | 6 files changed, 115 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/api/Api.luan Thu Jun 23 23:05:28 2022 -0600 @@ -0,0 +1,37 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local new_error = Luan.new_error or error() +local Parsers = require "luan:Parsers.luan" +local json_string = Parsers.json_string or error() +local Io = require "luan:Io.luan" +local Http = require "luan:http/Http.luan" + + +local Api = {} + +function Api.api(fn) + return function() + Io.stdout = Http.response.text_writer() + try + local rtn = fn() + %><%=json_string(rtn)%><% + catch e + if e.error ~= nil then + %><%=json_string(e.error)%><% + return + end + e.throw() + end + end +end + +function Api.user_error(msg) + local e = new_error(msg) + e.error = { + okay = false + error = msg + } + e.throw() +end + +return Api
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/api/forms.html Thu Jun 23 23:05:28 2022 -0600 @@ -0,0 +1,19 @@ +<!doctype html> +<html> + <head> + <meta name="viewport" content="width=device-width, initial-scale=1"> + </head> + <body> + <h1>API Forms</h1> + <hr> + <form action="login_email.json"> + <h3>login_email</h3> + <p>base_url: <input name=base_url type=url required></p> + <p>from: <input name=from required></p> + <p>email: <input name=email type=email required></p> + <p>password: <input name=password required></p> + <p><input type=submit></p> + </form> + <hr> + </body> +</html>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/api/login_email.json.luan Thu Jun 23 23:05:28 2022 -0600 @@ -0,0 +1,33 @@ +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 output_of = Io.output_of or error() +local Http = require "luan:http/Http.luan" +local Mail = require "site:/lib/Mail.luan" +local Api = require "site:/api/Api.luan" +local api = Api.api or error() +local user_error = Api.user_error or error() + + +return api(function() + local base_url = Http.request.parameters.base_url or user_error "missing base_url param" + local from = Http.request.parameters.from or user_error "missing from param" + local email = Http.request.parameters.email or user_error "missing email param" + local password = Http.request.parameters.password or user_error "missing password param" + local mailer = Mail.sender() or user_error "mail not configured" + mailer.send{ + From = from + To = email + Subject = "Login" + body = output_of(function() %> +Login or register by clicking this link: + +<%=base_url%>/login.html?email=<%=url_encode(email)%>&password=<%=password%> +<% end) + } + return { + okay = true + } +end)
--- a/src/lib/Forum.luan Tue Jun 21 21:39:27 2022 -0600 +++ b/src/lib/Forum.luan Thu Jun 23 23:05:28 2022 -0600 @@ -6,4 +6,6 @@ Forum.title = "forum title not set" +Forum.mailer_url = "http://localhost:8080" + return Forum
--- a/src/lib/Shared.luan Tue Jun 21 21:39:27 2022 -0600 +++ b/src/lib/Shared.luan Thu Jun 23 23:05:28 2022 -0600 @@ -1,7 +1,13 @@ local Luan = require "luan:Luan.luan" local error = Luan.error local Http = require "luan:http/Http.luan" +local Io = require "luan:Io.luan" +local uri = Io.uri or error() +local Parsers = require "luan:Parsers.luan" +local json_parse = Parsers.json_parse or error() local Forum = require "site:/lib/Forum.luan" +local forum_title = Forum.title or error() +local mailer_url = Forum.mailer_url or error() local User = require "site:/lib/User.luan" local Logging = require "luan:logging/Logging.luan" local logger = Logging.logger "Shared" @@ -22,7 +28,7 @@ local user = User.current() %> <div header> - <a href="/"><%=Forum.title%></a> + <a href="/"><%=forum_title%></a> <% if user == nil then %> <a href="/login.html">login</a> <% else %> @@ -52,4 +58,13 @@ return Http.request.scheme.."://"..Http.request.headers["host"] end +function Shared.call_mail_api(cmd,parameters) + local url = mailer_url.."/api/"..cmd..".json" + local options = { + parameters = parameters + } + local response = uri(url,options).read_text() + return json_parse(response) +end + return Shared
--- a/src/login.html.luan Tue Jun 21 21:39:27 2022 -0600 +++ b/src/login.html.luan Thu Jun 23 23:05:28 2022 -0600 @@ -12,10 +12,10 @@ local header = Shared.header or error() local footer = Shared.footer or error() local base_url = Shared.base_url or error() +local call_mail_api = Shared.call_mail_api or error() local Forum = require "site:/lib/Forum.luan" local forum_title = Forum.title or error() local User = require "site:/lib/User.luan" -local Mail = require "site:/lib/Mail.luan" local Db = require "site:/lib/Db.luan" local run_in_transaction = Db.run_in_transaction or error() @@ -87,17 +87,13 @@ end) elseif password == nil then local user = User.get_or_create_by_email(email) - local mailer = Mail.sender() or error "mail not configured" - mailer.send{ - From = forum_title.." <support@freedit.org>" - To = user.email - Subject = "Login" - body = output_of(function() %> -Login or register by clicking this link: - -<%=base_url()%>/login.html?email=<%=url_encode(email)%>&password=<%=user.password%> -<% end) - } + local result = call_mail_api( "login_email", { + base_url = base_url() + from = forum_title.." <support@freedit.org>" + email = user.email + password = user.password + } ) + result.okay or error(result.error) page(function() %> <p>We have sent you an email. Please check your email to login or register.</p>