Add promise all unique to run all promises
ci/woodpecker/push/build Pipeline was successful
Details
ci/woodpecker/push/build Pipeline was successful
Details
This commit is contained in:
parent
88cb4166df
commit
e5cab64698
|
@ -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>
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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;
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
Reference in New Issue