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

developer alamin's avatar

How can I use Laravel's socialite API from Vuejs?

How can I use Laravel's socialite API from Vuejs? . Any experites give me an idea. I have already create API and use it in laravel blade file . But now how to use in vuejs . My laravel and vueJs project run different port.

0 likes
4 replies
sididev's avatar

For more information check this link : https://laravel.com/docs/10.x/socialite

Here's a general idea of how to handle this:

Login Route in Vue.js: When the user clicks on the "Login with..." button, you'll want to make a request from Vue.js to a new endpoint in your Laravel API (let's call it /api/login/{provider}) where {provider} is the name of the Socialite driver (like 'google', 'facebook', etc).

Laravel Redirect to Provider: The /api/login/{provider} route in your Laravel API should handle redirecting to the OAuth provider's login page. This can be done using the Socialite's redirect method. This would look something like:

Route::get('login/{provider}', function ($provider) {
    return Socialite::driver($provider)->stateless()->redirect();
});

Provider Callback to Laravel: After successful login with the OAuth provider, they will redirect back to a callback URL you've specified in the provider's settings. This should be another endpoint in your Laravel API, e.g. /api/login/{provider}/callback.

Laravel Handle Provider User: Here you'll want to handle the incoming request, use Socialite to get the User from the provider, and then either find or create the User in your own database. This would look something like:

Route::get('login/{provider}/callback', function ($provider) {
    $providerUser = Socialite::driver($provider)->stateless()->user();
    $user = User::firstOrCreate(
        ['provider_id' => $providerUser->getId()],
        ['email' => $providerUser->getEmail(), 'name' => $providerUser->getName()]
    );

    // create a token for the user and return it
    $token = $user->createToken('Token Name')->accessToken;
    return response()->json(['token' => $token]);
});

Note that I've used the stateless() method on the Socialite driver because API requests are stateless.

I think it all

now for the view file eg : Login.vue u will get something linke that...

<template>
  <button @click="loginWithGoogle">Login with Google</button>
</template>

<script>
import { ref } from 'vue';
import axios from 'axios';

export default {
  setup() {
    const loginWithGoogle = async () => {
      try {
        const response = await axios.get('http://localhost:{laravel-port}/api/login/google');
        localStorage.setItem('token', response.data.token);
      } catch (error) {
        console.error(error);
      }
    };

    return {
      loginWithGoogle,
    };
  },
};
</script>

I hope this will help you ;p

martinbean's avatar

@developer alamin You don’t. Socialite is for authorising against OAuth providers. So you need to make actual redirects to the authorisation URLs. You need to do actual browser redirects; not making requests inside JavaScript or Vue.

Kylo's avatar

@martinbean As you said, I did actual redirect to authorization URLs and get the auth user from callback url. Then I do auth logic (store user, generate token cause I use vuejs as client side), response json of user info and token in the callback method. I want to know how to get that user info and token from client side? I'm beginner to implementing Oauth login.

Please or to participate in this conversation.