joelbutcher / socialstream

OAuth for Laravel, simplified.

Home Page:https://docs.socialstream.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Missing file "GenerateRedirectForProvider.php" / How to change redirect url after user creation

kokomomtl opened this issue · comments

commented

Hi, I am using version 3.8 of socialstream.

I need to add a Google analytics conversion tag on a page when a new user is registered. I was able to do it easily for regular user registration in jetstream, by adding a parameter in the url and displaying the tag conditionally on the the page, but I haven't been able to figure out how to make it work in socialstream.

In your documentation, you mention that there should be a file called GenerateRedirectForProvider.php in the actions folder, but it is not there in my installation. I'm assuming that is where I would add the parameter to the redirection.

I've tried updating the package to the latest version (4.0.1) and it did not add the missing file, also it broke my installation because of the renaming of the login components. Is there an upgrade guide somewhere for passing from version 3 to 4?

Thank-you for your help.

commented

Hi joel, thanks for fixing it in v4. Is there an upgrade guide somewhere?

commented

Thank-you for the info. Unfortunately it is missing the part about changing the x-jet references in the login and registration blades, which is what is currently breaking my install when i update

@kokomomtl this is covered by the Jetstream upgrade guides for 3.x

commented

You're right! I was able to make everything work properly again. Only thing i had to figure out myself is that I needed to change all the references to profile_photo_url to get_photo_url.

@kokomomtl that doesn't sound right – do you get an error when calling profile_photo_url? The User model has been updated to reflect the change made to the base HasProfilePhoto trait:

    use HasProfilePhoto {
        profilePhotoUrl as getPhotoUrl;
    }

You may want to check and update your user model accordingly. If this is missing from the upgrade guide, I'll add that

commented

Hi Joel,

indeed I was getting an error. I'm unsure if it was due to my particular setup, but changing it to get_photo_url solved the issue.

On an unrelated note, I haven't been able to figure out how to change the redirect url when a user signs up for the first time using a social login, do you have any pointers for me? I'd like to add a url parameter so I can conditionally load some scripts.

@kokomomtl profilePhotoUrl as getPhotoUrl >> getPhotoUrl == get_photo_url... Calling get_photo_url will be bypassing any of the OAuth provider avatar's you have pulled in for your users that Socialstream adds as functionality on top of jetstream. Instead, your code will just defer to the jetstream logic.

RE changing the redirect url. That should be done by adding the following logic to the boot method of SocialstreamServiceProvider, as shown here:

Socialstream::generatesProvidersRedirectsUsing(GenerateRedirectForProvider::class);

commented

@joelbutcher thanks for your answer.

For the profile url. In fact it's properly pulling the social avatar so I guess i'm all good.

Regarding the boot method, I already have that line in there. I'm unsure what to do with that? Isn't that simply for the oauth redirect and not for where the user is redirected when the account is created?

Sorry, i'm a bit new in Laravel, i've read pretty much everything I could about socialite, but all the solutions I found do not seem to work with Socialstream, as the socialite controllers are not published.

@kokomomtl

Just re-read your original comment about redirect URL's

when a user signs up for the first time

Are you wanting a separate user flow for users signing up for the first time versus login in again?

commented

Yes exactly. I need to track new signups for google analytics and google ads as conversions

You could fire an event that pings a web hook url for this. But I actually have a PR to allow users to override the authentication logic in the controller with #256

commented

Hi @joelbutcher , that would be great!

I've been spending a lot of time figuring this out. If you can get that update quickly and send me some detailed instructions on how to change the redirect url, with parameters (ie: /?first_sign_up=true), I will gladly send you a tip via Github's "sponsor" program to thank-you for your help.

@kokomomtl There's some documentation about the update here.

Customising the redirect URL is different, that's the URL that sends the user off to the providers OAuth flow and can be customise by changing the logic in the GenerateRedirectForProvider.php.

If what your after is to append a query string param onto the callback url like so:

https://my-app.dev/oauth/google/callback?initial_registration=true

Then I'm not sure how you'd do that. You'll probably want to extend the base action and add some logic to call a tracking service within register method, as this is called whenever a new users tries to authenticate with your application for the first time with a given provider / email combination:

protected function register(string $provider, ProviderUser $providerAccount): RedirectResponse|LoginResponse
{
if (! $providerAccount->getEmail()) {
$messageBag = new MessageBag;
$messageBag->add(
'socialstream',
__('No email address is associated with this :Provider account. Please try a different account.', ['provider' => $provider])
);
return redirect()->route('register')->withErrors($messageBag);
}
if (Jetstream::newUserModel()->where('email', $providerAccount->getEmail())->exists()) {
$messageBag = new MessageBag;
$messageBag->add(
'socialstream',
__('An account with that email address already exists. Please login to connect your :Provider account.', ['provider' => $provider])
);
return redirect()->route('register')->withErrors($messageBag);
}
$user = $this->createsUser->create($provider, $providerAccount);
return $this->login($user);
}

You'll then need to register this new action in your applications AppServiceProvider as per the docs:

<?phpnamespace App\Providersuse App\Actions\Socialstream\Auth\HandleOAuthCallback;
use Illuminate\Support\ServiceProvider;
​
class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        Socialstream::authenticatesOauthCallbackUsing(HandleOAuthCallback::class);
    }
}
commented

Hi @joelbutcher , I believe we are still not understanding each other. I want to change the url for the redirection after they register, and not the callback redirection. Basically, once the person is registered, they are currently being redirected to my "home" route. That is fine, but i'd like to add parameters. Or at least be able to redirect it to a different route, where I would have my tracking code.

If that's too complicated, i could deal with simply being able to change the route where the user is being redirected after social login, and then i could simply check if it's been more than a few minutes since that user was created or not.

@kokomomtl you should be able to just extend and replace the callback logic as discussed about for that 👍