From abab0df83f3da1a01b58bba2f3787e09e9e893c7 Mon Sep 17 00:00:00 2001 From: ari melody Date: Tue, 2 Jul 2024 20:21:34 +0100 Subject: [PATCH] fixed login flow inconsistency --- src/lib/client/api.js | 12 +++++++----- src/lib/client/client.js | 3 ++- src/lib/notifications.js | 2 +- src/lib/stores/user.js | 20 +++++++++++++++++++- src/lib/ui/Navigation.svelte | 9 +++++---- src/lib/ui/Notification.svelte | 2 +- src/routes/+layout.svelte | 20 +++++++------------- src/routes/+page.svelte | 10 ++++++---- src/routes/callback/+page.svelte | 26 +++++++++++--------------- src/routes/notifications/+page.svelte | 6 ++++++ 10 files changed, 65 insertions(+), 45 deletions(-) diff --git a/src/lib/client/api.js b/src/lib/client/api.js index c3accbb..09e6514 100644 --- a/src/lib/client/api.js +++ b/src/lib/client/api.js @@ -1,8 +1,9 @@ -import { client } from '../client/client.js'; +import { client } from '$lib/client/client.js'; +import { user } from '$lib/stores/user.js'; import { capabilities } from '../client/instance.js'; -import Post from '../post.js'; -import User from '../user/user.js'; -import Emoji from '../emoji.js'; +import Post from '$lib/post.js'; +import User from '$lib/user/user.js'; +import Emoji from '$lib/emoji.js'; import { get } from 'svelte/store'; export async function createApp(host) { @@ -92,7 +93,7 @@ export async function verifyCredentials() { } export async function getNotifications(since_id, limit, types) { - if (!get(client).user) return false; + if (!get(user)) return false; let url = `https://${get(client).instance.host}/api/v1/notifications`; @@ -112,6 +113,7 @@ export async function getNotifications(since_id, limit, types) { } export async function getTimeline(last_post_id) { + if (!get(user)) return false; let url = `https://${get(client).instance.host}/api/v1/timelines/home`; if (last_post_id) url += "?max_id=" + last_post_id; const data = await fetch(url, { diff --git a/src/lib/client/client.js b/src/lib/client/client.js index 3ac4269..e6d79ac 100644 --- a/src/lib/client/client.js +++ b/src/lib/client/client.js @@ -2,7 +2,7 @@ import { Instance, server_types } from './instance.js'; import * as api from './api.js'; import { get, writable } from 'svelte/store'; import { last_read_notif_id } from '$lib/notifications.js'; -import { user } from '$lib/stores/user.js'; +import { user, logged_in } from '$lib/stores/user.js'; export const client = writable(false); @@ -206,6 +206,7 @@ export class Client { console.warn("Failed to log out correctly; ditching the old tokens anyways."); } localStorage.removeItem(save_name); + logged_in.set(false); client.set(new Client()); console.log("Logged out successfully."); } diff --git a/src/lib/notifications.js b/src/lib/notifications.js index 5d16795..bbdc69f 100644 --- a/src/lib/notifications.js +++ b/src/lib/notifications.js @@ -29,7 +29,7 @@ export async function getNotifications() { } } } - notif.status = await api.parsePost(notif.status, 0, false); + notif.status = notif.status ? await api.parsePost(notif.status, 0, false) : null; notifications.update(notifications => [...notifications, notif]); } last_read_notif_id.set(data[0].id); diff --git a/src/lib/stores/user.js b/src/lib/stores/user.js index 6648031..fb9c2c4 100644 --- a/src/lib/stores/user.js +++ b/src/lib/stores/user.js @@ -1,4 +1,22 @@ -import { writable } from 'svelte/store'; +import { client } from '$lib/client/client.js'; +import * as api from '$lib/client/api.js'; +import { get, writable } from 'svelte/store'; export let user = writable(0); export let logged_in = writable(false); + +export async function getUser() { + // already known + if (get(user)) return get(user); + + // cannot provide- not logged in + if (!get(client).app || !get(client).app.token) return false; + + // logged in- attempt to retrieve using token + const data = await api.verifyCredentials(); + if (!data) return false; + + user.set(await api.parseUser(data)); + console.log(`Logged in as @${get(user).username}@${get(user).host}`); + return get(user); +} diff --git a/src/lib/ui/Navigation.svelte b/src/lib/ui/Navigation.svelte index 4c8344b..c4ce6e5 100644 --- a/src/lib/ui/Navigation.svelte +++ b/src/lib/ui/Navigation.svelte @@ -3,6 +3,7 @@ import Button from './Button.svelte'; import Feed from './Feed.svelte'; import { client } from '$lib/client/client.js'; + import { user } from '$lib/stores/user.js'; import { play_sound } from '$lib/sound.js'; import { getTimeline } from '$lib/timeline.js'; import { getNotifications } from '$lib/notifications.js'; @@ -150,11 +151,11 @@
- play_sound()}> + play_sound()}>
diff --git a/src/lib/ui/Notification.svelte b/src/lib/ui/Notification.svelte index dd29e6f..bfec23f 100644 --- a/src/lib/ui/Notification.svelte +++ b/src/lib/ui/Notification.svelte @@ -48,7 +48,7 @@ } - +
{#if data.type === "favourite"} diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 98f196d..27eab4b 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -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(); }); }); diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 84e9491..05c2764 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -1,7 +1,7 @@ -{#if $client.user} +{#if $logged_in} {:else} diff --git a/src/routes/callback/+page.svelte b/src/routes/callback/+page.svelte index 4049ce5..9540cb8 100644 --- a/src/routes/callback/+page.svelte +++ b/src/routes/callback/+page.svelte @@ -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("/"); }); }); diff --git a/src/routes/notifications/+page.svelte b/src/routes/notifications/+page.svelte index 32c02f7..25e3e97 100644 --- a/src/routes/notifications/+page.svelte +++ b/src/routes/notifications/+page.svelte @@ -1,10 +1,16 @@