繁体   English   中英

laravel socialite google login / google auth,回调“404 not found”

[英]laravel socialite google login / google auth, callback "404 not found"

我有这个链接弹出

 <a href="login/google" class="btn btn-white btn-outline-white"><span class="fa fa-google">oogle</span></a>

我的路线

 Route::get('login/google', [GoogleController::class, 'login']); Route::get('login/google/callback', [GoogleController::class, 'callback']); Route::middleware(['auth'])->group(function () { Route::get('logout', [GoogleController::class, 'logout']); Route::get('user', [UserController::class, 'index']); });

从谷歌云平台授权重定向url

在此处输入图像描述

服务.php

 'google' => [ 'client_id' => env('GOOGLE_CLIENT_ID'), 'client_secret' => env('GOOGLE_CLIENT_SECRET'), 'redirect' => 'http://127.0.0.1:8000/login/google/callback',

我的控制器

public function login() {
    return Socialite::driver('google')->redirect();
}
public function callback() {
    try {
        $google_user = Socialite::driver('google')->user();
        $user = User::where('email', $google_user->email)->first();
        if($user) {
            Auth::login($user);
            return redirect('user');
        }
        else {
            $new_user = User::create([
                'name'=> ucwords($google_user->name),
                'email'=> $google_user->email,
                'email_verified_at'=> now(),
                'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi',
                'remember_token'=> Str::random(10),
            ]);
            Auth::login($new_user);
            return redirect('user'); 
        }

       
    } catch (\Throwable $th) { 
       abort(404);
    }
}

但仍然收到此消息在此处输入图像描述

我希望你们能帮助我,我必须在我的论文答辩之前完成这个

use ->stateless() on the following lines
 
return Socialite::driver('google')->stateless()->redirect();

$google_user = Socialite::driver('google')->stateless()->user();

如果这不起作用,您可以打印出错误消息并将其发布,以便我们可以清楚地了解您的错误。

使用catch (Exception $e) {dd($e->getMessage());}

而不是catch (\Throwable $th) { abort(404);}

感谢您尝试帮助@SachithMuhandiram,当我尝试转储时,问题是数组到字符串的转换,

这是用户控制器,有问题的代码在这里

public function index()

{

    return view('index'. [

        'menus' => Menus::get(), **redline here**
        'most' => Menus::where('most','2')->get(), **redline here**
        'categories' => Categories::get(), **redline here**
        'user' => Auth::user() **redline here**

    ]);

}

这是我的用户模型,我保护了它

` protected $fillable = ['name', 'email', 'avatar', 'occupation', 'is_admin' ];

/**
 * The attributes that should be hidden for serialization.
 *
 * @var array<int, string>
 */
protected $hidden = [
    'password',
    'remember_token',
];

/**
 * The attributes that should be cast.
 *
 * @var array<string, string>
 */
protected $casts = [
    'email_verified_at' => 'datetime',
];

`索引.php

 @auth  
      @foreach ($user as $user )
        <tr>
          <td>{{ $user->name }}</td>
        </tr>
      @endforeach
      @endauth

我不知道如何解决它,我搜索并没有相同的情况

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM