campfire/src/routes/callback/+page.svelte

53 lines
1.8 KiB
Svelte
Raw Normal View History

<script>
import * as api from '$lib/api.js';
import { server } from '$lib/client/server.js';
import { app } from '$lib/client/app.js';
import { parseAccount } from '$lib/account.js';
import { get } from 'svelte/store';
import { goto } from '$app/navigation';
import { error } from '@sveltejs/kit';
import { unread_notif_count, last_read_notif_id } from '$lib/notifications.js';
import { account } from '$lib/stores/account.js';
2025-07-13 20:44:54 +01:00
import Lang from '$lib/lang';
export let data;
2025-07-13 20:44:54 +01:00
const lang = Lang('en_GB');
let auth_code = data.code;
if (!auth_code || !get(server) || !get(app)) {
2025-07-13 20:44:54 +01:00
error(400, { message: lang.string('error.bad_request') });
} else {
api.getToken(get(server).host, get(app).id, get(app).secret, auth_code).then(token => {
if (!token) {
2025-07-13 20:44:54 +01:00
error(400, { message: lang.string('error.invalid_auth_code') });
}
app.update(app => {
app.token = token;
return app;
});
api.verifyCredentials(get(server).host, get(app).token).then(data => {
if (!data) return goto("/");
2024-07-02 20:21:34 +01:00
account.set(parseAccount(data));
2025-07-13 20:44:54 +01:00
console.log(lang.string('logs.logged_in', get(account).fqn));
// spin up async task to fetch notifications
return api.getNotifications(
get(server).host,
get(app).token,
2024-07-02 20:21:34 +01:00
get(last_read_notif_id)
).then(notif_data => {
unread_notif_count.set(0);
2024-07-02 20:21:34 +01:00
if (notif_data.constructor === Array && notif_data.length > 0)
last_read_notif_id.set(notif_data[0].id);
goto("/");
});
});
});
}
</script>