Skip to content

Commit

Permalink
Uden om sveltekit server hvis api er lokal
Browse files Browse the repository at this point in the history
  • Loading branch information
jona799t committed Nov 21, 2023
1 parent 372768d commit 134297e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/routes/+layout.server.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { redirect } from '@sveltejs/kit';
import { validCookie } from '../lib/js/serverCookies.js';

export async function load({ cookies, url }) {
export async function load({ request, cookies, url }) {
// make the redirect in case redirect its needed
const redirectFromAuth = encodeURIComponent(url.href);
// If backend running on mobile
if (request.headers.get('user-agent') === 'BetterLectio Mobile') return { lectioCookie: 'local', pathname: url.pathname, authed: true, redirectFromAuth };
try {
const lectioCookie = cookies.get('lectio-cookie');
if (url.pathname !== '/auth' && url.pathname !== '/tos') {
Expand Down
6 changes: 3 additions & 3 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
export let data = null;
console.log(data);
if (data.lectioCookie) localStorage.setItem('lectio-cookie', data.lectioCookie);
else localStorage.removeItem('lectio-cookie');
if (data.lectioCookie && data.lectioCookie !== 'local') localStorage.setItem('lectio-cookie', data.lectioCookie);
else if (data.lectioCookie !== 'local') localStorage.removeItem('lectio-cookie');
$: authed = false;
Expand Down Expand Up @@ -104,7 +104,7 @@
}
function setCookie() {
localStorage.setItem('lectio-cookie', data.lectioCookie);
if (data.lectioCookie !== 'local') localStorage.setItem('lectio-cookie', data.lectioCookie);
authed = true;
}
Expand Down
3 changes: 1 addition & 2 deletions src/routes/auth/+page.server.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { fail } from '@sveltejs/kit';
import { fail, redirect } from '@sveltejs/kit';
import { superValidate } from 'sveltekit-superforms/server';
import { z } from 'zod';
import { validCookie } from '$lib/js/serverCookies.js';
import { redirect } from '@sveltejs/kit';


const schema = z.object({
Expand Down
35 changes: 30 additions & 5 deletions src/routes/auth/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,32 @@
console.log(plainOptions);
});
async function localLogin(e) {
if (api !== 'http://localhost:5000') return;
e.preventDefault();
const response = await fetch(`${api}/auth`, {
headers: {
brugernavn: $form.username,
adgangskode: $form.password,
skoleid: $form.school.skoleid
}
});
if (response.ok) {
// get the set-lectio-cookie header
const lectioCookie = response.headers.get('set-lectio-cookie');
// lectio-cookie in localstorage to prevent the server from reading the cookie.
if (lectioCookie && lectioCookie !== null) localStorage.setItem('lectio-cookie', lectioCookie);
const urlParams = new URLSearchParams(window.location.search);
const redirect = urlParams.get('redirect');
window.location.href = redirect ? redirect : '/forside';
}
console.log(await response.json());
}
async function qrLogin(url) {
const skoleId = await url.match(/\/\d+\//g).toString()
.replaceAll('/', '');
Expand All @@ -63,7 +89,7 @@
headers: {
userId,
QrId,
skoleId: $form.school
skoleId
}
});
if (response.ok) {
Expand Down Expand Up @@ -106,9 +132,8 @@
console.log(`Error scanning file. Reason: ${err}`);
});
}
</script>

<div class="flex items-center justify-center md:h-[75vh]">
{#key isInAutoAuth}
{#if !isInAutoAuth}
Expand All @@ -120,7 +145,7 @@
<button on:click={changeLoginType} class="tab {qrAuth ? 'tab-active' : ''}">QR kode</button>
</div>
<div class="divider mt-1 mb-2" />
<form use:enhance autocomplete="on" method="post">
<form use:enhance autocomplete="on" method="post" on:submit={localLogin}>
<div class="form-control w-full max-w-xl">
{#if qrAuth}
<div class="flex justify-center" on:drop|preventDefault={qrCodeDropped} on:dragover|preventDefault>
Expand All @@ -143,7 +168,7 @@
type="text"
name="username"
id="username-field"
placeholder="Lectio Brugernavn"
placeholder="Lectio brugernavn"
tabindex="0"
autocomplete="username"
class="input input-sm w-full max-w-wl mb-2.5 autofill:border-0 autofill:shadow-[inset_0_0_0px_1000px_hsl(var(--b1))]"
Expand Down

0 comments on commit 134297e

Please sign in to comment.