#Laravel + MongoDB + Sociallite

46 messages · Page 1 of 1 (latest)

open mirage
#

what is your query

latent thorn
#

This is my controller logic

<?php

namespace App\Http\Controllers\Auth;

use App\Models\User;
use App\Http\Controllers\Controller;
use Laravel\Socialite\Facades\Socialite;

class Discord extends Controller
{
    /**
     * Redirect the user to the Discord authentication page.
     *
     * @return \Illuminate\Http\Response
     */
    public function redirectToProvider()
    {
        return Socialite::driver('discord')
            ->setScopes(['identify', 'email', 'guilds'])
            ->redirect();
    }

    /**
     * Obtain the user information from Discord.
     *
     * @return \Illuminate\Http\Response
     */
    public function handleProviderCallback()
    {
        $discordUser = Socialite::driver('discord')->user();
        //dd($discordUser);

        
        $discordUser = User::create([
            'discord_id' => $discordUser->id,
        ], [
            'name' => $discordUser->name,
            'email' => $discordUser->email,
            'discord_token' => $discordUser->token,
            'discord_refresh_token' => $discordUser->refreshToken,
        ]);
        
        dd($discordUser);

        return redirect('/dashboard');
    }
}
#

my model:

<?php

namespace App\Models;

use Laravel\Sanctum\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use MongoDB\Laravel\Eloquent\Model;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    protected $connection = 'mongodb';

    protected $collection = 'users';

    protected $fillable = [
        'discord_id', 'name', 'email',
    ];

}
latent thorn
#

ok now the model save the data in mongoose .. but the next thing is .. we can i auth the user LARAVEL global?

latent thorn
#
$discordUser = Socialite::driver('discord')->user();
        $user = User::create([
            '_id' => $discordUser->id,
            'discord_id' => $discordUser->id,
            'name' => $discordUser->name,
            'email' => $discordUser->email,
            'discord_token' => $discordUser->token,
            'discord_refresh_token' => $discordUser->refreshToken,
        ]);
        //dd($discordUser);
        Auth::login($user, true);
        return redirect()->route('backend.dashboard')
            ->with('success', __('Login was successfully'));
#

This is my handleProviderCallback but the auth says

#
Illuminate\Auth\SessionGuard::login(): Argument #1 ($user) must be of type Illuminate\Contracts\Auth\Authenticatable, App\Models\User given, called in /home/users/evariooo/www/smartli.evarioo.de/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php on line 340
#

I know the mistake and in my opinion I did the right thing but it doesn't help..

open mirage
meager sequoia
latent thorn
#

This is the way from official docu …

meager sequoia
#

It isn't tho?

#

You're extending the incorrect class

latent thorn
#

I followed laravel docu for sociallite ...

meager sequoia
#

Socialite is just oauth, you'd then need to create a user in your db, authenticate them etc.

latent thorn
#

Serialization of 'Closure' is not allowed

#

after implementin the mongo db auth

meager sequoia
#

And I just mentioned your User is incorrect, hence you're seeing the authenticatable error 🤷‍♂️

latent thorn
#

my user and login are correct .. the aut is working perfect !

#

The problem is that I also have to / want to save the user so that I can use it via the normal features of Laravel such as Auth::user()->email or similar

meager sequoia
#

So, it's solved then?

latent thorn
#

no!

#

?!

#
public function handleGoogleCallback()
    {
        $googleUser = Socialite::driver('google')->stateless()->user();
        $user = User::where('email', $googleUser->email)->first();
        if(!$user)
        {
            $user = User::create(['name' => $googleUser->name, 'email' => $googleUser->email, 'password' => \Hash::make(rand(100000,999999))]);
        }

        Auth::login($user);

        return redirect(RouteServiceProvider::HOME);
    }
#

I need this .. somethin like this ..

meager sequoia
#

Ok, then what's the issue..?

#

You're being really vague here

#

Like, did you actually solve the issue you were mentioning, did you read the docs I sent, did you apply those changes?

#

And if so, what's not working? Because that would make it possible to use a MongoDB model for authentication 🤷‍♂️

latent thorn
#

I've already explained several times what the problem is. So: The login works great! The user is also stored in the mongodb. BUT: I need an auth via Laravel for the user so that I can retrieve it globally using {{ Auth::user()->name }} as an example..

#

my updatet model:

<?php

namespace App\Models;

use MongoDB\Laravel\Eloquent\Model;
use MongoDB\Laravel\Auth\User as Authenticatable;

class User extends Authenticatable
{
    protected $connection = 'mongodb';

    protected $collection = 'users';

    protected $fillable = [
        'discord_id', 'name', 'email', 'discord_token', 'discord_refresh_token',
    ];
}
#

after this changes another error is comming to me:

#

Serialization of 'Closure' is not allowed

meager sequoia
#

Yeah, we can't help with that, dunno where the error comes from, dunno what triggers it. Read the error logs, follow the stack trace

latent thorn
#

Seriously? ..

#

I'm speechless but no problem. Thanks.

meager sequoia
#

How would you expect people to know what causes that error? It's a really generic error. It's like saying "I have a 500 response", could be caused by anything. What you're sharing only tells us a closure is being serialized, not where, not what etc

latent thorn
#

i hvae no 500!

#

you cant read!

meager sequoia
#

🤦‍♂️

latent thorn
#

thats not my problem .. thats yours and for now im done here

#

error is fixd

#

have a nice