mercredi 7 février 2018

Validation Rule for social login

I was trying to implement social login with laravel. Everything seems OK. User can login . But problem is same email with facebook & google + also logged in. So i don't want to logged in with same email. How do i implement validation rule for social login? Here is the code i i was trying for validation but it throws in $validator

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

    public function handleProviderCallback($provider,Request $request)
    {
        if (!$request->has('code') || $request->has('denied'))
        {
            return redirect('/');
        }
        $user = Socialite::driver($provider)->user();     
        $authUser = $this->findOrCreateUser($user, $provider);
        Auth::login($authUser, true);      
    }

    public function findOrCreateUser($user, $provider)
    {
        $authUser = User::where('provider_id', $user->id)->first();
        if ($authUser) 
        {
            return $authUser;
        }

    $validator = Validator::make($user->toArray(), [
            'email' => 'max:255|unique:users',

        ]);
    if ($validator->passes())
    {

    return  User::create([
            'name'     => $user->name,
            'email'    => $user->email,
            'provider' => $provider,
            'provider_id' => $user->id,
            ]);
    }
        return Redirect::to('login')->with('errors',$validator->errors()); 
    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire