Search code examples
phplaravelfacebookgithublaravel-socialite

How to fix tying to get property name in view problem


I'm trying to connect laravel to socialite form facebook and GitHub everything works well but at the end of the connection to the social network apps I want to return to auth.register view with data from the social provider

enter image description here

I have tried sending it as an array and JSON to the view but still to no avail

function to get data from the social provider


    public function redirectToProvider($social)
    {
        return Socialite::driver($social)->redirect();
       // return dd($social);
      // return $social;
    }

function to handle result


 public function handleProviderCallback($social)
    {
        //$social = 'github';
        $gituser = Socialite::driver($social)->user();

        $user = User::where('provider_id', $gituser->getEmail())->first();

            if($user){
                \Auth::login($user, true); 
                return redirect($this->redirectTo);
             }else{
                  'nemail' => $gituser->getemail()]);
                return view('auth.register')->with('data', ['name' => $gituser->getName(), 'email' => $gituser->getemail()]); 
            }

    }

auth.register view



                            <div class="col-md-6">
                                <input id="name" type="text" class="form-control @error('name') is-invalid @enderror" name="name" value="{{ old('name') }} {{$data->name}}" required autocomplete="name" autofocus>

                                @error('name')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

                            <div class="col-md-6">
                                <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }} {{$data->email}} " required autocomplete="email">

                                @error('email')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

I expect to result to be that the data from the social provided is inserted into my auth.register form field (Name, Email)


Solution

  • You are filling the $data variable with an array and accessing it as an object.

    Change {{$data->name}} in your blade to {{$data['name']}}

    or modify the $data variable.

    return view('auth.register')->with('data', (object)['name' => $gituser->getName(), 'email' => $gituser->getemail()]);