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

Simplify login code a bit more #4167

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
11 changes: 3 additions & 8 deletions app/src/lib/components/Login.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import { getContext } from '$lib/utils/context';

const userService = getContext(UserService);
const loading = userService.loading;
const user = userService.user;

export let wide = false;

let signUpOrLoginLoading = false;
let token: LoginToken | undefined;
</script>

Expand Down Expand Up @@ -41,16 +41,11 @@
<Button
style="pop"
kind="solid"
loading={signUpOrLoginLoading}
loading={$loading}
icon="signin"
{wide}
on:mousedown={async () => {
signUpOrLoginLoading = true;
try {
await userService.login();
} finally {
signUpOrLoginLoading = false;
}
await userService.login();
}}
>
Sign up or Log in
Expand Down
25 changes: 5 additions & 20 deletions app/src/lib/components/WelcomeSigninAction.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,17 @@
'Enable GitButler features like automatic branch and commit message generation.';

const userService = getContext(UserService);
const loading = userService.loading;
const user = userService.user;

let loginSignupLoading = false;

async function onLoginOrSignup() {
loginSignupLoading = true;
try {
await userService.login();
} finally {
loginSignupLoading = false;
}
}

// reset loading state after 60 seconds
// this is to prevent the loading state from getting stuck
// if the user closes the tab before the request is finished
setTimeout(() => {
loginSignupLoading = false;
}, 60 * 1000);
</script>

{#if !$user}
<WelcomeAction
title="Log in or Sign up"
loading={loginSignupLoading}
on:mousedown={onLoginOrSignup}
loading={$loading}
on:mousedown={async () => {
await userService.login();
}}
>
<svelte:fragment slot="icon">
{@html signinSvg}
Expand Down
9 changes: 5 additions & 4 deletions app/src/lib/stores/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { observableToStore } from '$lib/rxjs/store';
import { sleep } from '$lib/utils/sleep';
import { openExternalUrl } from '$lib/utils/url';
import { plainToInstance } from 'class-transformer';
import { BehaviorSubject, Observable, Subject, distinct, map, merge, shareReplay } from 'rxjs';
import { Observable, Subject, distinct, map, merge, shareReplay } from 'rxjs';
import { writable } from 'svelte/store';
import type { Readable } from 'svelte/motion';

export type LoginToken = {
Expand All @@ -18,7 +19,7 @@ export type LoginToken = {

export class UserService {
private reset$ = new Subject<User | undefined>();
private loading$ = new BehaviorSubject(false);
readonly loading = writable(false);

private user$ = merge(
new Observable<User | undefined>((subscriber) => {
Expand Down Expand Up @@ -65,7 +66,7 @@ export class UserService {

async login(): Promise<User | undefined> {
this.logout();
this.loading$.next(true);
this.loading.set(true);
try {
// Create login token
const token = await this.httpClient.post<LoginToken>('login/token.json');
Expand All @@ -84,7 +85,7 @@ export class UserService {
console.error(err);
showError('Something went wrong', err);
} finally {
this.loading$.next(false);
this.loading.set(false);
}
}

Expand Down