Disable MustVerifyEmail Socialite
March 2, 2025 · AI Conversations
### **1. Remove `MustVerifyEmail` from the User Model**
If your `User` model implements `MustVerifyEmail`, you can simply remove it.
use IlluminateContractsAuthMustVerifyEmail; // Remove this
class User extends Authenticatable
{
use HasFactory, Notifiable; // Remove MustVerifyEmail
}
### **2. Auto-Verify Social Users**
If you still want email verification for regular users but want to automatically verify social users, you can do this in your Socialite callback method:
use IlluminateSupportFacadesAuth;
use AppModelsUser;
use LaravelSocialiteFacadesSocialite;
public function handleProviderCallback()
{
$socialUser = Socialite::driver('google')->user(); // Change driver as needed
$user = User::firstOrCreate(
['email' => $socialUser->getEmail()],
[
'name' => $socialUser->getName(),
'password' => bcrypt(str()->random(16)), // Set a random password
'email_verified_at' => now() // Auto-verify the email
]
);
Auth::login($user);
return redirect('/dashboard'); // Redirect to the intended page
}
### **3. Customize Middleware in Routes**
If your routes use `verified` middleware (`->middleware(['auth', 'verified'])`), and you want to bypass verification for social logins, modify the logic in `app/Http/Middleware/EnsureEmailIsVerified.php`:
public function handle($request, Closure $next)
{
if ($request->user() && $request->user()->email_verified_at) {
return $next($request);
}
if ($request->user() && $request->user()->provider) { // Assuming you store provider info
return $next($request);
}
return redirect(route('verification.notice'));
}
Would you like to implement a specific approach based on your project structure?
$user = User::where('email', $socialUser->getEmail())->first();
if (!$user) {
$user = User::create([
'name' => $socialUser->getName(),
'email' => $socialUser->getEmail(),
'provider' => $provider,
'provider_id' => $socialUser->getId(),
'password' => bcrypt(str()->random(16)), // Dummy password
'email_verified_at' => now()
]);
}
Auth::login($user);
not working
—
## **1. Check Authentication Guard**
If you have multiple authentication guards (e.g., `web`, `admin`, `coach`), ensure that you're logging in with the correct guard:
Auth::guard('web')->login($user);
If you're using a different guard, change `'web'` accordingly.
—
## **2. Ensure User Model Uses `Authenticatable`**
Make sure your `User` model extends `IlluminateFoundationAuthUser` (or `Authenticatable`):
use IlluminateFoundationAuthUser as Authenticatable;
class User extends Authenticatable
{
use HasFactory, Notifiable;
}
—
## **3. Check Session Driver**
If you are using a stateless API (`api` guard) or a `SESSION_DRIVER` that doesn't support sessions, `Auth::login()` won't work.
– Check your `.env` file:
SESSION_DRIVER=file
If it’s set to `array`, change it to `file`, `database`, or `cookie`.
– Run:
php artisan config:clear
—
## **4. Ensure `email_verified_at` is Cast as a Datetime**
Check if `email_verified_at` is properly cast in the `User` model:
protected $casts = [
'email_verified_at' => 'datetime',
];
—
## **5. Debug the Socialite Response**
$user = User::where('email', $socialUser->getEmail())->first();
if (!$user) {
$user = User::create([
'name' => $socialUser->getName(),
'email' => $socialUser->getEmail(),
'provider' => $provider,
'provider_id' => $socialUser->getId(),
'password' => bcrypt(str()->random(16)), // Dummy password
'email_verified_at' => now()
]);
}
Auth::login($user);
email_verified_at not saving value and its still ask for email verification
—
### **1. Check `fillable` Property in User Model**
Ensure `email_verified_at` is listed in the `fillable` array in your `User` model (`app/Models/User.php`):
protected $fillable = [
'name',
'email',
'provider',
'provider_id',
'password',
'email_verified_at', // Ensure this is included
];
If it's not there, Laravel might be ignoring it during `User::create()`.
—
### **2. Ensure `email_verified_at` is Cast as a Datetime**
In your `User` model, verify that `email_verified_at` is correctly cast:
protected $casts = [
'email_verified_at' => 'datetime',
];
—
### **3. Manually Update `email_verified_at` After Creation**
Instead of setting `email_verified_at` during `User::create()`, try updating it afterward:
$user = User::create([
'name' => $socialUser->getName(),
'email' => $socialUser->getEmail(),
'provider' => $provider,
'provider_id' => $socialUser->getId(),
'password' => bcrypt(str()->random(16)), // Dummy password
]);
// Manually update email_verified_at
$user->update(['email_verified_at' => now()]);
This ensures the value is saved in the database.
—
### **4. Verify the Database Entry**
Run this command in Tinker (`php artisan tinker`) to confirm if the column is being updated:
$user = User::where('email', 'example@email.com')->first();
dd($user->email_verified_at);
If it returns `null`, the issue may be with your model settings or database migration.
—
### **5. Check Migration for `email_verified_at`**
Ensure your `users` table migration correctly defines the `email_verified_at` column:
$table->timestamp('email_verified_at')->nullable();
If it's missing, you may need to create a migration to add it:
php artisan make:migration add_email_verified_at_to_users_table --table=users
In the generated migration file (`database/migrations/…`):
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->timestamp('email_verified_at')->nullable()->after('email');
});
}
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('email_verified_at');
});
}
Run the migration:
php artisan migrate
—
### **6. Ensure Middleware Does Not Require Verification**
If you are using the `verified` middleware on your routes, and you want to bypass it for social logins, modify `app/Http/Middleware/EnsureEmailIsVerified.php`:
public function handle($request, Closure $next)
{
if ($request->user() && $request->user()->email_verified_at) {
return $next($request);
}
if ($request->user() && $request->user()->provider) { // Assuming you store provider info
return $next($request);
}
return redirect(route('verification.notice'));
}
—
### **7. Double-Check the Login Guard**
If you're using multiple guards (`admin`, `coach`, etc.), make sure you're logging in with the correct one:
Auth::guard('web')->login($user);
—
### **Final Test**
After making these changes, clear your cache and re-test:
php artisan config:clear
php artisan cache:clear
Now, try logging in via Socialite again and check if `email_verified_at` is correctly stored.
Let me know if you still face issues! 🚀