没有找到Laravel socialite- route

时间:2017-06-02 11:10:14

标签: facebook laravel twitter laravel-5 laravel-5.4

我已经按照本教程进行了操作:

https://blog.damirmiladinov.com/laravel/laravel-5.2-socialite-facebook-login.html#.WTFGLGg1_IUhttps://blog.damirmiladinov.com/laravel/laravel-5.2-socialite-twitter-login.html#.WTFGPmg1_IX

我已经完成了两个教程。然而,在尝试使用facebook或twitter登录后,我得到“NotfoundHttpException”

代码与这些教程中的代码相同,但仅仅是为了它,我将在这里一一发布:

这是使用Socialize包:

SocialAuthController.php:

class SocialAuthController extends Controller
{
    public function redirect($provider)
    {
    return Socialite::driver($provider)->redirect();    
    } 
public function callback(SocialAccountService $service, $provider)
{
    // Important change from previous post is that I'm now passing
    // whole driver, not only the user. So no more ->user() part
    $user = $service->createOrGetUser(Socialite::driver($provider));

    auth()->login($user);

    return redirect()->to('/tfgm');
}
}

路线:

Route::get('/redirect/{provider}', 'SocialAuthController@redirect');
Route::get('/callback/{provider}', 'SocialAuthController@callback');

SocialAccountService.php(我把它放在模型所在的app文件夹中,我不知道这是否正确):

<?php

namespace App;

use Laravel\Socialite\Contracts\Provider;

class SocialAccountService
{
    public function createOrGetUser(Provider $provider)
    {

        $providerUser = $provider->user();
        $providerName = class_basename($provider); 

        $account = SocialAccount::whereProvider($providerName)
            ->whereProviderUserId($providerUser->getId())
            ->first();

        if ($account) {
            return $account->user;
        } else {

            $account = new SocialAccount([
                'provider_user_id' => $providerUser->getId(),
                'provider' => $providerName
            ]);

            $user = User::whereEmail($providerUser->getEmail())->first();

            if (!$user) {

                $user = User::create([
                    'email' => $providerUser->getEmail(),
                    'name' => $providerUser->getName(),
                ]);
            }

            $account->user()->associate($user);
            $account->save();

            return $user;

        }

    }
}

0 个答案:

没有答案