Stay on guild self service page after logging in

This commit is contained in:
Cadence Ember 2025-02-18 00:46:54 +13:00
parent 331f2f6a38
commit efd7cb3fd4
4 changed files with 29 additions and 13 deletions

View file

@ -39,7 +39,14 @@ function render(event, filename, locals) {
defaultContentType(event, "text/html; charset=utf-8")
const session = await auth.useSession(event)
const managed = await auth.getManagedGuilds(event)
const rel = x => getRelativePath(event.path, x)
const rel = (to, paramsObject) => {
let result = getRelativePath(event.path, to)
if (paramsObject) {
const params = new URLSearchParams(paramsObject)
result += "?" + params.toString()
}
return result
}
return template(Object.assign({},
getQuery(event), // Query parameters can be easily accessed on the top level but don't allow them to overwrite anything
globals, // Globals

View file

@ -18,8 +18,10 @@ block body
div
!= icons.Icons.IconInfo
div
- const self = `@${reg.sender_localpart}:${reg.ooye.server_name}`
strong You picked self-service mode
.mt4 To complete setup, you need to manually choose a Matrix space to link with #[strong= guild.name].
.mt4 On Matrix, invite #[code.s-code-block: a.fc-black.s-link(href=`https://matrix.to/#/${self}` target="_blank")= self] to a space. Then you can pick the space on this page.
h3.mt32.fs-category Choose a space
@ -33,13 +35,12 @@ block body
else
if session.data.mxid
tr
- const self = `@${reg.sender_localpart}:${reg.ooye.server_name}`
td.p16 On Matrix, invite #[code.s-code-block: a.s-link(href=`https://matrix.to/#/${self}` target="_blank")= self] to a space. Then you can pick it from this list.
td.p16 Invite the bridge to a space, and the space will show up here.
else
tr
td.d-flex.ai-center.pl16.g16
| You need to log in with Matrix first.
a.s-btn.s-btn__matrix.s-btn__outlined(href=rel("/log-in-with-matrix")) Log in with Matrix
a.s-btn.s-btn__matrix.s-btn__outlined(href=rel(`/log-in-with-matrix`, {next: `./guild?guild_id=${guild_id}`})) Log in with Matrix
h3.mt48.fs-category Auto-create
.s-card

View file

@ -7,6 +7,8 @@ block body
.d-flex.g16#form-container
.fl-grow1
form.d-flex.gy16.fd-column(method="post" action="/api/log-in-with-matrix" hx-post="/api/log-in-with-matrix" hx-indicator="#log-in-button" hx-select="#ok" hx-target="#form-container")
if next
input(type="hidden" name="next" value=next)
.d-flex.gy4.fd-column
label.s-label(for="mxid") Your Matrix ID
input.fl-grow1.s-input.wmx3#mxid(name="mxid" required placeholder="@user:example.org")

View file

@ -17,10 +17,12 @@ const auth = sync.require("../auth")
const schema = {
form: z.object({
mxid: z.string()
mxid: z.string(),
next: z.string().optional()
}),
token: z.object({
token: z.string()
token: z.string().optional(),
next: z.string().optional()
})
}
@ -43,17 +45,16 @@ const validToken = new LRUCache({max: 200})
*/
as.router.get("/log-in-with-matrix", defineEventHandler(async event => {
const parsed = await getValidatedQuery(event, schema.token.safeParse)
let {token, next} = await getValidatedQuery(event, schema.token.parse)
if (!parsed.success) {
if (!token) {
// We are in the first request and need to tell them to input their mxid
return pugSync.render(event, "log-in-with-matrix.pug", {})
return pugSync.render(event, "log-in-with-matrix.pug", {next})
}
const userAgent = getRequestHeader(event, "User-Agent")
if (userAgent?.match(/bot/)) throw createError({status: 400, data: "Sorry URL previewer, you can't have this URL."})
const token = parsed.data.token
if (!validToken.has(token)) return sendRedirect(event, `${reg.ooye.bridge_origin}/log-in-with-matrix`, 302)
const session = await auth.useSession(event)
@ -63,12 +64,13 @@ as.router.get("/log-in-with-matrix", defineEventHandler(async event => {
await session.update({mxid})
return sendRedirect(event, "./", 302) // open to homepage where they can see they're logged in
if (!next) next = "./" // open to homepage where they can see they're logged in
return sendRedirect(event, next, 302)
}))
as.router.post("/api/log-in-with-matrix", defineEventHandler(async event => {
const api = getAPI(event)
const {mxid} = await readValidatedBody(event, schema.form.parse)
const {mxid, next} = await readValidatedBody(event, schema.form.parse)
let roomID = null
// Don't extend a duplicate invite for the same user
@ -117,7 +119,11 @@ as.router.post("/api/log-in-with-matrix", defineEventHandler(async event => {
validToken.set(token, mxid)
console.log(`web log in requested for ${mxid}`)
const body = `Hi, this is Out Of Your Element! You just clicked the "log in" button on the website.\nOpen this link to finish: ${reg.ooye.bridge_origin}/log-in-with-matrix?token=${token}\nThe link can be used once.`
const paramsObject = {token}
if (next) paramsObject.next = next
const params = new URLSearchParams(paramsObject)
let link = `${reg.ooye.bridge_origin}/log-in-with-matrix?${params.toString()}`
const body = `Hi, this is Out Of Your Element! You just clicked the "log in" button on the website.\nOpen this link to finish: ${link}\nThe link can be used once.`
await api.sendEvent(roomID, "m.room.message", {
msgtype: "m.text",
body