登录后更改重定向路径并使用 Laravel 8 登录 Google

时间:2021-04-20 04:58:05

标签: php laravel laravel-8

我在目录 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 开发的新手。如果请帮助我我该怎么做我是 web 开发的新手。< /p>

2 个答案:

答案 0 :(得分:0)

return redirect()->route('home'); 或者您为该视图命名的路线 但是您需要确保在路由上有一个 auth 中间件。例如

Route::get('/', function () {
    return view('index');
})->name('home')->middleware('auth');

答案 1 :(得分:0)

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

为了简单起见,您可以在 web.php

中使用 Route::view('/', 'index')->name('home');

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

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