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 {loginStore, profileStore, type LoginStore, type ProfileData} from "./stores/login";
|
||||||
import {getEnv} from "./utils/env";
|
import {getEnv} from "./utils/env";
|
||||||
|
|
||||||
let profile: ProfileData;
|
|
||||||
|
|
||||||
loginStore.subscribe((value: LoginStore) => {
|
loginStore.subscribe((value: LoginStore) => {
|
||||||
getMe();
|
getMe();
|
||||||
});
|
});
|
||||||
|
|
||||||
profileStore.subscribe((value: ProfileData) => (profile = value));
|
|
||||||
|
|
||||||
async function getMe() {
|
async function getMe() {
|
||||||
try {
|
try {
|
||||||
let p = <ProfileData>await getUser("@me");
|
let p = <ProfileData>await getUser("@me?from=app");
|
||||||
profileStore.set(p);
|
profileStore.set(p);
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
profileStore.set(undefined);
|
profileStore.set(undefined);
|
||||||
|
@ -39,8 +35,8 @@
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
<nav>
|
<nav>
|
||||||
{#if profile !== undefined}
|
{#if $profileStore !== undefined}
|
||||||
<HeaderDropdown {profile} />
|
<HeaderDropdown profile={$profileStore} />
|
||||||
{:else}
|
{:else}
|
||||||
<a href="/register" use:link>Register</a>
|
<a href="/register" use:link>Register</a>
|
||||||
<a href="/login" use:link>Login</a>
|
<a href="/login" use:link>Login</a>
|
||||||
|
|
|
@ -2,9 +2,12 @@
|
||||||
import {CheckCircle, ExternalLink, Link2, Lock, Slash, XCircle} from "lucide-svelte";
|
import {CheckCircle, ExternalLink, Link2, Lock, Slash, XCircle} from "lucide-svelte";
|
||||||
import {onMount} from "svelte";
|
import {onMount} from "svelte";
|
||||||
import {navigate, useLocation} from "svelte-navigator";
|
import {navigate, useLocation} from "svelte-navigator";
|
||||||
|
import {get} from "svelte/store";
|
||||||
import {getUser} from "~/api/login";
|
import {getUser} from "~/api/login";
|
||||||
import {getOAuthApp, getOAuthScopes, postAuthorize} from "~/api/oauth";
|
import {getOAuthApp, getOAuthScopes, postAuthorize} from "~/api/oauth";
|
||||||
import LazyDelay from "~/lib/LazyDelay.svelte";
|
import LazyDelay from "~/lib/LazyDelay.svelte";
|
||||||
|
import {profileStore} from "~/stores/login";
|
||||||
|
import {PromiseAllUnique} from "~/utils/promise-all-unique";
|
||||||
|
|
||||||
const fakeScope = [
|
const fakeScope = [
|
||||||
"Eat cake",
|
"Eat cake",
|
||||||
|
@ -39,23 +42,23 @@
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
let params = new URLSearchParams($location.search);
|
let params = new URLSearchParams($location.search);
|
||||||
try {
|
if (get(profileStore) === undefined) {
|
||||||
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 (_) {
|
|
||||||
let backParams = new URLSearchParams();
|
let backParams = new URLSearchParams();
|
||||||
backParams.set("back", window.location.pathname + window.location.search);
|
backParams.set("back", window.location.pathname + window.location.search);
|
||||||
navigate("/login?" + backParams.toString());
|
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() {
|
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