open login page only once in the browser

This commit is contained in:
Nikita Galaiko 2023-02-14 10:57:54 +01:00
parent ebbcf38487
commit 0dda0afc90
No known key found for this signature in database
GPG Key ID: EBAB54E845BA519D

View File

@ -1,6 +1,6 @@
<script lang="ts">
import {open} from '@tauri-apps/api/shell'
import Authentication, { type LoginToken } from "$lib/authentication";
import Authentication from "$lib/authentication";
import type { PageData } from "./$types";
export let data: PageData;
@ -8,9 +8,8 @@
const authApi = Authentication();
const pollForUser = async (token: LoginToken) => {
await open (token.url);
const apiUser = await authApi.login.user.get(token.token).catch(() => null);
const pollForUser = async (token: string) => {
const apiUser = await authApi.login.user.get(token).catch(() => null);
if (apiUser) {
user.set(apiUser);
return apiUser;
@ -30,11 +29,11 @@
{:else}
{#await authApi.login.token.create()}
<div>loading...</div>
{:then token}
{#await pollForUser(token)}
{:then {url, token}}
{#await Promise.all([open(url),pollForUser(token)])}
<div>Log in in your system browser</div>
<p>If you are not redirected automatically, you can
<button on:click={() => pollForUser(token)}>Try again</button>
<button on:click={() => open(url)}>Try again</button>
</p>
{/await}
{/await}