【问题标题】:Laravel authentication with google is not working with guard谷歌的 Laravel 身份验证不适用于警卫
【发布时间】:2021-01-11 23:35:33
【问题描述】:

我一直在使用具有多个保护身份验证的 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', [App\Http\Controllers\Customer\Auth\GoogleController::class, '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");
    }
}

【问题讨论】:

  • 是否将customer 设置为默认保护?而你没有展示你的中间件
  • 您好,我已经更新了我的问题并添加了中间件代码
  • 是customer你的默认守卫吗?
  • 没有。默认守卫是 sill 用户,我没有更改任何内容。我刚刚添加了额外的客户保护
  • 那么Auth::login(...) 没有使用customer 保护...你必须告诉它要使用什么保护,否则它将使用默认值

标签: php laravel google-oauth laravel-authentication laravel-guard


【解决方案1】:

感谢@lagbox 问题解决了, Reference

错误:Auth::login($newUser);

正确:Auth::guard('customer')-&gt;login($newUser);

【讨论】:

    猜你喜欢
    • 2016-11-25
    • 2021-10-14
    • 2015-09-20
    • 2018-07-23
    • 2016-11-11
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    相关资源
    最近更新 更多