简体   繁体   English

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

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

i have this on link pop up我有这个链接弹出

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

My route我的路线

 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']); });

authorize redirect url from google cloud platform从谷歌云平台授权重定向url

enter image description here在此处输入图像描述

services.php服务.php

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

my controller我的控制器

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);
    }
}

but still got this message enter image description here但仍然收到此消息在此处输入图像描述

i hope you guys can help me, i have to finish this before my thesis defence我希望你们能帮助我,我必须在我的论文答辩之前完成这个

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

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

If that doesn't work you could print out the error message and post it so that we can get a clear understanding of your error.如果这不起作用,您可以打印出错误消息并将其发布,以便我们可以清楚地了解您的错误。

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

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

Thanks for trying to help @SachithMuhandiram, the problem is array to string conversion when i try to dump,感谢您尝试帮助@SachithMuhandiram,当我尝试转储时,问题是数组到字符串的转换,

this is user controller, problematic code is here这是用户控制器,有问题的代码在这里

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**

    ]);

}

this is my user model, and i protected this这是我的用户模型,我保护了它

` protected $fillable = [ 'name', 'email', 'avatar', 'occupation', 'is_admin' ]; ` 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',
];

` Index.php `索引.php

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

i dont know how to fix it, i search and there's not same case我不知道如何解决它,我搜索并没有相同的情况

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

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