Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

588
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!