Skip to content

Commit

Permalink
fix(console): move nhost util functions into nhost store class
Browse files Browse the repository at this point in the history
  • Loading branch information
xmlking committed Jul 5, 2024
1 parent 890622a commit 8455439
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 218 deletions.
66 changes: 0 additions & 66 deletions apps/console/src/lib/nhost.ts

This file was deleted.

16 changes: 13 additions & 3 deletions apps/console/src/lib/stores/nhost.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { browser } from '$app/environment';
import { env } from '$env/dynamic/public';
import { PUBLIC_NHOST_REGION, PUBLIC_NHOST_SUBDOMAIN } from '$env/static/public';
import { extractSession, getClientSession, setClientSession } from '$houdini';
import { SearchSecurityKeysStore, extractSession, getClientSession, setClientSession } from '$houdini';
import { NHOST_SESSION_KEY } from '$lib/constants';
import { hasSecurityKey } from '$lib/nhost';
import { NhostClient, type NhostClientConstructorParams } from '@nhost/nhost-js';
import type { User } from '@nhost/nhost-js';
import { Logger } from '@spectacular/utils';
import Cookies from 'js-cookie';
import { getContext, onDestroy, setContext } from 'svelte';
import { type Readable, type Writable, derived, get, readable, readonly, writable } from 'svelte/store';

const skQuery = new SearchSecurityKeysStore().artifact.raw;
export class SvelteKitNhostClient extends NhostClient {
#log = new Logger('auth.store.client');

Expand Down Expand Up @@ -110,12 +110,22 @@ export class SvelteKitNhostClient extends NhostClient {
async elevate() {
const $elevated = get(this.elevated);
const $user = get(this.#user);
if (!$elevated && $user?.id && (await hasSecurityKey($user.id))) {
if (!$elevated && $user?.id && (await this.hasSecurityKey())) {
const { error } = await this.auth.elevateEmailSecurityKey($user?.email as string);
if (error) return error;
}
return null;
}

async hasSecurityKey() {
const userId = get(this.#user)?.id;
const { data, error } = await this.graphql.request(skQuery, { userId });
if (error) {
this.#log.error({ error });
return false;
}
return data?.authUserSecurityKeys.length > 0;
}
}

// this is important if u are gonna have any SSR
Expand Down
99 changes: 0 additions & 99 deletions apps/console/src/lib/stores/user.ts

This file was deleted.

2 changes: 0 additions & 2 deletions apps/console/src/routes/(app)/organizations/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import { invalidateAll } from '$app/navigation';
import { DeleteOrgStore } from '$houdini';
import { default as ChipButton } from '$lib/components/chip-button.svelte';
import { handleMessage } from '$lib/components/layout/toast-manager';
import { isAuthenticated, user } from '$lib/stores/user';
import { getToastStore, popup } from '@skeletonlabs/skeleton';
import * as Table from '@spectacular/skeleton/components/table';
import { DataHandler } from '@vincjo/datatables';
import { formatDistance } from 'date-fns';
import type { PageData } from './$houdini';
export let data: PageData;
$: ({ OrganizationsList } = data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export let initialData: ChangeEmail;
const log = new Logger('profile:password:browser');
const toastStore = getToastStore();
const loadingState = getLoadingState();
const { nhost } = getNhostClient();
const nhost = getNhostClient();
const form = superForm(defaults(initialData, zod(changeEmailSchema)), {
SPA: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { zod, zodClient } from 'sveltekit-superforms/adapters';
const log = new Logger('profile:password:browser');
const toastStore = getToastStore();
const loadingState = getLoadingState();
const { nhost } = getNhostClient();
const nhost = getNhostClient();
const form = superForm(defaults(zod(changePasswordSchema)), {
SPA: true,
Expand Down
34 changes: 0 additions & 34 deletions apps/console/src/routes/(app)/profile/components/elevate.svelte

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
import { invalidate } from '$app/navigation';
import * as m from '$i18n/messages';
import { handleMessage } from '$lib/components/layout/toast-manager';
import { nhost } from '$lib/stores/user';
import { elevate, elevated } from '$lib/stores/user';
import { AppBar, getToastStore } from '@skeletonlabs/skeleton';
import { Alerts } from '@spectacular/skeleton/components/form';
import { UserRound } from 'lucide-svelte';
import SuperDebug from 'sveltekit-superforms';
import { getNhostClient } from '$lib/stores/nhost';
// Variables
let claims = nhost.auth.getHasuraClaims();
let message: App.Superforms.Message | undefined;
const errors: string[] = [];
const toastStore = getToastStore();
const nhost = getNhostClient();
const { elevated } = nhost;
let claims = nhost.auth.getHasuraClaims();
// Functions
async function handleRefresh() {
Expand All @@ -23,7 +24,7 @@ async function handleRefresh() {
}
async function handleElevate() {
const error = await elevate();
const error = await nhost.elevate();
if (error) {
errors.push(error.message);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ const log = new Logger('profile:keys:browser');
const toastStore = getToastStore();
const loadingState = getLoadingState();
const nhost = getNhostClient();
const { elevate } = nhost;
const form = superForm(defaults(zod(webAuthnSchema)), {
SPA: true,
dataType: 'json',
Expand All @@ -34,7 +32,7 @@ const form = superForm(defaults(zod(webAuthnSchema)), {
async onUpdate({ form, cancel }) {
if (!form.valid) return;
// First, check if elevate is required
const error = await elevate();
const error = await nhost.elevate();
if (error) {
log.error('Error elevating user', { error });
setError(form, '', error.message, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import { GraphQLError } from 'graphql';
import { KeyRound, Trash } from 'lucide-svelte';
import { fade } from 'svelte/transition';
export let message: App.Superforms.Message | undefined;
export let errors: string[];
// Variables
const log = new Logger('auth:profile:skey:browser');
const toastStore = getToastStore();
const nhost = getNhostClient();
export let message: App.Superforms.Message | undefined;
export let errors: string[];
export let securityKey: SecurityKeyFragment;
$: securityKeyFragment = fragment(
Expand All @@ -39,10 +42,8 @@ const deleteSecurityKey = new RemoveSecurityKeyStore();
const handleDelete = async () => {
// before
isDeleting = true;
// check if elevate is needed
const { elevate } = getNhostClient();
const error = await elevate();
const error = await nhost.elevate();
if (error) {
errors.push(error.message);
handleMessage(
Expand Down

0 comments on commit 8455439

Please sign in to comment.