简体   繁体   中英

Laravel 6 not require email verification to social login

I am new in laravel. I have created login and signup form using auth command.

I have activated the email verification for login. Also I have created the social login for Gmail,Fb etc. using the socialite based on below link.

https://www.tutsmake.com/laravel-6-google-login-tutorial-with-socialite-demo-example/

Now I don't require email verification for those user who login through social but mandatory for manual signup.

My Homecontroller

namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class HomeController extends Controller
{
/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
     $this->middleware(['auth', 'verified']);
}

/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Contracts\Support\Renderable
 */
public function index()
{
    return view('frontend.index');
}
public function seedr()
{
    $users=DB::table('users')->get();

    return view('backend.seedr',['users'=>$users]);
}

}

My SocialController

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator,Redirect,Response,File;
use Socialite;

use App\User;
class SocialController extends Controller
{
   public function redirect($provider)
  {
    //echo $provider;die;
    return Socialite::driver($provider)->redirect();
  }

  public function callback($provider)
 {

   $getInfo = Socialite::driver($provider)->user();

   $user = $this->createUser($getInfo,$provider);

  auth()->login($user);

  return redirect()->to('/home');

 }
 function createUser($getInfo,$provider){

  $user = User::where('provider_id', $getInfo->id)->first();

 if (!$user) {
  //$mytime = Carbon::now();
 $currenttime=date("Y-m-d h:i:s a", time());

 $user = User::create([
    'name'     => $getInfo->name,
    'email'    => $getInfo->email,
    'provider' => $provider,
    'provider_id' => $getInfo->id,

]);
 //die;
}
return $user;
}
}

You probably have a column email_verified_at that stores the date when the email address was verified, by default it's null which means the user is not verified. In your SocialController@createUser set it to the current date:

 $user = User::create([
    'name'     => $getInfo->name,
    'email'    => $getInfo->email,
    'provider' => $provider,
    'provider_id' => $getInfo->id,
    'email_verified_at' => now()
]);

Do NOT use it as the @Raftel mentioned! When you add email_verified_at to your $fillable variable, then hacker could break your whole Email Verification by sending a hidden input (eg <input type="hidden" name="email_verified_at" value="<?php echo now() ?>"/ > ...

You can use undocumented method on eloquent models called forceFill() instead: https://www.mike-griffiths.co.uk/blog/laravels-forcefill-and-forcecreate/

You can use markEmailAsVerified()

$user = User::create([
    'name'     => $getInfo->name,
    'email'    => $getInfo->email,
    'provider' => $provider,
    'provider_id' => $getInfo->id,

]);
$user->markEmailAsVerified();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM