DutchCodingCompany / filament-socialite

Add OAuth login through Laravel Socialite to Filament.
MIT License
141 stars 36 forks source link

UserObserver didn't get called in AppServiceProvider when there is FilamentSocialiteFacade::setCreateUserCallback #94

Closed fbunadi closed 2 months ago

fbunadi commented 2 months ago

Hi, please bear with me. I'm still new to Laravel and Filament. I have a few questions regarding this plugin.

  1. UserObserver didn't get triggered when a new user is created

    If I removed the below code from AppServiceProvider, User::observe(UserObserver::class) gets called when a new user is created in the user table. However it didn't get triggered when I have the below code.

FilamentSocialiteFacade::setCreateUserCallback(fn (string $provider, SocialiteUserContract $oauthUser, FilamentSocialite $socialite) => $socialite->getUserModelClass()::create([
            'name' => $oauthUser->getName(),
            'email' => $oauthUser->getEmail(),
            'password' => Hash::make(Str::random(7)),
        ]));

Here's my AppServiceProvider.php

Event::listen(function (\SocialiteProviders\Manager\SocialiteWasCalled $event) {
            $event->extendSocialite('okta', \SocialiteProviders\Okta\Provider::class);
        });

        FilamentSocialiteFacade::setCreateUserCallback(fn (string $provider, SocialiteUserContract $oauthUser, FilamentSocialite $socialite) => $socialite->getUserModelClass()::create([
            'name' => $oauthUser->getName(),
            'email' => $oauthUser->getEmail(),
            'password' => Hash::make(Str::random(7)),
        ]));

        User::observe(UserObserver::class);
  1. How do I get Socialite token so I can pass it to FilamentSocialiteFacade::setCreateUserCallback?
bramr94 commented 2 months ago
  1. UserObserver didn't get triggered when a new user is created

Which method do you want to trigger?

  1. How do I get Socialite token so I can pass it to FilamentSocialiteFacade::setCreateUserCallback?

The token should be in the $oathUser.

fbunadi commented 2 months ago

Hi Bram,

I created a job which need to be executed when there is a new user is created.

Here's my UserObserver.php

namespace App\Observers;

use App\Models\User;
use Illuminate\Support\Facades\Log;
use App\Jobs\StoreUserEnrolledCourses;

class UserObserver
{
    /**
     * Handle the User "created" event.
     */
    public function created(User $user): void
    {

        Log::debug('HELLO THERE');
        StoreUserEnrolledCourses::dispatch($user);
    }

    /**
     * Handle the User "updated" event.
     */
    public function updated(User $user): void
    {
        //
    }

    /**
     * Handle the User "deleted" event.
     */
    public function deleted(User $user): void
    {
        //
    }

    /**
     * Handle the User "restored" event.
     */
    public function restored(User $user): void
    {
        //
    }

    /**
     * Handle the User "force deleted" event.
     */
    public function forceDeleted(User $user): void
    {
        //
    }
}
fbunadi commented 2 months ago

Hi Bram,

Nevermind, it works now. The UserObserver gets executed after a user is created. Thank you so much for your help.

niekbr commented 2 months ago

If you want it to execute just before creation, you can use the creating function on the observer instead of created