Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug where no error toast shown on login failure #4160

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix bug where no error toast shown on login failure
  • Loading branch information
mtsgrd committed Jun 24, 2024
commit b44053d417a1ffc416b9bf1e2602850ec0502b5b
7 changes: 1 addition & 6 deletions app/src/lib/components/Login.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script lang="ts">
import Link from '../shared/Link.svelte';
import { showError } from '$lib/notifications/toasts';
import Button from '$lib/shared/Button.svelte';
import { UserService, type LoginToken } from '$lib/stores/user';
import { getContext } from '$lib/utils/context';
Expand Down Expand Up @@ -48,11 +47,7 @@
on:mousedown={async () => {
signUpOrLoginLoading = true;
try {
token = await userService.createLoginToken();
await userService.login(token);
} catch (err) {
console.error(err);
showError('Could not create login token', err);
await userService.login();
} finally {
signUpOrLoginLoading = false;
}
Expand Down
5 changes: 2 additions & 3 deletions app/src/lib/components/WelcomeSigninAction.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
async function onLoginOrSignup() {
loginSignupLoading = true;
try {
const token = await userService.createLoginToken();
await userService.login(token);
} catch {
await userService.login();
} finally {
loginSignupLoading = false;
}
}
Expand Down
22 changes: 10 additions & 12 deletions app/src/lib/stores/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { resetPostHog, setPostHogUser } from '$lib/analytics/posthog';
import { resetSentry, setSentryUser } from '$lib/analytics/sentry';
import { API_URL, type HttpClient } from '$lib/backend/httpClient';
import { invoke } from '$lib/backend/ipc';
import { showError } from '$lib/notifications/toasts';
import { observableToStore } from '$lib/rxjs/store';
import { sleep } from '$lib/utils/sleep';
import { openExternalUrl } from '$lib/utils/url';
Expand Down Expand Up @@ -62,11 +63,15 @@ export class UserService {
resetSentry();
}

async login(token: LoginToken): Promise<User | undefined> {
async login(): Promise<User | undefined> {
this.logout();
this.loading$.next(true);
try {
openExternalUrl(token.url);
// Create login token
const token = await this.httpClient.post<LoginToken>('login/token.json');
const url = new URL(token.url);
url.host = API_URL.host;
openExternalUrl(url.toString());

// Assumed min time for login flow
await sleep(4000);
Expand All @@ -75,21 +80,14 @@ export class UserService {
this.setUser(user);

return user;
} catch (err) {
console.error(err);
showError('Something went wrong', err);
} finally {
this.loading$.next(false);
}
}

async createLoginToken(): Promise<LoginToken> {
const token = await this.httpClient.post<LoginToken>('login/token.json');
const url = new URL(token.url);
url.host = API_URL.host;
return {
...token,
url: url.toString()
};
}

async pollForUser(token: string): Promise<User | undefined> {
let apiUser: User | null;
for (let i = 0; i < 120; i++) {
Expand Down