Skip to content

Commit

Permalink
Merge pull request #237 from BetterLectio/dev
Browse files Browse the repository at this point in the history
Update version and fix bugs (v1.1.0)
  • Loading branch information
victorDigital committed Jan 7, 2024
2 parents 4a5af02 + b2142d1 commit f5cbc54
Show file tree
Hide file tree
Showing 16 changed files with 564 additions and 34 deletions.
19 changes: 17 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "betterlectio",
"version": "0.10.139",
"version": "1.1.5",
"private": true,
"scripts": {
"dev": "vite dev",
Expand Down Expand Up @@ -36,6 +36,7 @@
"@event-calendar/time-grid": "^1.5.0",
"@netlify/functions": "^2.4.0",
"@sentry/sveltekit": "^7.80.0",
"chartjs-plugin-datalabels": "^2.2.0",
"crypto-es": "^2.1.0",
"dompurify": "^3.0.5",
"firebase": "^10.6.0",
Expand Down
71 changes: 71 additions & 0 deletions src/lib/js/globalConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { get, post } from '$lib/js/http.js';
import { cookieInfo } from '$lib/js/LectioCookieHandler';
import { writable } from 'svelte/store';

let configNonce = Date.now().toString(36);
let betlecMappeId = null;
let betlecConfigId = null;

async function setConfig(value, overwrite = true) {
console.log('Opdaterer config filen');
const body = {
fileName: 'config.json',
folderId: betlecMappeId,
contentType: 'application/json',
content: JSON.stringify(value),
fileComment: 'Denne fil er autogenereret af BetterLectio. Hvis du fjerner den kan du risikere at alle dine indstillinger på BetterLectio fjernes.',
public: false
};
if (overwrite) body.documentId = betlecConfigId;
await post('/dokument_upload', JSON.stringify(body));
}

const writableConfig = () => {
const { subscribe, set, update } = writable(undefined);

return {
subscribe,
set: value => {
if (value) setConfig(value);
return set(value);
},
update
};
};
export const config = writableConfig();

async function getConfig() {
configNonce = await Date.now().toString(36);
if (!betlecMappeId) {
const user = await cookieInfo();
const dokumenter = await get(`/dokumenter?folderid=S${user.userId}__&nonce=${configNonce}`);
const betlecMappe = dokumenter.indhold.filter(dokument => dokument.navn === '.betterlectio');
if (betlecMappe.length > 0) {
betlecMappeId = betlecMappe[0].id;
} else {
console.log('Opretter mappen');
await post('/opret_mappe', JSON.stringify({
folderName: '.betterlectio',
folderComment: 'Denne mappe er autogenereret af BetterLectio. Hvis du fjerner den kan du risikere at alle dine indstillinger, themes og plugins på BetterLectio fjernes.',
folderId: `S${user.userId}__`
}));
getConfig();
return;
}
}

const betlecMappeDokumenter = await get(`/dokumenter?folderid=${betlecMappeId}&nonce=${configNonce}`);
const _betlecConfigId = betlecMappeDokumenter.indhold.filter(dokument => dokument.navn === 'config.json');
if (_betlecConfigId.length > 0) {
betlecConfigId = _betlecConfigId[0].id;
console.log(betlecConfigId);
} else {
console.log('Opretter config fil');
await setConfig({}, false);
getConfig();
return;
}

config.set(await get(`/dokument_hent?id=${betlecConfigId}&configNonce=${configNonce}`));
}
getConfig();
19 changes: 19 additions & 0 deletions src/lib/js/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,22 @@ export async function post(endpoint, body) {
const response = await get(endpoint, body);
return response;
}


export async function getDocument(id, docType = null, expectedContentType = 'image/*') {
let url = `${api}/dokument_hent?id=${id}`;
if (docType) url += `&doctype=${docType}&raw=true`;
const response = await fetch(url,
{
headers:
{
'lectio-cookie': localStorage.getItem('lectio-cookie'),
'Access-Control-Allow-Origin': '*',
'content-type': expectedContentType
}
}, { method: 'GET' });

const blob = await response.blob();
const urlBlob = URL.createObjectURL(blob);
return urlBlob;
}
62 changes: 60 additions & 2 deletions src/routes/auth/+page.server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fail, redirect } from '@sveltejs/kit';
import { superValidate } from 'sveltekit-superforms/server';
import { z } from 'zod';
import { validCookie } from '$lib/js/serverCookies.js';
import { z } from 'zod';


const schema = z.object({
Expand All @@ -11,7 +11,8 @@ const schema = z.object({
id: z.string().min(2).max(4),
skole: z.string().min(2).max(268),
skoleid: z.string().min(2).max(4)
})
}),
remember: z.boolean().optional()
});

export const load = (async ({ cookies, url }) => {
Expand All @@ -24,6 +25,50 @@ export const load = (async ({ cookies, url }) => {
}
}
const form = await superValidate(schema);

let persistantSession = cookies.get('persistant-session');
if (persistantSession) {
persistantSession = JSON.parse(persistantSession);
form.data.username = persistantSession.username;
form.data.school = persistantSession.school;

// login and redirect if valid
const response = await fetch(`https://api.betterlectio.dk/auth`, {
headers: {
brugernavn: persistantSession.username,
adgangskode: persistantSession.password,
skoleid: persistantSession.school.skoleid
}
});

if (response.ok) {
// get the set-lectio-cookie header
const lectioCookie = response.headers.get('set-lectio-cookie');

// set a cookie with the lectio cookie
cookies.set('lectio-cookie', lectioCookie, {
path: '/',
maxAge: 60 * 60 * 24 * 7 // 1 week
});

// if remember me is checked, set a persistant session cookie
if (form.data.remember) {
cookies.set('persistant-session', JSON.stringify({
username: form.data.username,
password: form.data.password,
school: form.data.school
}), {
path: '/',
secure: true,
maxAge: 60 * 60 * 24 * 365 // 1 year
});
}

// redirect to the homepage (in future change to the page the user was on before logging in, but for now just redirect to the homepage because im lazy :) )
throw redirect(302, '/forside');
}
}

return { form };
});

Expand Down Expand Up @@ -59,6 +104,19 @@ export const actions = {
return fail(401, { form });
}

// if remember me is checked, set a persistant session cookie
if (form.data.remember) {
cookies.set('persistant-session', JSON.stringify({
username: form.data.username,
password: form.data.password,
school: form.data.school
}), {
path: '/',
secure: true,
maxAge: 60 * 60 * 24 * 365 // 1 year
});
}

return { form };
}
};
44 changes: 28 additions & 16 deletions src/routes/auth/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,20 @@
}
</script>

<!-- You can open the modal using ID.showModal() method -->
<dialog id="modal1" class="modal modal-bottom sm:modal-middle">
<div class="modal-box">
<form method="dialog">
<button class="btn btn-sm btn-circle btn-ghost absolute right-2 top-2">✕</button>
</form>
<h3 class="font-bold text-lg">Vigtig information vedrørende cookies</h3>
<p class="py-4">For at opretholde en vedvarende login-session kræver det, at vi gemmer dine oplysninger i en cookie. Vi fraskriver os ethvert ansvar i tilfælde af uautoriseret adgang til dine oplysninger som følge af denne lagring.</p>
<div class="modal-action">
<button class="btn btn-info btn-sm" onclick="modal1.close()">Jeg accepterer</button>
</div>
</div>
</dialog>

<div class="flex items-center justify-center md:h-[75vh]">
{#key isInAutoAuth}
{#if !isInAutoAuth}
Expand Down Expand Up @@ -248,8 +262,8 @@

>
</AutoComplete>
<div class="join p-1.5 bg-base-100">
<div class="flex join-item">
<div class="join p-1.5 bg-base-100 flex flex-col sm:flex-row">
<div class="flex join-item sm:mb-0 mb-2">
<input
type="checkbox"
bind:checked={saveSchoolChecked}
Expand All @@ -260,20 +274,18 @@
/>
<label class="block text-sm pr-0 font-medium px-3 select-none" for="saveSchoolIdCheck">Husk skole</label>
</div>
{#if api === 'http://localhost:5000'}
<div class="divider divider-horizontal"></div>
<div class="flex join-item">
<input
type="checkbox"
checked="checked"
id="saveLogin"
tabindex="0"
class="checkbox checkbox-sm"
name="saveLogin"
/>
<label class="block text-sm pr-0 font-medium px-3 select-none" for="saveLogin">Forbliv logget ind</label>
</div>
{/if}
<div class="divider divider-horizontal hidden sm:flex"></div>
<div class="flex join-item">
<input
bind:checked={$form.remember}
type="checkbox"
id="saveLogin"
tabindex="0"
class="checkbox checkbox-sm"
name="saveLogin"
/>
<label onclick="modal1.showModal()" class="text-sm pr-0 font-medium px-3 select-none flex gap-2 items-center" for="saveLogin"><span>Forbliv logget ind</span> <span><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-info-circle" viewBox="0 0 16 16"><path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14m0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16"/><path d="m8.93 6.588-2.29.287-.082.38.45.083c.294.07.352.176.288.469l-.738 3.468c-.194.897.105 1.319.808 1.319.545 0 1.178-.252 1.465-.598l.088-.416c-.2.176-.492.246-.686.246-.275 0-.375-.193-.304-.533zM9 4.5a1 1 0 1 1-2 0 1 1 0 0 1 2 0"/></svg></span></label>
</div>
</div>
<p class="text-xs mt-4">
Denne side bruger cookies til at huske dine oplysninger til næste gang, du logger ind. Når du logger ind, accepterer du, at din
Expand Down
Loading

0 comments on commit f5cbc54

Please sign in to comment.