403 error svelte store

Hey, I am trying to implement stores instead of global variable for the authentification
and there is an error 403 I can’t figure out …

it is an error but that I don’t get all the time, somethimes I reload the page and it works. the problem is that the call to be the backend is done before authentifaction is fully established, I guess because it is a signature error.
Screenshot from 2023-11-03 23-38-51

My logic is that in my main layout.ts I check is the user is authenicated with this function:

function update_dS_State() {
	const identity = authClient?.getIdentity();
	const principal = identity?.getPrincipal();
	dS.update((value) => ({
		...value,
		identity: identity,
		principal: principal,
		backend: createActor(backendCanisterId, { agentOptions: { host, identity } })
	}));
}

export async function syncAuth() {
	authClient = await AuthClient.create({
		idleOptions: {
			idleTimeout: 1000 * 60 * 60 * 24 * 30,
			disableDefaultIdleCallback: true
		}
	});
	if (await authClient.isAuthenticated()) {
		update_dS_State();
	}
}

Then there is this: function to gather the users (denizens)

let denizens:any = [];

	const fetchDenizens = async () => {
        if ($dS.backend) {
            try {
                denizens = await $dS.backend.get_all_denizens();
                console.log('Fetched denizens:', denizens);
            } catch (error) {
                console.error('Error fetching denizens:', error);
            }
        }
    };

	
    $: if ($dS.backend) {
		fetchDenizens();
	}

and this is how my dS store is declared:

export const dS = writable<Session>({
	backend: null,
	principal: null,
	denizen: null,
	identity: null
});

I am almost sure the error is because I am not handling stores properly …
Any idea on how to solve this ? Thanks

It’s a bit hard to tell without reading the all code. Is your project open source?

Alternatively, here is what I generally do:

  1. I create a store that contains signIn, signOut and sync

https://github.com/junobuild/juno/blob/main/src/frontend/src/lib/stores/auth.store.ts

  1. In the root +layout.svelte I implement an init function that calls the above store sync feature and when ready renders the app with a slot

<script>
 const init = async () => await authStore.sync()
</script>

{#await init()}
	<!-- here a spinner of animation -->
{:then _}
	<slot />
{/await}

https://github.com/junobuild/juno/blob/ce18c9b7d06492aa9e79852bfdee16ec6a978980/src/frontend/src/routes/%2Blayout.svelte#L65

From there, I know the authentication store is initialized everywhere in the app and I can safely query $authStore.identity to get to know the status of the identity.

2 Likes

I tried to add the logic you mentionned in the +layout.svelte,
so now it is in the layout.ts and in the +layout.svelte but still the same error.

I pushed the test to a repo:

I am stuying and some part in my test are inspired of what you do in your repos.

Thanks,

Elie
PS: (in case the dockerfile and docker-compose.yml were test that I tried but it is not working properly …)

I cannot install your repo so hard to tell.

❯ npm i
npm ERR! code ERESOLVE
npm ERR! ERESOLVE could not resolve
npm ERR!
npm ERR! While resolving: @nfid/embed@0.10.0-alpha.1
npm ERR! Found: tslib@2.6.2
npm ERR! node_modules/tslib
npm ERR! dev tslib@“^2.3.1” from the root project
npm ERR! tslib@“^2.1.0” from rxjs@7.5.7
npm ERR! node_modules/rxjs
npm ERR! rxjs@“7.5.7” from @nfid/embed@0.10.0-alpha.1
npm ERR! node_modules/@nfid/embed
npm ERR! @nfid/embed@“^0.10.0-alpha.1” from the root project
npm ERR! 1 more (@peculiar/webcrypto)
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer tslib@“2.4.0” from @nfid/embed@0.10.0-alpha.1
npm ERR! node_modules/@nfid/embed
npm ERR! @nfid/embed@“^0.10.0-alpha.1” from the root project
npm ERR!
npm ERR! Conflicting peer dependency: tslib@2.4.0
npm ERR! node_modules/tslib
npm ERR! peer tslib@“2.4.0” from @nfid/embed@0.10.0-alpha.1
npm ERR! node_modules/@nfid/embed
npm ERR! @nfid/embed@“^0.10.0-alpha.1” from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR!
npm ERR!
npm ERR! For a full report see:
npm ERR! /Users/daviddalbusco/.npm/_logs/2023-11-04T16_55_51_634Z-eresolve-report.txt

npm ERR! A complete log of this run can be found in:
npm ERR! /Users/daviddalbusco/.npm/_logs/2023-11-04T16_55_51_634Z-debug-0.log

I would suggest to remove code and then add it brick by brick until you figure out what’s the issue.

I would begin by removing the +layout.ts.

I would also try to call syncAuth() only once for the all app otherwise you might face a race condition.

Hope that helps.

Ah I managed to instal the dependency, I had to delete your package-lock.json which seems not portable.

I finally managed to boot your app locally but, cannot reproduce your issue :man_shrugging:

1 Like

Side note, though unrelated to the issue at hand, in my opinion, your following store structure raises a bad practice. I would advise against using a store that separates a principal and an identity into two distinct fields. This separation has the potential to cause desynchronization and result in invalid data. To some extent, it can lead to situations where a principal is not correctly associated with its corresponding identity while it is suppose to reflect the single authentication of the user.

export const dS = writable<Session>({
	backend: null,
	principal: null,
	denizen: null,
	identity: null
});

Instead I would suggest to hold only the identity and if you need an easy way to access the principal, use a derived store.

Just as side note if interesting.

1 Like

Ok thanks for trying, as I said it is not happening all the time, have you try to refresh several time ?
And thanks for the good practice, I’ll remove the principal then.

Thanks again for support,

Elie

I did, I tried to refresh the browser window several time but, the error did not showed :man_shrugging:

1 Like