Software Engineer experienced in building applications using Python, Rust (Web), Go, and JavaScript/TypeScript. I am actively looking for new job opportunities.
Sadly, there is no much resource online for that aside the example in Real World Svelte app. But I will try to explain it here. I will assume you are using TypeScript and that my user type is like:
In the load function, we ensure that a logged in user can't re-login. In the other script tag in the login component and immediately after a user successfully logs in, you can then set the user in the session:
...// Code that logs user in. After a successfully login attempt$session.user=responseFromTheBackend;...
Since session is a store shipped with SvelteKit, we used the auto-subscription sign, $ to access and set the user properties to the response gotten from the backend after the user signs in. Which means the backend must return the user's data as declared in:
This time, it doesn't go back to the backend to fetch user's data. Instead, it gets the user data stored in the session store and validates the requests made by the user. The session store can be imported like:
Can you show how or point to a resource?
Sadly, there is no much resource online for that aside the example in Real World Svelte app. But I will try to explain it here. I will assume you are using TypeScript and that my user type is like:
If this type declaration is in
src/lib/types/user.interface.ts
, I can then import it toapp.d.ts
and make theSession
typed as follows:Then in your login page, you first ensure that the user is not logged in by having this load function:
isEmpty
is a function that checks whether or not an object is empty. An implementation is:In the
load
function, we ensure that a logged in user can't re-login. In the other script tag in the login component and immediately after a user successfully logs in, you can then set the user in the session:Since
session
is a store shipped with SvelteKit, we used the auto-subscription sign,$
to access and set the user properties to the response gotten from the backend after the user signs in. Which means the backend must return the user's data as declared in:After setting this, on every protected page, you can have a load function such as:
This time, it doesn't go back to the backend to fetch user's data. Instead, it gets the user data stored in the
session
store and validates the requests made by the user. Thesession
store can be imported like:Hope this helps.
Yeah, thanks for taking the time. Though I don't write Typescript, but I can understand what you're trying to do.