fixed login flow inconsistency

This commit is contained in:
ari melody 2024-07-02 20:21:34 +01:00
parent 998e8f2517
commit abab0df83f
Signed by: ari
GPG key ID: CF99829C92678188
10 changed files with 65 additions and 45 deletions

View file

@ -3,27 +3,25 @@
import Navigation from '$lib/ui/Navigation.svelte';
import Widgets from '$lib/ui/Widgets.svelte';
import { client, Client } from '$lib/client/client.js';
import { user, getUser } from '$lib/stores/user.js';
import { get } from 'svelte/store';
import { logged_in } from '$lib/stores/user.js';
import { unread_notif_count, last_read_notif_id } from '$lib/notifications.js';
let ready = new Promise(resolve => {
if (get(client)) {
if (get(client).user) logged_in.set(true);
if (get(user)) logged_in.set(true);
return resolve();
}
let new_client = new Client();
new_client.load();
client.set(new_client);
return new_client.getClientUser().then(user => {
if (!user) {
client.set(new_client);
return resolve();
}
if (user) logged_in.set(true);
new_client.user = user;
window.peekie = new_client;
return getUser().then(new_user => {
if (!new_user) return resolve();
logged_in.set(true);
user.set(new_user);
// spin up async task to fetch notifications
get(client).getNotifications(
@ -33,10 +31,6 @@
unread_notif_count.set(notif_data.length);
});
client.update(client => {
client.user = user;
return client;
});
return resolve();
});
});

View file

@ -1,7 +1,7 @@
<script>
import { page } from '$app/stores';
import { get } from 'svelte/store';
import { client } from '$lib/client/client.js';
import { logged_in } from '$lib/stores/user.js';
import { timeline, getTimeline } from '$lib/timeline.js';
import LoginForm from '$lib/ui/LoginForm.svelte';
@ -9,16 +9,18 @@
import User from '$lib/user/user.js';
import Button from '$lib/ui/Button.svelte';
getTimeline();
logged_in.subscribe(logged_in => {
if (logged_in) getTimeline();
});
document.addEventListener("scroll", event => {
if (get(page).url.pathname !== "/") return;
if (get(logged_in) && get(page).url.pathname !== "/") return;
if (window.innerHeight + window.scrollY >= document.body.offsetHeight - 2048) {
getTimeline();
}
});
</script>
{#if $client.user}
{#if $logged_in}
<Feed posts={$timeline} />
{:else}
<LoginForm />

View file

@ -3,6 +3,8 @@
import { goto } from '$app/navigation';
import { error } from '@sveltejs/kit';
import { get } from 'svelte/store';
import { last_read_notif_id } from '$lib/notifications.js';
import { logged_in, user, getUser } from '$lib/stores/user.js';
export let data;
@ -23,24 +25,18 @@
return c;
});
get(client).getClientUser().then(user => {
if (user) client.update(client => {
client.user = user
return client;
});
getUser().then(new_user => {
if (!new_user) return;
logged_in.set(true);
user.set(new_user);
return get(client).getNotifications(
get(last_read_notification_id)
get(last_read_notif_id)
).then(notif_data => {
client.update(client => {
// we've just logged in, so assume all past notifications are read.
// i *would* just use the mastodon marker API to get the last read
// notification, but this does not appear to be widely supported.
if (notif_data.constructor === Array && notif_data.length > 0)
last_read_notification_id.set(notif_data[0].id);
client.save();
return client;
});
if (notif_data.constructor === Array && notif_data.length > 0)
last_read_notif_id.set(notif_data[0].id);
get(client).save();
goto("/");
});
});

View file

@ -1,10 +1,16 @@
<script>
import { notifications, getNotifications } from '$lib/notifications.js';
import { logged_in } from '$lib/stores/user.js';
import { goto } from '$app/navigation';
import { get } from 'svelte/store';
import Notification from '$lib/ui/Notification.svelte';
if (!get(logged_in)) goto("/");
getNotifications();
/*
document.addEventListener("scroll", event => {
if (get(logged_in) && get(page).url.pathname !== "/") return;
if (window.innerHeight + window.scrollY >= document.body.offsetHeight - 2048) {
getNotifications();
}