changeset 6:d59ae3e468f1

login work
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 23 Oct 2023 21:09:05 -0600
parents aa1920665f98
children f29dd482a25a
files src/lib/Shared.luan src/login1.red.luan src/login2.red.luan
diffstat 3 files changed, 41 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
diff -r aa1920665f98 -r d59ae3e468f1 src/lib/Shared.luan
--- a/src/lib/Shared.luan	Mon Oct 23 00:43:41 2023 -0600
+++ b/src/lib/Shared.luan	Mon Oct 23 21:09:05 2023 -0600
@@ -46,4 +46,9 @@
 <%
 end
 
+function Shared.discord_redirect_uri()
+	local request = Http.request
+	return request.scheme.."://"..request.headers["Host"].."/login2.red"
+end
+
 return Shared
diff -r aa1920665f98 -r d59ae3e468f1 src/login1.red.luan
--- a/src/login1.red.luan	Mon Oct 23 00:43:41 2023 -0600
+++ b/src/login1.red.luan	Mon Oct 23 21:09:05 2023 -0600
@@ -4,14 +4,15 @@
 local url_encode = Html.url_encode or error()
 local Http = require "luan:http/Http.luan"
 local Config = require "site:/lib/Config.luan"
+local Shared = require "site:/lib/Shared.luan"
+local discord_redirect_uri = Shared.discord_redirect_uri or error()
 local Logging = require "luan:logging/Logging.luan"
 local logger = Logging.logger "login1.red"
 
 
 return function()
 	local config = Config.get()
-	local request = Http.request
-	local redirect_uri = request.scheme.."://"..request.headers["Host"].."/login2.red"
+	local redirect_uri = discord_redirect_uri()
 	local scope = "identify"
 	local url = "https://discord.com/api/oauth2/authorize?client_id="..config.discord.client_id.."&redirect_uri="..url_encode(redirect_uri).."&response_type=code&scope="..url_encode(scope)
 	-- logger.info(url)
diff -r aa1920665f98 -r d59ae3e468f1 src/login2.red.luan
--- a/src/login2.red.luan	Mon Oct 23 00:43:41 2023 -0600
+++ b/src/login2.red.luan	Mon Oct 23 21:09:05 2023 -0600
@@ -1,10 +1,42 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
+local stringify = Luan.stringify or error()
+local Io = require "luan:Io.luan"
+local uri = Io.uri or error()
 local Http = require "luan:http/Http.luan"
+local Config = require "site:/lib/Config.luan"
+local Shared = require "site:/lib/Shared.luan"
+local discord_redirect_uri = Shared.discord_redirect_uri or error()
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "login2.red"
 
 
 return function()
 	local code = Http.request.parameters.code or error()
-	Http.response.set_persistent_cookie("user",code)
+	logger.info("code = "..code)
+	--Http.response.set_persistent_cookie("user",code)
+
+	local url = "https://discord.com/api/oauth2/token"
+	local config = Config.get()
+	local options = {
+		method = "POST"
+		headers = {
+			["Content-Type"] = "application/x-www-form-urlencoded"
+		}
+		authorization = {
+			username = config.discord.client_id
+			password = config.discord.client_secret
+			type = "basic"
+		}
+		parameters = {
+			grant_type = "authorization_code"
+			code = code
+			redirect_uri = discord_redirect_uri()
+		}
+	}
+	logger.info(stringify(options))
+	local result = uri(url,options).read_text()
+	logger.info(result)
+
 	Http.response.send_redirect "/"
 end