fix login, use token from login

This commit is contained in:
Philipp 2024-04-24 03:09:01 +02:00
parent 1a4cbad65a
commit 8afa9be65f
Signed by: Philipp
GPG key ID: 9EBD8439AFBAB750
7 changed files with 100 additions and 82 deletions

View file

@ -1,13 +1,6 @@
<script> <script>
import "../app.css"; import "../app.css";
export const load = async ({ request, locals, cookies }) => {
return {
user: locals.user
};
};
</script> </script>
<slot /> <slot />

View file

@ -1,26 +1,27 @@
import { fail, redirect } from '@sveltejs/kit'; import { fail, redirect } from '@sveltejs/kit';
export async function load({ cookies }) {
export const actions = { if (cookies.get("mToken")) {
default: async ({ request, cookies }) => { redirect(302, '/emotes');
//if (cookies.get('matrixToken')) {
// redirect(302, '/emotes');
//}
const data = await request.formData();
const homeserver = data.get('url');
const username = data.get('username');
const password = data.get('password');
let token = "";
if(!homeserver || !username || !password ) return fail(400, "Invalid credentials");
cookies.set(
'matrixToken', token, {
path: '/',
maxAge: 60 * 60 * 24 * 365,
httpOnly: false
},
);
redirect(302, '/');
} }
} }
export const actions = {
default: async ({ cookies, request }) => {
if (cookies.get("mToken")) {
redirect(302, '/emotes');
}
const data = await request.formData();
const token = data.get("token");
const homeserver = data.get("hs");
if(!homeserver || !token) return fail(400, "Invalid credentials");
cookies.set('mToken', token, { path: "/" });
cookies.set('hs', homeserver, { path: "/" });
return redirect(302, '/emotes')
}
};

View file

@ -1,8 +1,7 @@
<script> <script>
import { enhance } from '$app/forms'; import { enhance } from '$app/forms';
export let data;
let homeserver, username, password = ""; let homeserver, username, password = "";
let result = null; let formData = new FormData();
async function matrixLogin() { async function matrixLogin() {
const res = await fetch(homeserver + "/_matrix/client/r0/login", { const res = await fetch(homeserver + "/_matrix/client/r0/login", {
@ -15,8 +14,17 @@
}); });
const json = await res.json(); const json = await res.json();
result = json; formData.append("token", json.access_token);
if(result) data.matrixToken = result.access_token formData.append("hs", json.home_server);
await sendPost()
}
async function sendPost() {
const res = await fetch("/", {
method: "POST",
body: formData
});
} }
</script> </script>
@ -31,7 +39,7 @@
<h1 class="text-xl font-bold leading-tight tracking-tight text-gray-900 md:text-2xl dark:text-white"> <h1 class="text-xl font-bold leading-tight tracking-tight text-gray-900 md:text-2xl dark:text-white">
Sign in to your Matrix Account Sign in to your Matrix Account
</h1> </h1>
<form method="POST" class="space-y-4 md:space-y-3" use:enhance> <form class="space-y-4 md:space-y-3">
<div> <div>
<label for="url" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Homeserver</label> <label for="url" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Homeserver</label>
<input bind:value={homeserver} type="url" name="url" id="url" class="bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="https://matrix.boehm.sh"/> <input bind:value={homeserver} type="url" name="url" id="url" class="bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="https://matrix.boehm.sh"/>
@ -45,7 +53,7 @@
<input bind:value={password} type="password" name="password" id="password" placeholder="••••••••" class="bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" /> <input bind:value={password} type="password" name="password" id="password" placeholder="••••••••" class="bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" />
</div> </div>
<div class="mt-5"> <div class="mt-5">
<button on:click={matrixLogin} type="submit" class="w-full text-white bg-primary-600 hover:bg-primary-700 focus:ring-4 focus:outline-none focus:ring-primary-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-primary-600 dark:hover:bg-primary-700 dark:focus:ring-primary-800">Sign in</button> <button on:click|preventDefault={matrixLogin} type="submit" class="w-full text-white bg-primary-600 hover:bg-primary-700 focus:ring-4 focus:outline-none focus:ring-primary-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-primary-600 dark:hover:bg-primary-700 dark:focus:ring-primary-800">Sign in</button>
</div> </div>
</form> </form>
</div> </div>

View file

@ -0,0 +1,5 @@
<script>
import "./app.css";
</script>
<slot />

View file

@ -0,0 +1,6 @@
export const load = (async ({ cookies}) => {
const homeserver = cookies.get("hs");
const token = cookies.get("mToken");
return { homeserver: homeserver, token: token}
})

View file

@ -1,24 +1,29 @@
<script> <script>
import {flip} from 'svelte/animate'; import { flip } from 'svelte/animate';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
let baskets = []; let baskets = [];
let isLoading = true; let isLoading = true;
export let data;
const hs = "https://" + data.homeserver;
const token = data.token;
onMount(async () => { onMount(async () => {
const response = await fetch("https://matrix.org/_matrix/client/v3/rooms/!WesIJpYbWDvdAYThkW:matrix.org/state", { const response = await fetch(hs + "/_matrix/client/v3/rooms/!WesIJpYbWDvdAYThkW:matrix.org/state", {
method: "GET", method: "GET",
headers: { headers: {
"Authorization": "Bearer xxx" "Authorization": "Bearer " + token
} }
}) })
const data = await response.json(); const res = await response.json();
data.forEach(e => { res.forEach(e => {
if(e.type === "im.ponies.room_emotes" && e.content.pack != undefined) { if(e.type === "im.ponies.room_emotes" && e.content.pack != undefined) {
let prepareArr = []; let prepareArr = [];
for(const [key, value] of Object.entries(e.content.images)) { for(const [key, value] of Object.entries(e.content.images)) {
let image_id = value.url.split("/")[3]; let image_id = value.url.split("/")[3];
let hs = value.url.split("/")[2]; let emote_hs = value.url.split("/")[2];
prepareArr.push("https://matrix.boehm.sh/_matrix/media/r0/thumbnail/" + hs + "/" + image_id + "?width=256&height=256&method=crop") prepareArr.push(hs + "/_matrix/media/r0/thumbnail/" + emote_hs + "/" + image_id + "?width=256&height=256&method=crop")
} }
baskets.push({"name": e.content.pack.display_name, "items": prepareArr}) baskets.push({"name": e.content.pack.display_name, "items": prepareArr})
} }
@ -57,11 +62,11 @@ import { onMount } from 'svelte';
{:else} {:else}
{#each baskets as basket, basketIndex (basket)} {#each baskets as basket, basketIndex (basket)}
<div animate:flip> <div animate:flip>
<b>{basket.name}</b> <p>{basket.name}</p>
<ul <ul
class:hovering={hoveringOverBasket === basket.name} class:hovering={hoveringOverBasket === basket.name}
on:dragenter={() => hoveringOverBasket = basket.name} on:dragenter={() => hoveringOverBasket = basket.name}
on:dragleave={() => hoveringOverBasket = null} on:dragleave={() => hoveringOverBasket = null}
on:drop={event => drop(event, basketIndex)} on:drop={event => drop(event, basketIndex)}
ondragover="return false" > ondragover="return false" >
{#each basket.items as item, itemIndex (item)} {#each basket.items as item, itemIndex (item)}
@ -77,42 +82,3 @@ import { onMount } from 'svelte';
</div> </div>
{/each} {/each}
{/if} {/if}
<style>
.hovering {
border-color: orange;
}
.item {
display: inline; /* required for flip to work */
}
li {
background-color: lightgray;
cursor: pointer;
display: inline-block;
margin-right: 10px;
margin-top: 10px;
padding: 10px;
}
li:hover {
background: orange;
color: white;
}
ul {
border: solid lightgray 1px;
display: fit-content; /* required for drag & drop to work when .item display is inline */
padding: 10px;
margin-left: 10px;
width: 100%;
min-height: 100px;
table-layout: fixed;
}
b {
margin-left: 10px;
}
p {
margin-left: 10px;
}
img {
height: 32px;
}
</style>

39
src/routes/emotes/app.css Normal file
View file

@ -0,0 +1,39 @@
html {
background-color: black;
}
.hovering {
border-color: orange;
}
.item {
display: inline; /* required for flip to work */
}
li {
background-color: darkslategray;
cursor: pointer;
display: inline-block;
margin-right: 10px;
margin-top: 10px;
padding: 10px;
}
li:hover {
background: orange;
color: white;
}
ul {
border: solid lightgray 1px;
display: fit-content; /* required for drag & drop to work when .item display is inline */
padding: 10px;
margin-left: 10px;
width: 100%;
min-height: 100px;
table-layout: fixed;
}
p {
padding-top: 10px;
margin-left: 10px;
color: white;
}
img {
height: 32px;
}