Ad

How To Get Session Token After Successful Authentication?

- 1 answer

After successful authentication via a form post sign-in, I need to be able to use the same session token within the response to do another post to a protected route, but this time using XMLHttpRequest.

How would I get the session token, considering that the successful authentication response has already passed.

Ad

Answer

The session token is stored in a laravel_session cookie, assuming default Laravel settings (see config/session.php).

You can read the cookie in javascript using document.cookie. For example:

function readCookie(name)
{
    var matches = document.cookie.match('(^|; )'+name+'=([^;]*)');

    if (matches) {
        return decodeURIComponent(matches[2]);
    }

    return null;
}

var token = readCookie('laravel_session');
Ad
source: stackoverflow.com
Ad