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

Lars-Janssen's avatar

Laravel & Vue.js session

Hi,

I use Laravel 5.3 and vue.js 2.0. So for this application I would like to use sessions instead of jwt tokens. So when for example a user signs in I would like to make an ajax call with vue.js. Is it possible to create a session then? And if the credentials are false showing error messages but without refreshing the entire page?

And for example if someone likes a post I would like to handle that with an ajax call aswel. But how would I do that if I only have a session. Do I have to use jwt tokens in that case?

Bit confused about this.

Thanks!

0 likes
6 replies
Gog0's avatar

With the default configuration you would have your session cookie when you open your website and it would be sent with each request to the server, including your ajax requests, so you should be able to do what you want there.

if you want to show your error messages without reloading the page, just return a json response with the error in your authentication controller and show it in your ajax callback.

If your user likes a post, his session cookie will be sent with the ajax request so you will be able to know who it is. There is no necessity here to use jwt in your scenario. Did you face any troubles in your application with sessions?

Lars-Janssen's avatar

@ejdelmonico @Gog0 thanks for answering me.

So with the default installation of Laravel with authentication (php artisan make:auth)

I can make a post request to /api/login and it will create a session if the credentials are right?

Or is this wrong?

Gog0's avatar
Gog0
Best Answer
Level 2

@lars6 Your laravel app would start a session as soon as your user is opening one of your webpage. You can check with the developer tools in your browser, you'll get a cookie called "laravel_session" by default containing your PHP session ID.

The authentication has nothing to do with starting the session. It will just store in the user's already existing session that he is now loged in.

Also even if what's provided witth make:auth is a good place to start, I suppose you would have some customisation work to login using ajax as you won't want to load another page but just a "success" information to use in your vue component.

1 like
Lars-Janssen's avatar

@Gog0

How would I create the session if I do it for example like this (with an ajax request):

public function login(Request $request)
    {
        $auth = false;
        $credentials = $request->only('email', 'password');

        if (Auth::attempt($credentials, $request->has('remember'))) {
             $auth = true;
        }

        if ($request->ajax() && $auth) {
            return response()->json([
                'auth' => 'success'
            ]);
        }

        return response()->json([
            'auth' => 'false'
        ], 401);
    }
Gog0's avatar

What do you mean? As I told you the user get a session as soon as he is viewing the website, so even before trying to login. It's all done for you, you don't have to do anything manualy. So here if your authentication attempt is ok it will be stored in the user session which already exists. Nothing to do yourself here.

What's your real question here ^^ ? I'm not sure to understand your problem.

Please or to participate in this conversation.