Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pn523's avatar
Level 2

remember_token for laravel api

I am building api and when I register user through frontend using jquery ajax, how do I generate remember_me token in frontend ? (I didn't run any scaffolding for auth).

0 likes
2 replies
Nakov's avatar

There is no remember me option for token based authentication, you need to provide the token each time you are trying to access an authenticated endpoint.

fylzero's avatar

@pn If you are using Axios... you can write an interceptor that re-logs someone in based on their session.

I do this... basically if an axios call fails auth... it reloads the actual laravel page, literally refreshes the browser window. That will re-generate the laravel session token and automagically log them back in.

// Refresh Laravel Session for Axios
window.axios.interceptors.response.use(
    response => {
        // Call was successful, don't do anything special.
        return response;
    },
    error => {
        if (error.response.status === 401) {

            // Reload the page to refresh the laravel_token cookie.
            window.location.reload();
        }

        // If the error is not related to being Unauthorized, reject the promise.
        return Promise.reject(error);
    }
);

This can be taken a lot further... but I'm fine with this approach since remember tokens are technically forever under the hood. It's like 1 or 5 years and refreshes everytime something is loaded.

This approached solved remember me for my SPA app.

If you want to use a remember me checkbox specifically... you could write that but it wouldn't be real anyway... since ajax login basically auths every call via headers... devices are just assumed logged in until logged out.

Look into Laravel Airlock. I know Taylor Otwell is putting together a solid answer to SPA login with this upcoming package...

https://github.com/laravel/airlock

24 likes

Please or to participate in this conversation.