changeset 5:aa1920665f98

discord login
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 23 Oct 2023 00:43:41 -0600
parents 8896ffd7b152
children d59ae3e468f1
files src/lib/Shared.luan src/login.red.luan src/login1.red.luan src/login2.red.luan
diffstat 4 files changed, 30 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/Shared.luan	Sun Oct 22 23:53:59 2023 -0600
+++ b/src/lib/Shared.luan	Mon Oct 23 00:43:41 2023 -0600
@@ -28,7 +28,7 @@
 		<div header>
 			<h1><a href="/">Disearch</a></h1>
 <%	if get_user() == nil then %>
-			<a href="login.red">login</a>
+			<a href="login1.red">login</a>
 <%	else %>
 			<a href="javascript:logout()">logout</a>
 <%	end %>
--- a/src/login.red.luan	Sun Oct 22 23:53:59 2023 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,9 +0,0 @@
-local Luan = require "luan:Luan.luan"
-local error = Luan.error
-local Http = require "luan:http/Http.luan"
-
-
-return function()
-	Http.response.set_persistent_cookie("user","whatever")
-	Http.response.send_redirect "/"
-end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/login1.red.luan	Mon Oct 23 00:43:41 2023 -0600
@@ -0,0 +1,19 @@
+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 Http = require "luan:http/Http.luan"
+local Config = require "site:/lib/Config.luan"
+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 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)
+	Http.response.send_redirect(url)
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/login2.red.luan	Mon Oct 23 00:43:41 2023 -0600
@@ -0,0 +1,10 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local Http = require "luan:http/Http.luan"
+
+
+return function()
+	local code = Http.request.parameters.code or error()
+	Http.response.set_persistent_cookie("user",code)
+	Http.response.send_redirect "/"
+end