Add clear button

This commit is contained in:
split / May 2024-07-10 22:40:38 -07:00
parent 3199cfd8dd
commit a470c82ff6
Signed by: split
GPG key ID: C325C61F0BF517C0
3 changed files with 20 additions and 18 deletions

View file

@ -21,7 +21,7 @@ export function launchLogin(url: string) {
response_type: "code", response_type: "code",
client_id: configuration.oauth2.client.id, client_id: configuration.oauth2.client.id,
redirect_uri: url, redirect_uri: url,
scope: "openid profile", scope: configuration.oauth2.client.scopes,
state state
}) })
// Did not think this would work lmao // Did not think this would work lmao
@ -97,16 +97,18 @@ export async function getUserInfo(id: string) {
userInfo = userInfoCache.get(tokenInfo.owner) userInfo = userInfoCache.get(tokenInfo.owner)
else { else {
let userInfoRequest = await fetchUserInfo(tokenInfo.token) let userInfoRequest = await fetchUserInfo(tokenInfo.token)
console.log(`userinforequest with ${userInfoRequest.status}`)
if (!userInfoRequest.ok) { if (!userInfoRequest.ok) {
// assume that token has expired. // assume that token has expired.
// try fetching a new one // try fetching a new one
console.log("refresh token", tokenInfo.refreshToken)
if (!tokenInfo.refreshToken) return // no refresh token. back out if (!tokenInfo.refreshToken) return // no refresh token. back out
let token = await getNewToken({ let token = await getNewToken({
grant_type: "refresh_token", grant_type: "refresh_token",
refresh_token: tokenInfo.refreshToken refresh_token: tokenInfo.refreshToken
}) })
console.log("new token", token)
console.log("new reftoken", token?.refresh_token)
if (!token) return // refresh failed. back out if (!token) return // refresh failed. back out
await prisma.token.update({ await prisma.token.update({
where: { id }, where: { id },
@ -139,7 +141,7 @@ export async function getUserInfo(id: string) {
// cache userinfo // cache userinfo
userInfoCache.set(tokenInfo.owner, userInfo) userInfoCache.set(tokenInfo.owner, userInfo)
setTimeout(() => userInfoCache.delete(tokenInfo.owner), 60*60*1000) setTimeout(() => userInfoCache.delete(tokenInfo.owner), 15*60*1000)
} }
return { ...userInfo, identifier: userInfo[configuration.userinfo.identifier] } as User return { ...userInfo, identifier: userInfo[configuration.userinfo.identifier] } as User
@ -179,7 +181,7 @@ export async function getRequestUser(request: Request, cookies: Cookies) {
// could cache this, but lazy // could cache this, but lazy
let userInfo = await (await fetchUserInfo(tokens.access_token)).json() as User let userInfo = await (await fetchUserInfo(tokens.access_token)).json() as User
console.log(tokens.refresh_token)
// create a new token // create a new token
let newToken = await prisma.token.create({ let newToken = await prisma.token.create({
data: { data: {

View file

@ -16,18 +16,20 @@ export async function load({ request, parent, url }) {
} }
export const actions = { export const actions = {
set: async ({request, cookies}) => { default: async ({request, cookies}) => {
let user = await getRequestUser(request, cookies); let user = await getRequestUser(request, cookies);
if (!user) if (!user)
return fail(401, {error: "unauthenticated"}) return fail(401, {error: "unauthenticated"})
let submission = await request.formData(); let submission = await request.formData();
let newAvatar = submission.get("newAvatar") let newAvatar = undefined
if (newAvatar !== undefined && !(newAvatar instanceof File)) if (submission.get("action") != "Clear") {
return fail(400, {success: false, error: "incorrect entry type"}) newAvatar = submission.get("newAvatar")
if (!configuration.allowed_types.includes(newAvatar.type)) if (newAvatar !== undefined && !(newAvatar instanceof File))
return fail(400, {success: false, error: `allowed types does not include ${newAvatar.type}`}) return fail(400, {success: false, error: "incorrect entry type"})
if (!configuration.allowed_types.includes(newAvatar.type))
return fail(400, {success: false, error: `allowed types does not include ${newAvatar.type}`})
}
let time = await setNewAvatar(user.sub, newAvatar) let time = await setNewAvatar(user.sub, newAvatar)
return { return {
@ -35,6 +37,7 @@ export const actions = {
message: Object.entries(time) message: Object.entries(time)
.map(([res, time]) => `${res}x${res} took ${time}ms to render`) .map(([res, time]) => `${res}x${res} took ${time}ms to render`)
.join("\n") .join("\n")
|| "No timing information available"
} }
} }
} }

View file

@ -36,9 +36,6 @@
form > input[type="file"]::file-selector-button { form > input[type="file"]::file-selector-button {
display: none; display: none;
} }
form > label {
flex-shrink: 0;
}
summary::marker { summary::marker {
content: "" content: ""
} }
@ -74,10 +71,10 @@
</div> </div>
</details> </details>
</p> </p>
<form method="post" enctype="multipart/form-data" action="?/set"> <form method="post" enctype="multipart/form-data">
<label for="newAvatar">Set a new avatar &#x279C;</label>
<input type="file" bind:files={files} accept={data.allowedImageTypes.join(",")} name="newAvatar"> <input type="file" bind:files={files} accept={data.allowedImageTypes.join(",")} name="newAvatar">
<input type="submit" value="Upload"> <input type="submit" name="action" value="Upload">
<input type="submit" name="action" value="Clear">
</form> </form>
{#if form} {#if form}
{#if form.success} {#if form.success}