-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #229 from BetterLectio/auth-serverside-dev
Auth serverside dev
- Loading branch information
Showing
21 changed files
with
304 additions
and
296 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export async function validCookie(base64Cookie) { | ||
const cookies = await JSON.parse(await Buffer.from(base64Cookie, 'base64')); | ||
const skoleId = cookies[await (await cookies.map(cookie => cookie.name)).indexOf('LastLoginExamno')].value; | ||
const response = await fetch(`https://www.lectio.dk/lectio/${skoleId}/help/mainhelp.aspx`, { headers: { cookie: await (await cookies.map(cookie => `${cookie.name}=${cookie.value}`)).join('; ') } }); | ||
|
||
return (await response.text()).includes('Log ud'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,29 @@ | ||
import { Redirect } from '@sveltejs/kit'; | ||
import { redirect } from '@sveltejs/kit'; | ||
import { validCookie } from '../lib/js/serverCookies.js'; | ||
|
||
/** @type {import('./$types').LayoutServerLoad} */ | ||
export async function load({ cookies, url }) { | ||
// check cookies to check if user is logged in | ||
// if not, redirect to /login | ||
|
||
return {}; | ||
// make the redirect in case redirect its needed | ||
const redirectFromAuth = encodeURIComponent(url.href); | ||
try { | ||
const lectioCookie = cookies.get('lectio-cookie'); | ||
if (url.pathname !== '/auth') { | ||
if (!lectioCookie) { | ||
throw redirect(302, `/auth${redirectFromAuth ? `?redirect=${redirectFromAuth}` : ''}`); | ||
} | ||
// Check if the cookie is valid | ||
const cookieIsValid = await validCookie(lectioCookie); | ||
if (!cookieIsValid) { | ||
// delete the cookie if it exists | ||
if (cookies.get('lectio-cookie')) cookies.delete('lectio-cookie'); | ||
// redirect to auth page | ||
throw redirect(302, `/auth${redirectFromAuth ? `?redirect=${redirectFromAuth}` : ''}`); | ||
} | ||
} | ||
return { lectioCookie, pathname: url.pathname }; | ||
} catch (error) { | ||
// redirect to auth page and pass the redirect url and delete the cookie | ||
console.error('Cookie-check error: ',error); | ||
if (cookies.get('lectio-cookie')) cookies.delete('lectio-cookie'); | ||
throw redirect(302, `/auth${redirectFromAuth ? `?redirect=${redirectFromAuth}` : ''}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { redirect } from '@sveltejs/kit'; | ||
|
||
|
||
export function load({ cookies, url }) { | ||
// if the path is "/" and the user has a cookie, redirect to "/forside" | ||
const lectioCookie = cookies.get('lectio-cookie'); | ||
if (url.pathname === '/' && lectioCookie) throw redirect(302, '/forside'); | ||
return {}; | ||
} |
Oops, something went wrong.