Skip to content

Commit

Permalink
Improve app-bar and fix test and svelte-check
Browse files Browse the repository at this point in the history
  • Loading branch information
myieye committed Jan 24, 2025
1 parent 3fadf17 commit b40ab44
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion frontend/src/lib/layout/AppBar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
});
let isPlaywright = false;
let environmentName = env.PUBLIC_ENV_NAME?.toLowerCase();
export let user: LexAuthUser | undefined;
export let user: LexAuthUser | null;
$: loggedIn = !!user;
</script>

Expand Down
6 changes: 4 additions & 2 deletions frontend/src/lib/layout/Layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

<svelte:window on:keydown={closeOnEscape} />

{#if user && user.audience == 'LexboxApi'}
{#if user?.audience === 'LexboxApi'}
<div class="drawer drawer-end grow">
<input id="drawer-toggle" type="checkbox" bind:checked={menuToggle} class="drawer-toggle" />

Expand Down Expand Up @@ -94,7 +94,9 @@
</div>
</div>
{:else}
<AppBar user={undefined} />
<!-- If the user is authenticated (i.e. not null), but not authorized to use the API, we
still want to pass them in here, so we show their name rather than Login and Register buttons. -->
<AppBar {user} />

<Content>
<slot />
Expand Down
9 changes: 6 additions & 3 deletions frontend/src/lib/user.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {describe, expect, it} from 'vitest';
import {jwtToUser} from '$lib/user';

import type {AuthUserOrg} from '$lib/gql/generated/graphql';
import {jwtToUser} from '$lib/user';

describe('jwtToUser', () => {
it('should convert a jwt token to a LexAuthUser', () => {
const jwtUser = {
Expand All @@ -27,7 +29,7 @@ describe('jwtToUser', () => {
'exp': 1722673515,
'iat': 1721377515,
'iss': 'LexboxApi',
'aud': 'LexboxApi'
'aud': 'LexboxApi' as const,
};
const user = jwtToUser(jwtUser);
expect(user).toEqual({
Expand Down Expand Up @@ -66,7 +68,8 @@ describe('jwtToUser', () => {
'canCreateProjects': true,
'createdByAdmin': false,
'locale': 'en',
'emailOrUsername': '[email protected]'
'emailOrUsername': '[email protected]',
'audience': 'LexboxApi',
});
});
});
21 changes: 12 additions & 9 deletions frontend/src/lib/user.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { browser } from '$app/environment'
import { redirect, type Cookies } from '@sveltejs/kit'
import { jwtDecode } from 'jwt-decode'
import { deleteCookie, getCookie } from './util/cookies'
import {browser} from '$app/environment';
import {redirect, type Cookies} from '@sveltejs/kit';
import {jwtDecode} from 'jwt-decode';
import {deleteCookie, getCookie} from './util/cookies';
import {hash} from '$lib/util/hash';
import { ensureErrorIsTraced, errorSourceTag } from './otel'
import {ensureErrorIsTraced, errorSourceTag} from './otel';
import zxcvbn from 'zxcvbn';
import { type AuthUserProject, type AuthUserOrg, ProjectRole, UserRole, type CreateGuestUserByAdminInput, type OrgRole } from './gql/types';
import { _createGuestUserByAdmin } from '../routes/(authenticated)/admin/+page';
import {type AuthUserProject, type AuthUserOrg, ProjectRole, UserRole, type CreateGuestUserByAdminInput, type OrgRole, LexboxAudience as GqlLexboxAudience} from './gql/types';
import {_createGuestUserByAdmin} from '../routes/(authenticated)/admin/+page';

type LoginError = 'BadCredentials' | 'Locked';
type LoginResult = {
Expand All @@ -24,6 +24,8 @@ type RegisterResponseErrors = {
}
}

type ApiLexboxAudience = 'LexboxApi' | 'Unknown';

type JwtTokenUser = {
sub: string
name: string
Expand All @@ -36,7 +38,7 @@ type JwtTokenUser = {
unver?: boolean | undefined,
mkproj?: boolean | undefined,
creat?: boolean | undefined,
aud: string,
aud: ApiLexboxAudience,
loc: string,
}

Expand All @@ -54,7 +56,7 @@ export type LexAuthUser = {
emailVerified: boolean
canCreateProjects: boolean
createdByAdmin: boolean
audience: string
audience: ApiLexboxAudience
locale: string
}

Expand Down Expand Up @@ -156,6 +158,7 @@ export async function createGuestUserByAdmin(password: string, passwordStrength:
canCreateProjects: responseUser.canCreateProjects ?? false,
createdByAdmin: responseUser.createdByAdmin ?? false,
emailOrUsername: (responseUser.email ?? responseUser.username) as string,
audience: responseUser.audience === GqlLexboxAudience.LexboxApi ? 'LexboxApi' : 'Unknown',
}
return { user }
}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/routes/(authenticated)/admin/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ export async function _createGuestUserByAdmin(input: CreateGuestUserByAdminInput
orgId
role
}
audience
}
errors {
__typename
Expand Down

0 comments on commit b40ab44

Please sign in to comment.