Add clear button

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

View file

@ -16,18 +16,20 @@ export async function load({ request, parent, url }) {
}
export const actions = {
set: async ({request, cookies}) => {
default: async ({request, cookies}) => {
let user = await getRequestUser(request, cookies);
if (!user)
return fail(401, {error: "unauthenticated"})
let submission = await request.formData();
let newAvatar = submission.get("newAvatar")
if (newAvatar !== undefined && !(newAvatar instanceof File))
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 newAvatar = undefined
if (submission.get("action") != "Clear") {
newAvatar = submission.get("newAvatar")
if (newAvatar !== undefined && !(newAvatar instanceof File))
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)
return {
@ -35,6 +37,7 @@ export const actions = {
message: Object.entries(time)
.map(([res, time]) => `${res}x${res} took ${time}ms to render`)
.join("\n")
|| "No timing information available"
}
}
}

View file

@ -36,9 +36,6 @@
form > input[type="file"]::file-selector-button {
display: none;
}
form > label {
flex-shrink: 0;
}
summary::marker {
content: ""
}
@ -74,10 +71,10 @@
</div>
</details>
</p>
<form method="post" enctype="multipart/form-data" action="?/set">
<label for="newAvatar">Set a new avatar &#x279C;</label>
<form method="post" enctype="multipart/form-data">
<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>
{#if form}
{#if form.success}