首页 > 解决方案 > 登录后更改重定向路径并使用 Laravel 8 使用 Google 登录

问题描述

我在目录 app/HTTP/controllers/LoginWithGoogle.php 中有这段代码

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Laravel\Socialite\Facades\Socialite;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Exception;

class LoginWithGoogleController extends Controller
{
     public function redirectToGoogle()
    {
        return Socialite::driver('google')->redirect();
    }

    public function handleGoogleCallback()
    {
        try {

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

            $finduser = User::where('google_id', $user->id)->first();

            if($finduser){

                Auth::login($finduser);

                return redirect()->intended('dashboard');

            }else{
                $newUser = User::create([
                    'name' => $user->name,
                    'email' => $user->email,
                    'google_id'=> $user->id,
                    'password' => encrypt('123456dummy')
                ]);

                Auth::login($newUser);

                return redirect()->intended('dashboard');
            }

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

}

我想更改return redirect()->intended('dashboard'); and return redirect()->intended('dashboard'); 目录 resorces/views/index.blade.php 如果请帮助我该怎么办如果请帮助我,我是 Web 开发的新手。如果请帮助我,我该怎么做我是 Web 开发的新手。如果请帮助我,我该怎么做我是 Web 开发的新手。

标签: phplaravellaravel-8

解决方案


正如 Micheal 所说,您可以使用return redirect()->route('home')而不是return redirect()->intended('dashboard');

为简单起见,您可以Route::view('/', 'index')->name('home');web.php中使用

此外,如果您想传递静态值,您可以在第三个参数中将其作为数组传递

Route::view('/', 'index', ['title' => 'Welcome to home'])->name('home');

推荐阅读