-1

我一直在使用具有多个保护身份验证的 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');
    }
}

配置/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,
        ],
      
    ],

谷歌控制器.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());
        }
    }
}

网页.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");
    }
}
4

1 回答 1

2

感谢@lagbox 问题解决了, 参考

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

正确的 :Auth::guard('customer')->login($newUser);

于 2020-09-25T16:18:10.987 回答