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

589
Visualizações
php jwt login storage

I'm building a web app with CodeIgniter 4 where I implemented a REST API for register and login and a profile page just a test my login.

I have a login form that sends a javascript fetch post request to my api and I receives a jwt token. This is working.

Now I am in the situation where I think I did not understand the principle. I want that the user stays logged in and doesn't need to re-login every time. The token expires after 12h.

And I want to use php (if possible) as the entire app runs on php.

Currently, I have a little javascript function to store my token:

const store = {};
store.setJWT = (data) => {
    this.JWT = data;
};

But this is not secure against page reload.

Additionally I am creating a cookie with php, when the user logs in:

helper('cookie');
set_cookie([
    'name' => 'login',
    'value' => $token,
    'expire' => $exp,
    'httponly' => true,
    'secure' => true,
]);

I am able to fetch data from the API using the cookie or the store object.


const token = getCookie('login');

const res = await fetch('/profile', {
    headers: {
        'Authorization': `Bearer ${token}` // or store.JWT
    }
});

So.... what I want is: The user goes to a protected url e.g. https://myapp.com/profile and if he is logged in, he has access. If not, he gets redirect to the login page.

Is using the cookie to store the jwt a good idea? Or did I completely misunderstood the idea of JWT and it is not used to be used for a login?

Additionally: I still don't know if biulding the login as an API was the best idea.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

First of all there is nothing wrong with building "login with API". It is common practice. And JWT is perfectly suited for auth.

You sure can store JWT token inside a cookie, but it is a little bit wrong in my opinion. Usually JWT tokens are stored in the local storage on the client side. It will persist after page reload.

Set token in the local storage:

localStorage.setItem('token', token);

Get token from the local storage:

token = localStorage.getItem('token');

To better understand the conception of the JWT you can copy some token (without Bearer) and paste it in jwt.io. Basically JWT contain all the required information about the user and server can trust this information.

And when you set the cookie like this

set_cookie([
    'name' => 'login',
    'value' => $token,
    'expire' => $exp,
    'httponly' => true,
    'secure' => true,
]);

It is an overkill. JWT possible already contains all this information and you can extract it from the token.

over 4 years ago · Santiago Trujillo 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