Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

280
Visualizações
How to save JWT in sveltekit?

Being new to sveltekit, I have made a login form based on realworldapp, which receives JWT token upon successful login.

<script context="module">
    export async function load({ session }) {
        if (session.user) {
            return {
                status: 302,
                redirect: '/'
            };
        }
        return {};
    }
</script>

<script>
    import { session } from '$app/stores';
    import { goto } from '$app/navigation';
    import { post } from '$lib/utils.js';
    import { fade } from 'svelte/transition';
 
 
    let username = '';
    let password = '';
    let errors = null;
    let message = '';
    let hasError = false
    async function submit(event) {
        errors = response.errors;
        if (response.token) {
            $session.access_token = response.token.access_token;
            //how to save this token in a cookie?
            goto('/profile');
        } else {
            message= response.message;
            hasError = true
            setTimeout(() => {hasError=false}, 5000);            
        }
    }
</script>

        <form  on:submit|preventDefault={submit} >
            <h3 class="form-title">Login</h3>
            {#if hasError}
                <div  transition:fade  class="err-msg">{message}</div>
            {/if} 
                <input type="text" bind:value={username}  placeholder="username">
                <input type="password" bind:value={password} placeholder="password">
              
                <input type="submit"  value="send">
        </form>  

It works fine and I can get the token in a response like:

{
message: "successully logged in!"
token: {access_token: 'eyJhbGciOiJIUzI1....'
}

Now, I'm wondering what is the safe and idomatic way to save the token to a cookie and retrive it, so that usr does not need toauthenticate each time that opens the browser?

I looket at the official docs but could not find any hints about it. Nor could I find a working example in a tutorial about it. Hence the quesiton.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your core concern is "how do I save a value in a cookie, then retrieve it later?"

Cookies are stored in the browser as a string that requires parsing to read or update. There are many libraries in JavaScript to help you do this, one popular one being js-cookie. While you don't need a library, you may find it more convenient than parsing the string yourself.

A basic example of how to write and read a cookie is:

import Cookies from 'js-cookie'


Cookies.set('name', 'value');

Cookies.get('name') // => 'value'

There are more options you can set too, like the path, the expiry, etc.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda