1

I'm using OAuth for login in my Laravel Controller. Its working fine but the thing is when the user is registered for the first time, I wanna trigger the HTML 5 geolocation API to fetch the user's current location and do some mixpanel stuff. Earlier I was using AJAX in the JS for the login so there was no such problem but now that I've implemented a complete server side solution, I'm stuck with this one problem.

The Laravel Controller code looks something like this :

       function callback(){
          \\ fetch the access token and graph data
          if($res = \Auth::mjAuthenticate('facebook', $fbData)){                    
                $user = \Auth::scope()->getUser();                          

                return \Redirect::to('events'); 
            }
            if (\Auth::mjRegister('facebook', $fbData)) {

                $user = \Auth::scope()->getUser();                                      

                return \Redirect::to('events');
            }

            return $this->handleFailure('Some Problem Occured');
        }

The Earlier JS Code was :

             ajax
                .post('auth/login', {
                    data: {
                        oauth_provider: 'facebook',
                        oauth_token: accessToken
                    },
                    cache: false
                })
                .done(function(data) {                                              

                    mixpanel.track('User Logged In', {
                        id: data.resource.id,
                        provider: 'Facebook',
                        email: data.resource.email,
                        first_name: data.resource.first_name,
                        last_name: data.resource.last_name
                    });
                    if (data.msg == 'Resource registered') {
                      if(navigator.geolocation){
                      // Prompt for Allow Deny Geolocation popup.
                      }
                    }
                 });
Param Singh
  • 1,325
  • 3
  • 13
  • 28
  • Not getting the question. Where are you stuck exactly ? If I read just the question which is : Triggering Javascript Code from PHP Laravel Controller. The answer would be : Use echo " function(); "; or something similar. you're not technically calling the function in PHP, but this as close as your going to get. – brute_force Oct 03 '15 at 07:33
  • Is doing echo "" is right ? Is there any other right way to do this thing? I'm stuck at how do I execute some client side JS code during the PHP code execution as shown above. – Param Singh Oct 03 '15 at 07:36
  • http://stackoverflow.com/a/1045885/1235298 Read this. You will understand the question your are asking. – brute_force Oct 03 '15 at 07:46
  • How are you getting to `function callback` - is it via a form submission, or a link on a page, etc, etc ? – Chris Oct 03 '15 at 11:00
  • No, its a route function endpoint like Route::get('fb-login-callback', 'FacebookLogin@callback'); – Param Singh Oct 03 '15 at 11:05

0 Answers0