···24242525const postAction = (client: any, scopes: string[]) => {
2626 return async ({ request, cookies }: { request: Request, cookies: Cookies }) => {
2727+ const scopedCookies = scopeCookies(cookies)
2828+ scopedCookies.set("postAuth", client.name)
2729 const form = await request.formData()
2828- const author = form.get("author")?.toString().substring(0, 32).replace(/([^_a-z0-9]+)/gi, '')
2930 const content = form.get("content")?.toString().substring(0, 512)
3030- const scopedCookies = scopeCookies(cookies)
3131- if (author === undefined || content === undefined) {
3232- scopedCookies.set("sendError", "one of author or content fields are missing")
3333- redirect(303, auth.callbackUrl)
3434- }
3535- if (['dusk', 'yusdacra'].includes(author.trim())) {
3636- scopedCookies.set("sendError", "author cannot be dusk or yusdacra (those are my names choose something else smh)")
3131+ if (content === undefined) {
3232+ scopedCookies.set("sendError", "content field is missing")
3733 redirect(303, auth.callbackUrl)
3834 }
3935 // save form content in a cookie
4040- const params = new URLSearchParams({ author, content })
3636+ const params = new URLSearchParams({ content })
4137 scopedCookies.set("postData", params.toString())
4238 // get auth url to redirect user to
4339 const authUrl = auth.createAuthUrl((state) => client.getAuthUrl(state, scopes), cookies)
···6258 getRatelimited: false,
6359 }
6460 const rawPostData = scopedCookies.get("postData") || null
6565- if (rawPostData !== null) {
6161+ const postAuth = scopedCookies.get("postAuth") || null
6262+ if (rawPostData !== null && postAuth !== null) {
6663 // delete the postData cookie after we got it cause we dont need it anymore
6764 scopedCookies.delete("postData")
6565+ scopedCookies.delete("postAuth")
6866 // check if we are landing from an auth from a post action
6967 let code: string | null = null
7068 // try to get the code, fails if invalid oauth request
···7371 } catch (err: any) {
7472 data.sendError = err.toString()
7573 }
7676- // if we do have a code, then actually make the put request to guestbook server
7777- if (code !== null) {
7474+ // if we do have a code, then make the access token request
7575+ const authClient = auth.getAuthClient(postAuth)
7676+ if (authClient !== null && code !== null) {
7777+ // get and validate access token, also get username
7878+ let author: string
7979+ try {
8080+ const tokenResp = await authClient.getToken(code)
8181+ author = await authClient.identifyToken(tokenResp)
8282+ } catch(err: any) {
8383+ scopedCookies.set("sendError", `oauth failed: ${err.toString()}`)
8484+ redirect(303, auth.callbackUrl)
8585+ }
7886 let respRaw: Response
7987 try {
8088 const postData = new URLSearchParams(rawPostData)
8181- respRaw = await fetch(`${GUESTBOOK_BASE_URL}`, { method: 'POST', body: postData })
8989+ // set author to the identified value we got
9090+ postData.set('author', author)
9191+ // return error if content was not set or if empty
9292+ const content = postData.get('content')
9393+ if (content === null || content.trim().length === 0) {
9494+ scopedCookies.set("sendError", `content field was empty`)
9595+ redirect(303, auth.callbackUrl)
9696+ }
9797+ // set content, make sure to trim it
9898+ postData.set('content', content.substring(0, 512).trim())
9999+ respRaw = await fetch(GUESTBOOK_BASE_URL, { method: 'POST', body: postData })
82100 } catch (err: any) {
83101 scopedCookies.set("sendError", `${err.toString()} (is guestbook server running?)`)
84102 redirect(303, auth.callbackUrl)
···97115 data.page = Math.max(data.page, 1)
98116 let respRaw: Response
99117 try {
100100- respRaw = await fetch(GUESTBOOK_BASE_URL + "/" + data.page)
118118+ respRaw = await fetch(`${GUESTBOOK_BASE_URL}/${data.page}`)
101119 } catch (err: any) {
102120 data.getError = `${err.toString()} (is guestbook server running?)`
103121 return data
+1-13
src/routes/guestbook/+page.svelte
···1616 just fill the post in and click on your preferred auth method to post
1717 </p>
1818 <p>rules: be a good human bean pretty please</p>
1919- <p>
2020- (note: the author name must only include alphanumerical characters or underscore, and must
2121- be less than 32 characters)
2222- </p>
2319 <form method="post">
2420 <div class="entry entryflex">
2521 <div class="flex flex-row">
···3430 required
3531 />
3632 <p class="place-self-end text-sm font-monospace">
3737- --- posted by <input
3838- type="text"
3939- name="author"
4040- placeholder="author"
4141- class="p-0 bg-inherit border-hidden max-w-[16ch] text-right text-sm text-shadow-white placeholder-shown:[text-shadow:none] [field-sizing:content]"
4242- pattern="[_a-zA-Z0-9]+"
4343- maxlength="32"
4444- required
4545- />
3333+ --- posted by ...
4634 </p>
4735 </div>
4836 <div class="entry flex flex-wrap gap-1.5 p-1">