Add promise all unique to run all promises
ci/woodpecker/push/build Pipeline was successful Details

This commit is contained in:
Melon 2023-01-03 15:20:01 +00:00
parent 88cb4166df
commit e5cab64698
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
3 changed files with 29 additions and 20 deletions

View File

@ -6,17 +6,13 @@
import {loginStore, profileStore, type LoginStore, type ProfileData} from "./stores/login";
import {getEnv} from "./utils/env";
let profile: ProfileData;
loginStore.subscribe((value: LoginStore) => {
getMe();
});
profileStore.subscribe((value: ProfileData) => (profile = value));
async function getMe() {
try {
let p = <ProfileData>await getUser("@me");
let p = <ProfileData>await getUser("@me?from=app");
profileStore.set(p);
} catch (_) {
profileStore.set(undefined);
@ -39,8 +35,8 @@
</a>
<nav>
{#if profile !== undefined}
<HeaderDropdown {profile} />
{#if $profileStore !== undefined}
<HeaderDropdown profile={$profileStore} />
{:else}
<a href="/register" use:link>Register</a>
<a href="/login" use:link>Login</a>

View File

@ -2,9 +2,12 @@
import {CheckCircle, ExternalLink, Link2, Lock, Slash, XCircle} from "lucide-svelte";
import {onMount} from "svelte";
import {navigate, useLocation} from "svelte-navigator";
import {get} from "svelte/store";
import {getUser} from "~/api/login";
import {getOAuthApp, getOAuthScopes, postAuthorize} from "~/api/oauth";
import LazyDelay from "~/lib/LazyDelay.svelte";
import {profileStore} from "~/stores/login";
import {PromiseAllUnique} from "~/utils/promise-all-unique";
const fakeScope = [
"Eat cake",
@ -39,23 +42,23 @@
onMount(async () => {
let params = new URLSearchParams($location.search);
try {
await getUser("@me");
try {
app = await getOAuthApp(params.get("client_id"));
try {
scopes = await getOAuthScopes(params.get("scope"));
} catch (err) {
navigate("/oauth/invalid-scope?" + new URLSearchParams({redirect: params.get("redirect_uri")}));
}
} catch (_) {
navigate("/oauth/invalid-app?" + new URLSearchParams({redirect: params.get("redirect_uri")}));
}
} catch (_) {
if (get(profileStore) === undefined) {
let backParams = new URLSearchParams();
backParams.set("back", window.location.pathname + window.location.search);
navigate("/login?" + backParams.toString());
}
try {
[app, scopes] = await PromiseAllUnique([getOAuthApp(params.get("client_id")), getOAuthScopes(params.get("scope"))]);
} catch (err) {
switch (err.index) {
case 0:
navigate("/oauth/invalid-app?" + new URLSearchParams({redirect: params.get("redirect_uri")}));
break;
case 1:
navigate("/oauth/invalid-scope?" + new URLSearchParams({redirect: params.get("redirect_uri")}));
break;
}
}
});
async function runAuthorize() {

View File

@ -0,0 +1,10 @@
export async function PromiseAllUnique(promises: Promise<any>[]): Promise<any> {
return await Promise.all(
promises.map((promise, i) =>
promise.catch(err => {
err.index = i;
throw err;
}),
),
);
}