Skip to content

Commit

Permalink
Fix login redirect (#975)
Browse files Browse the repository at this point in the history
* remove await from logout redirect
* catch error when token is unparsable
* move login redirect to parent layout to reduce redundant code
  • Loading branch information
duranb authored Nov 1, 2023
1 parent d31472a commit edde9ae
Show file tree
Hide file tree
Showing 26 changed files with 57 additions and 192 deletions.
91 changes: 48 additions & 43 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,61 @@ import { isLoginEnabled } from './utilities/login';
import { ADMIN_ROLE } from './utilities/permissions';

export const handle: Handle = async ({ event, resolve }) => {
if (!isLoginEnabled()) {
const permissibleQueries = await effects.getUserQueries(null);
const rolePermissions = await effects.getRolePermissions(null);
event.locals.user = {
activeRole: ADMIN_ROLE,
allowedRoles: [ADMIN_ROLE],
defaultRole: ADMIN_ROLE,
id: 'unknown',
permissibleQueries,
rolePermissions,
token: '',
};
} else {
const cookieHeader = event.request.headers.get('cookie') ?? '';
const cookies = parse(cookieHeader);
const { activeRole: activeRoleCookie = null, user: userCookie = null } = cookies;
try {
if (!isLoginEnabled()) {
const permissibleQueries = await effects.getUserQueries(null);
const rolePermissions = await effects.getRolePermissions(null);
event.locals.user = {
activeRole: ADMIN_ROLE,
allowedRoles: [ADMIN_ROLE],
defaultRole: ADMIN_ROLE,
id: 'unknown',
permissibleQueries,
rolePermissions,
token: '',
};
} else {
const cookieHeader = event.request.headers.get('cookie') ?? '';
const cookies = parse(cookieHeader);
const { activeRole: activeRoleCookie = null, user: userCookie = null } = cookies;

if (userCookie) {
const userBuffer = Buffer.from(userCookie, 'base64');
const userStr = userBuffer.toString('utf-8');
const baseUser: BaseUser = JSON.parse(userStr);
const { success } = await effects.session(baseUser);
const decodedToken: ParsedUserToken = jwtDecode(baseUser.token);
if (userCookie) {
const userBuffer = Buffer.from(userCookie, 'base64');
const userStr = userBuffer.toString('utf-8');
const baseUser: BaseUser = JSON.parse(userStr);
const { success } = await effects.session(baseUser);
const decodedToken: ParsedUserToken = jwtDecode(baseUser.token);

if (success) {
const allowedRoles = decodedToken['https://hasura.io/jwt/claims']['x-hasura-allowed-roles'];
const defaultRole = decodedToken['https://hasura.io/jwt/claims']['x-hasura-default-role'];
const activeRole = activeRoleCookie ?? defaultRole;
const user: User = {
...baseUser,
activeRole,
allowedRoles,
defaultRole,
permissibleQueries: null,
rolePermissions: null,
};
const permissibleQueries = await effects.getUserQueries(user);
if (success) {
const allowedRoles = decodedToken['https://hasura.io/jwt/claims']['x-hasura-allowed-roles'];
const defaultRole = decodedToken['https://hasura.io/jwt/claims']['x-hasura-default-role'];
const activeRole = activeRoleCookie ?? defaultRole;
const user: User = {
...baseUser,
activeRole,
allowedRoles,
defaultRole,
permissibleQueries: null,
rolePermissions: null,
};
const permissibleQueries = await effects.getUserQueries(user);

const rolePermissions = await effects.getRolePermissions(user);
event.locals.user = {
...user,
permissibleQueries,
rolePermissions,
};
const rolePermissions = await effects.getRolePermissions(user);
event.locals.user = {
...user,
permissibleQueries,
rolePermissions,
};
} else {
event.locals.user = null;
}
} else {
event.locals.user = null;
}
} else {
event.locals.user = null;
}
} catch (e) {
console.log(e);
event.locals.user = null;
}

return await resolve(event);
Expand Down
8 changes: 7 additions & 1 deletion src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { base } from '$app/paths';
import { redirect } from '@sveltejs/kit';
import { shouldRedirectToLogin } from '../utilities/login';
import type { LayoutServerLoad } from './$types';

export const load: LayoutServerLoad = async ({ locals }) => {
export const load: LayoutServerLoad = async ({ locals, url }) => {
if (!url.pathname.includes('login') && shouldRedirectToLogin(locals.user)) {
throw redirect(302, `${base}/login`);
}
return { ...locals };
};
7 changes: 0 additions & 7 deletions src/routes/constraints/+page.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import { base } from '$app/paths';
import { redirect } from '@sveltejs/kit';
import effects from '../../utilities/effects';
import { shouldRedirectToLogin } from '../../utilities/login';
import type { PageLoad } from './$types';

export const load: PageLoad = async ({ parent }) => {
const { user } = await parent();

if (shouldRedirectToLogin(user)) {
throw redirect(302, `${base}/login`);
}

const {
modelMap: initialModelMap,
planMap: initialPlanMap,
Expand Down
5 changes: 0 additions & 5 deletions src/routes/constraints/edit/[id]/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ import { base } from '$app/paths';
import { redirect } from '@sveltejs/kit';
import effects from '../../../../utilities/effects';
import { parseFloatOrNull } from '../../../../utilities/generic';
import { shouldRedirectToLogin } from '../../../../utilities/login';
import type { PageLoad } from './$types';

export const load: PageLoad = async ({ parent, params }) => {
const { user } = await parent();

if (shouldRedirectToLogin(user)) {
throw redirect(302, `${base}/login`);
}

const { id: constraintIdParam } = params;

if (constraintIdParam !== null && constraintIdParam !== undefined) {
Expand Down
7 changes: 0 additions & 7 deletions src/routes/constraints/new/+page.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import { base } from '$app/paths';
import { redirect } from '@sveltejs/kit';
import effects from '../../../utilities/effects';
import { shouldRedirectToLogin } from '../../../utilities/login';
import type { PageLoad } from './$types';

export const load: PageLoad = async ({ parent }) => {
const { user } = await parent();

if (shouldRedirectToLogin(user)) {
throw redirect(302, `${base}/login`);
}

const {
modelMap: initialModelMap,
models: initialModels,
Expand Down
7 changes: 0 additions & 7 deletions src/routes/dictionaries/+page.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { base } from '$app/paths';
import { redirect } from '@sveltejs/kit';
import { shouldRedirectToLogin } from '../../utilities/login';
import type { PageLoad } from './$types';

export const load: PageLoad = async ({ parent }) => {
const { user } = await parent();

if (shouldRedirectToLogin(user)) {
throw redirect(302, `${base}/login`);
}

return { user };
};
7 changes: 0 additions & 7 deletions src/routes/expansion/rules/+page.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { base } from '$app/paths';
import { redirect } from '@sveltejs/kit';
import { shouldRedirectToLogin } from '../../../utilities/login';
import type { PageLoad } from './$types';

export const load: PageLoad = async ({ parent }) => {
const { user } = await parent();

if (shouldRedirectToLogin(user)) {
throw redirect(302, `${base}/login`);
}

return { user };
};
5 changes: 0 additions & 5 deletions src/routes/expansion/rules/edit/[id]/+page.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { base } from '$app/paths';
import { redirect } from '@sveltejs/kit';
import effects from '../../../../../utilities/effects';
import { shouldRedirectToLogin } from '../../../../../utilities/login';
import type { PageLoad } from './$types';

export const load: PageLoad = async ({ parent, params }) => {
const { user } = await parent();

if (shouldRedirectToLogin(user)) {
throw redirect(302, `${base}/login`);
}

const { id: ruleIdParam } = params;

if (ruleIdParam !== null && ruleIdParam !== undefined) {
Expand Down
7 changes: 0 additions & 7 deletions src/routes/expansion/rules/new/+page.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { base } from '$app/paths';
import { redirect } from '@sveltejs/kit';
import { shouldRedirectToLogin } from '../../../../utilities/login';
import type { PageLoad } from './$types';

export const load: PageLoad = async ({ parent }) => {
const { user } = await parent();

if (shouldRedirectToLogin(user)) {
throw redirect(302, `${base}/login`);
}

return { user };
};
7 changes: 0 additions & 7 deletions src/routes/expansion/runs/+page.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import { base } from '$app/paths';
import { redirect } from '@sveltejs/kit';
import effects from '../../../utilities/effects';
import { shouldRedirectToLogin } from '../../../utilities/login';
import type { PageLoad } from './$types';

export const load: PageLoad = async ({ parent }) => {
const { user } = await parent();

if (shouldRedirectToLogin(user)) {
throw redirect(302, `${base}/login`);
}

const expansionRuns = await effects.getExpansionRuns(user);

return { expansionRuns };
Expand Down
7 changes: 0 additions & 7 deletions src/routes/expansion/sets/+page.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { base } from '$app/paths';
import { redirect } from '@sveltejs/kit';
import { shouldRedirectToLogin } from '../../../utilities/login';
import type { PageLoad } from './$types';

export const load: PageLoad = async ({ parent }) => {
const { user } = await parent();

if (shouldRedirectToLogin(user)) {
throw redirect(302, `${base}/login`);
}

return { user };
};
7 changes: 0 additions & 7 deletions src/routes/expansion/sets/new/+page.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import { base } from '$app/paths';
import { redirect } from '@sveltejs/kit';
import effects from '../../../../utilities/effects';
import { shouldRedirectToLogin } from '../../../../utilities/login';
import type { PageLoad } from './$types';

export const load: PageLoad = async ({ parent }) => {
const { user } = await parent();

if (shouldRedirectToLogin(user)) {
throw redirect(302, `${base}/login`);
}

const { plans: initialPlans } = await effects.getPlansAndModels(user);

return { initialPlans, user };
Expand Down
7 changes: 0 additions & 7 deletions src/routes/models/+page.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import { base } from '$app/paths';
import { redirect } from '@sveltejs/kit';
import effects from '../../utilities/effects';
import { shouldRedirectToLogin } from '../../utilities/login';
import type { PageLoad } from './$types';

export const load: PageLoad = async ({ parent }) => {
const { user } = await parent();

if (shouldRedirectToLogin(user)) {
throw redirect(302, `${base}/login`);
}

const initialModels = await effects.getModels(user);

return {
Expand Down
7 changes: 0 additions & 7 deletions src/routes/plans/+page.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import { base } from '$app/paths';
import { redirect } from '@sveltejs/kit';
import effects from '../../utilities/effects';
import { shouldRedirectToLogin } from '../../utilities/login';
import type { PageLoad } from './$types';

export const load: PageLoad = async ({ parent }) => {
const { user } = await parent();

if (shouldRedirectToLogin(user)) {
throw redirect(302, `${base}/login`);
}

const { models = [], plans = [] } = await effects.getPlansAndModels(user);

return {
Expand Down
5 changes: 0 additions & 5 deletions src/routes/plans/[id]/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@ import { redirect } from '@sveltejs/kit';
import { SearchParameters } from '../../../enums/searchParameters';
import effects from '../../../utilities/effects';
import { getSearchParameterNumber } from '../../../utilities/generic';
import { shouldRedirectToLogin } from '../../../utilities/login';
import type { PageLoad } from './$types';

export const load: PageLoad = async ({ parent, params, url }) => {
const { user } = await parent();

if (shouldRedirectToLogin(user)) {
throw redirect(302, `${base}/login`);
}

const { id } = params;
const planId = parseFloat(id);

Expand Down
5 changes: 0 additions & 5 deletions src/routes/plans/[id]/merge/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@ import type {
PlanMergeRequestSchema,
} from '../../../../types/plan';
import effects from '../../../../utilities/effects';
import { shouldRedirectToLogin } from '../../../../utilities/login';
import type { PageLoad } from './$types';

export const load: PageLoad = async ({ parent, params }) => {
const { user } = await parent();

if (shouldRedirectToLogin(user)) {
throw redirect(302, `${base}/login`);
}

const { id } = params;
const planId = parseFloat(id);

Expand Down
7 changes: 0 additions & 7 deletions src/routes/scheduling/+page.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import { base } from '$app/paths';
import { redirect } from '@sveltejs/kit';
import effects from '../../utilities/effects';
import { shouldRedirectToLogin } from '../../utilities/login';
import type { PageLoad } from './$types';

export const load: PageLoad = async ({ parent }) => {
const { user } = await parent();

if (shouldRedirectToLogin(user)) {
throw redirect(302, `${base}/login`);
}

const { models = [], plans = [] } = await effects.getPlansAndModelsForScheduling(user);

return {
Expand Down
5 changes: 0 additions & 5 deletions src/routes/scheduling/conditions/edit/[id]/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ import { base } from '$app/paths';
import { redirect } from '@sveltejs/kit';
import effects from '../../../../../utilities/effects';
import { parseFloatOrNull } from '../../../../../utilities/generic';
import { shouldRedirectToLogin } from '../../../../../utilities/login';
import type { PageLoad } from './$types';

export const load: PageLoad = async ({ parent, params }) => {
const { user } = await parent();

if (shouldRedirectToLogin(user)) {
throw redirect(302, `${base}/login`);
}

const { id: conditionIdParam } = params;
const { models = [], plans = [] } = await effects.getPlansAndModelsForScheduling(user);

Expand Down
Loading

0 comments on commit edde9ae

Please sign in to comment.