I have been working with a Laravel project with multiple guard authentications. one of my guard name is 'customer'. Only customers are allowed to login with their Google Account. Previously I was using Laravel default Users Model as Customer account and it was working fine while login with Google account. Now in order to make my Customer account separate I had created a new Customer Model and shifted all my things to that model. Everything is working fine but while login with Google account it's creating a new customer row with all data but not logging in to the dashboard. I am using middleware for customers. Since its not logging in the middleware is preventing to access the dashboard. Here is my codes below. let me know if anyone can help.
Customer Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Customer extends Authenticatable
{
use Notifiable;
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password','google_id','linkedin_id'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
}
Customer Migration File
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCustomersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('customers', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->text('password');
$table->text('phone')->unique()->nullable();
$table->text('address')->nullable();
$table->integer('shipments')->nullable();
$table->integer('balance')->nullable();
$table->integer('due')->nullable();
$table->string('google_id')->nullable();
$table->string('linkedin_id')->nullable();
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('customers');
}
}
config/Auth.php
'guards' => [
'customer' => [
'driver' => 'session',
'provider' => 'customers',
],
],
'providers' => [
'customers' => [
'driver' => 'eloquent',
'model' => App\Models\Customer::class,
],
],
'passwords' => [
'customers' => [
'provider' => 'customers',
'table' => 'password_resets',
'expire' => 60,
],
],
GoogleController.php
<?php
namespace App\Http\Controllers\Customer\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Socialite;
use Auth;
use Exception;
use App\Models\Customer;
class GoogleController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function redirectToGoogle()
{
return Socialite::driver('google')->redirect();
}
/**
* Create a new controller instance.
*
* @return void
*/
public function handleGoogleCallback()
{
try {
$customer = Socialite::driver('google')->user();
$finduser = Customer::where('google_id', $customer->id)->first();
if($finduser){
Auth::login($finduser);
return redirect('/customer/dashboard');
}else{
$newUser = Customer::create([
'name' => $customer->name,
'email' => $customer->email,
'google_id'=> $customer->id,
'password' => encrypt('123456dummy')
]);
Auth::login($newUser);
return redirect('/customer/dashboard');
}
} catch (Exception $e) {
dd($e->getMessage());
}
}
}
Web.php
Route::get('/auth/google', [App\Http\Controllers\Customer\Auth\GoogleController::class, 'redirectToGoogle']);
Route::get('/auth/google/callback', [App\Http\Controllers\Customer\Auth\GoogleController::class, 'handleGoogleCallback']);
Route::get('/dashboard',[App\Http\Controllers\Customer\DashboardController::class,'index'])->middleware('customer')->name('dashboard');
I think the problem is with GoogleController
Auth::login($finduser);
Auth::login($newUser);
The above codes are not working. Can anybody help me out >?
Updates : Middleware codes are added below
<?php
namespace App\Http\Middleware;
use Auth;
use Closure;
use Illuminate\Http\Request;
class Customer
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if(Auth::guard('customer')->check()){
return $next($request);
}
return redirect('/customer/login')->with('error',"You must login to access Customer Panel");
}
}
Thanks to @lagbox The problem is solved , Reference
Wrong :
Auth::login($newUser);
Right :
Auth::guard('customer')->login($newUser);