#Laravel + MongoDB + Sociallite
46 messages · Page 1 of 1 (latest)
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',
];
}
ok now the model save the data in mongoose .. but the next thing is .. we can i auth the user LARAVEL global?
$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..
here from where you import the User class ?
Don't think this would be correct, you're extending the Laravel User model, which is an Eloquent model, so not a proper MongoDB model. You'd want to extend the MongoDB Eloquent model and then implement the authenticatable interface, as the error is kinda suggesting you to do.
This is the way from official docu …
Socialite is just oauth, you'd then need to create a user in your db, authenticate them etc.
but i need the data in my db 😉 and the data were created .. the problem is the Auth::user()
Serialization of 'Closure' is not allowed
after implementin the mongo db auth
And I just mentioned your User is incorrect, hence you're seeing the authenticatable error 🤷♂️
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
So, it's solved then?
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 ..
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 🤷♂️
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
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
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
🤦♂️