In this section, you learn about session management in your client application. There are several ways to manage sessions in single-page web applications. The most common one is to manage sessions through browser cookies. In this section, you learn how to manage sessions with browser cookies and Svelte store.
The js-cookie
npm package is the easiest way to work with cookies. Install this package in your project with the following command.
$ npm i js-cookie
When a user logs in, use the js-cookie library to store the login response in the browser cookies. Make the following changes to your src/routes/login.svelte
file.
<script>
import { setClient, mutation } from '@urql/svelte';
import Cookies from 'js-cookie';
import client from '../client';
import { goto } from '$app/navigation';
setClient(client);
let error;
const loginMutation = mutation({
query: `
mutation OwnerLogin($email: String!, $password: String!) {
login(email: $email, password: $password) {
secret
ttl
email
ownerId
}
}
`,
});
async function onSubmit(e) {
const formData = new FormData(e.target);
const data = {};
for (let field of formData) {
const [key, value] = field;
data[key] = value;
}
const { email, password } = data;
const resp = await loginMutation({ email, password })
if(resp.data?.login) {
alert('Login Successful');
Cookies.set(
'fauna-session',
JSON.stringify(resp.data.login),
{ expires: new Date(resp.data.login.ttl) }
)
goto('/')
}
if(resp.error) {
error = resp.error?.message;
}
}
</script>
<div class="wrap">
<div class="uk-card uk-card-default uk-card-body">
<h3 class="uk-card-title">Login</h3>
{#if error}
<div class="uk-alert-danger" uk-alert style={{ maxWidth: '300px', padding: '10px'}}>
{error}
</div>
{/if}
<form on:submit|preventDefault={onSubmit} >
<div class="uk-margin">
<input
class="uk-input"
type="text"
placeholder="Email"
name="email"
/>
</div>
<div class="uk-margin">
<input
class="uk-input"
type="password"
placeholder="Password"
name="password"
/>
</div>
<div class="uk-margin">
<input class="uk-input" type="submit" />
</div>
</form>
</div>
</div>
<style>
.wrap {
margin: 10% 40%;
min-width: 300px;
}
</style>
Return to localhost:3000/login and sign in with a user you have registered. Inspect your browser cookies and notice that your application creates a cookie named fauna-session.
In the next section, you learn how to retrieve the user access token from your cookies and make GraphQL queries and mutations using it.
📙 Get the final code for this section here