samedi 15 février 2020

Auth login is not working for new users in laravel 6

Auth login is not working for the new users in laravel 6 socialite. but the user data is saved in a database. I mean to say When a new user tries to register using laravel socialite(Twitter, Facebook), he/she not auto-login but data saved in the database. I want the user to auto login when he/she register using socialites.

class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = RouteServiceProvider::HOME;


public function __construct()
{
 //   $this->middleware('guest')->except('logout');
}

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


public function handleProviderCallback($provider)
{
 try {
    $social_user = Socialite::driver($provider)->user();
    $authuser = $this->findOrCreateUser($social_user,$provider);

    Auth::login($authuser,true);
    return redirect('/');
  } catch(\Exception $e){

   return redirect('/');
}


}
public function findOrCreateUser($social_user,$provider){

    $authuser = SocialProvider::where('provider_id', $social_user->id)->first();

    if(!$authuser){

          $username = slugify($social_user->getName())."-".mt_rand(10000, 99999);
          $password = rand(1000000000, 99999999999);

          $user = User::firstOrCreate(['email' => $social_user->getEmail()], 
          [
              'username'=> $username,
              'name'=> $social_user->getName(),
              'picture'=> $social_user->getAvatar(),
              'password' => Hash::make($password),
              'ip_address'=> get_user_ip(),
          ]);


      $user->socialProviders()->create(

        ['provider_id'=>$social_user->getId(), 'provider'=>$provider]

      );
      return $user;

    }else{


      $user_id = $authuser->user_id;

      $userdata = Constant_model::getDataOneColumn('users',"id",$user_id);

      $user = User::firstOrCreate(['email'=> $userdata[0]->email],
        ['ip_address'=> $userdata[0]->ip_address,
        'username'=> $userdata[0]->username,
        'name'=> $userdata[0]->name,
        'picture'=> $userdata[0]->picture,
        ],


    );


      return $user;

    }
}


public function logout(){

  Auth::logout();
  return redirect('/');
}


}


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2SQmXab
via IFTTT

Aucun commentaire:

Enregistrer un commentaire