chore: debug cookies better
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import { getSessionData } from '$lib/utils/services/auth/session';
|
import { getSessionData } from '$lib/utils/services/auth/session';
|
||||||
|
import { getCioCookieString } from '$lib/utils/functions/cookies';
|
||||||
import { type Handle, redirect } from '@sveltejs/kit';
|
import { type Handle, redirect } from '@sveltejs/kit';
|
||||||
import { isPublicApiRoute, isPublicRoute } from '$lib/utils/functions/routes/isPublicRoute';
|
import { isPublicApiRoute, isPublicRoute } from '$lib/utils/functions/routes/isPublicRoute';
|
||||||
import { ROUTE } from '$lib/utils/constants/routes';
|
import { ROUTE } from '$lib/utils/constants/routes';
|
||||||
@@ -22,12 +23,14 @@ export const handle: Handle = async (args) => {
|
|||||||
|
|
||||||
const handlePagesRoutes: Handle = async ({ event, resolve }) => {
|
const handlePagesRoutes: Handle = async ({ event, resolve }) => {
|
||||||
const { pathname } = event.url;
|
const { pathname } = event.url;
|
||||||
|
const hasCioCookie = Boolean(getCioCookieString(event.cookies));
|
||||||
|
|
||||||
if (isPublicRoute(pathname)) {
|
if (isPublicRoute(pathname)) {
|
||||||
return resolve(event);
|
return resolve(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!event.locals.user) {
|
if (!event.locals.user && !hasCioCookie) {
|
||||||
|
console.log('no user and no cio cookie, redirecting to login');
|
||||||
const shouldAddRedirectParam = !pathname.includes(ROUTE.LOGOUT);
|
const shouldAddRedirectParam = !pathname.includes(ROUTE.LOGOUT);
|
||||||
const fullPath = pathname + event.url.search;
|
const fullPath = pathname + event.url.search;
|
||||||
const redirectPath = shouldAddRedirectParam
|
const redirectPath = shouldAddRedirectParam
|
||||||
|
|||||||
@@ -111,6 +111,7 @@ class AppInitApi extends BaseApi {
|
|||||||
|
|
||||||
const redirect = page.url.searchParams.get('redirect');
|
const redirect = page.url.searchParams.get('redirect');
|
||||||
if (redirect) {
|
if (redirect) {
|
||||||
|
console.log('redirecting to', redirect);
|
||||||
// goto redirect won't accept dynamic url so we need to use window.location.href
|
// goto redirect won't accept dynamic url so we need to use window.location.href
|
||||||
window.location.href = redirect;
|
window.location.href = redirect;
|
||||||
return;
|
return;
|
||||||
@@ -118,6 +119,7 @@ class AppInitApi extends BaseApi {
|
|||||||
|
|
||||||
// This allows you to be on the landing page of an organization site and not be redirected
|
// This allows you to be on the landing page of an organization site and not be redirected
|
||||||
if (isOrgSite && window.location.pathname === '/') {
|
if (isOrgSite && window.location.pathname === '/') {
|
||||||
|
console.log('no redirect is needed');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,11 +132,13 @@ class AppInitApi extends BaseApi {
|
|||||||
if (isCloud) {
|
if (isCloud) {
|
||||||
const shouldRedirectToOnboarding = !userHasOrganizations && !isOrgSite;
|
const shouldRedirectToOnboarding = !userHasOrganizations && !isOrgSite;
|
||||||
if (shouldRedirectToOnboarding) {
|
if (shouldRedirectToOnboarding) {
|
||||||
|
console.log('cloud: redirecting to onboarding');
|
||||||
return goto(resolve(`/onboarding`, {}));
|
return goto(resolve(`/onboarding`, {}));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Self-hosted: when user has no orgs, route to /onboarding
|
// Self-hosted: when user has no orgs, route to /onboarding
|
||||||
if (!userHasOrganizations) {
|
if (!userHasOrganizations) {
|
||||||
|
console.log('self-hosted: redirecting to onboarding');
|
||||||
return goto(resolve(`/onboarding`, {}));
|
return goto(resolve(`/onboarding`, {}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -142,6 +146,7 @@ class AppInitApi extends BaseApi {
|
|||||||
if (!shouldRedirectOnAuth(page.url.pathname)) return;
|
if (!shouldRedirectOnAuth(page.url.pathname)) return;
|
||||||
|
|
||||||
const shouldGoToLMS = isCloud ? isOrgSite || !!isStudent : !!isStudent;
|
const shouldGoToLMS = isCloud ? isOrgSite || !!isStudent : !!isStudent;
|
||||||
|
console.log('redirecting to', shouldGoToLMS ? 'lms' : 'org');
|
||||||
return shouldGoToLMS ? this.goToLMS() : this.goToOrg();
|
return shouldGoToLMS ? this.goToLMS() : this.goToOrg();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import type { Cookies } from '@sveltejs/kit';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the classroomio cookie string for API/auth requests.
|
||||||
|
* Filters cookies whose names include "classroomio" and joins them as `name=value; ...`.
|
||||||
|
*/
|
||||||
|
export function getCioCookieString(cookies: Cookies): string {
|
||||||
|
return cookies
|
||||||
|
.getAll()
|
||||||
|
.filter((c) => c.name.includes('classroomio'))
|
||||||
|
.map((c) => `${c.name}=${c.value}`)
|
||||||
|
.join('; ');
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ import { get } from 'svelte/store';
|
|||||||
import { currentOrg } from '$lib/utils/store/org';
|
import { currentOrg } from '$lib/utils/store/org';
|
||||||
import type { Cookies } from '@sveltejs/kit';
|
import type { Cookies } from '@sveltejs/kit';
|
||||||
import { env } from '$env/dynamic/public';
|
import { env } from '$env/dynamic/public';
|
||||||
|
import { getCioCookieString } from '$lib/utils/functions/cookies';
|
||||||
|
|
||||||
export const getRequestBaseUrl = () => {
|
export const getRequestBaseUrl = () => {
|
||||||
if (typeof window === 'undefined') {
|
if (typeof window === 'undefined') {
|
||||||
@@ -221,11 +222,7 @@ export function getApiHeaders(
|
|||||||
cookies: Cookies,
|
cookies: Cookies,
|
||||||
orgId?: string
|
orgId?: string
|
||||||
): { headers: { cookie: string; 'cio-org-id'?: string } } {
|
): { headers: { cookie: string; 'cio-org-id'?: string } } {
|
||||||
const cioCookies = cookies
|
const cioCookies = getCioCookieString(cookies);
|
||||||
.getAll()
|
|
||||||
.filter((c) => c.name.includes('classroomio'))
|
|
||||||
.map((c) => `${c.name}=${c.value}`)
|
|
||||||
.join('; ');
|
|
||||||
|
|
||||||
console.log('cioCookies', cioCookies);
|
console.log('cioCookies', cioCookies);
|
||||||
console.log('cookies', cookies.getAll());
|
console.log('cookies', cookies.getAll());
|
||||||
|
|||||||
@@ -79,7 +79,7 @@
|
|||||||
const isSessionReady = $derived(!$session.isPending && !$session.isRefetching && $session.data);
|
const isSessionReady = $derived(!$session.isPending && !$session.isRefetching && $session.data);
|
||||||
|
|
||||||
// The goal of this effect is to make sure that we do a redirect to /login if the session data is expired
|
// The goal of this effect is to make sure that we do a redirect to /login if the session data is expired
|
||||||
// This should be happening on the hooks.server.ts but we've made the home page public so we can show a loading spinner while the app is initializing OR an error message if the backend is down.
|
// This should be happening on the hooks.server.ts (if there is no cookie) but we've made the home page public so we can show a loading spinner while the app is initializing OR an error message if the backend is down.
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
if ($session.isPending || $session.isRefetching || !!$session.data) {
|
if ($session.isPending || $session.isRefetching || !!$session.data) {
|
||||||
console.log('session is pending or refetching');
|
console.log('session is pending or refetching');
|
||||||
@@ -91,7 +91,7 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$session.data && path !== '/login') {
|
if (!$session.data && !path.startsWith('/login')) {
|
||||||
console.log('session data is not available, go to login');
|
console.log('session data is not available, go to login');
|
||||||
window.location.href = '/login';
|
window.location.href = '/login';
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user