如何解决使用Google的Laravel身份验证不适用于Guard
我一直在使用具有多个防护身份验证的Laravel项目。我的警卫名之一是“客户”。仅允许客户使用其Google帐户登录。以前,我使用Laravel默认用户模型作为客户帐户,并且在使用Google帐户登录时工作正常。现在,为了使我的客户帐户分开,我创建了一个新的客户模型,并将我所有的东西转移到该模型上。一切正常,但是在使用Google帐户登录时,它正在创建一个包含所有数据的新客户行,但未登录到仪表板。我正在为客户使用中间件。由于未登录中间件,因此阻止了访问仪表板。这是我下面的代码。让我知道是否有人可以提供帮助。
客户模型
<?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',];
}
客户迁移文件
<?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','handleGoogleCallback']);
Route::get('/dashboard',[App\Http\Controllers\Customer\DashboardController::class,'index'])->middleware('customer')->name('dashboard');
我认为问题出在GoogleController
Auth::login($finduser);
Auth::login($newUser);
以上代码无效。有人可以帮我吗?
更新:在下面添加了中间件代码
<?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");
}
}
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。