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

Reysa's avatar
Level 1

OAuth login with Laravel 10 and VueJS 3

Hello I'm super new to Vue and Laravel api so my question might be dumb and easy :D

as I stated in title I want to use OAuth login for discord (https://discord.com/developers/docs/topics/oauth2#oauth2) for my website in previouse version I was using blade instead of Vue so I was using socialite and it was easy but now that I'm suing Vue I have no idea how can I generate and redirect user to discord login page and then get data in callback

Would be nice if you explain super simple for me :D thanks in advance <3

1 like
3 replies
LaryAI's avatar
Level 58

To implement OAuth login with Discord in Laravel 8 and VueJS 3, you can follow these steps:

  1. Install the Socialite package using composer:
composer require laravel/socialite
  1. Create a Discord app and get the client ID and secret from the Discord developer portal.

  2. Add the following environment variables to your .env file:

DISCORD_CLIENT_ID=your_client_id
DISCORD_CLIENT_SECRET=your_client_secret
DISCORD_REDIRECT_URI=http://localhost:8000/auth/discord/callback
  1. Create a route for the Discord login and callback:
Route::get('/auth/discord', function () {
    return Socialite::driver('discord')->redirect();
});

Route::get('/auth/discord/callback', function () {
    $user = Socialite::driver('discord')->user();
    // Handle the user data
});
  1. In your Vue component, create a button that redirects the user to the Discord login page:
<template>
  <button @click="loginWithDiscord">Login with Discord</button>
</template>

<script>
import axios from 'axios';

export default {
  methods: {
    loginWithDiscord() {
      axios.get('/auth/discord').then(response => {
        window.location.href = response.data.redirect;
      });
    }
  }
}
</script>
  1. In the Laravel callback route, handle the user data and redirect the user to the appropriate page:
Route::get('/auth/discord/callback', function () {
    $user = Socialite::driver('discord')->user();
    // Handle the user data
    return redirect('/dashboard');
});

That's it! You should now be able to implement OAuth login with Discord in your Laravel 8 and VueJS 3 application.

2 likes
Reysa's avatar
Level 1

@LaryAI well , never thought one day I have to thank an AI but that was the answer hahaha but now there is another problem I'm getting this error:

Acess to XMLHttpRequest at 'https://discord.com/api/oauth2/authorize?client_id=34234&redirect_uri=http%3A%2F%2Flocalhost%3A8000%2Foauth%2Fcallback&scope=identify+email&response_type=code&state=Sadasdaa21=none' (redirected from 'http://localhost:8000/auth') from origin 'http://localhost:8000' has been blocked by CORS policy: Request header field x-xsrf-token is not allowed by Access-Control-Allow-Headers in preflight response.

and have no idea to fix that

1 like
Reysa's avatar
Reysa
OP
Best Answer
Level 1

I managed to fix the problem by changing the "auth/discord" function to this:

return Socialite::with('discord')->redirect()->getTargetUrl();

the reason of the problem was without "getTargetUrl()" it was returing html codes instead of actual url

1 like

Please or to participate in this conversation.