身份验证无效

时间:2015-09-03 06:33:45

标签: php

我已经问过这个问题,但我仍然没有得到答案。

How to login using Github, Facebook, Gmail and Twitter in Laravel 5.1?

auth not working

我能够存储数据库中gmail,github的数据。

控制器

if (Auth::attempt(['email' => $email, 'password' => $user_id]))
{
    return redirect()->intended('user/UserDashboard');
} 
else
{
    //here i am going to insert if user not logged in already
    return redirect()->intended('user/UserDashboard');
}

我的问题是,如果我回显任何数据而不是return redirect()->intended('user/UserDashboard');,那么它就会显示出来。如果我添加重定向,那么它不起作用。

1 个答案:

答案 0 :(得分:2)

我在使用google for oAuth时并不完全确定它是如何工作的,但我认为你想要的流程是:

  1. 用户从谷歌返回。
  2. 检查用户是否已存在,否则创建新的用户记录。
  3. 登录用户。
  4. 重定向到信息中心。
  5. 假设上述方法看起来像这样。

    控制器

    public function google()
    {
        // Redirect user to google for authentication
        return Socialite::driver('google')->redirect();
    
        // In Laravel 5.0 this would be
        // return Socialize::with('google')->redirect();
    }
    
    public function googleCallback()
    {
        // Return from google with user object
        $googleUser = Socialite::driver('google')->user();
    
        // In Laravel 5.0 the above would be
        // $user = Socialize::with('google')->user();
    
        // If user exists, retrieve the first() record, 
        // otherwise create a new record
        $user = User::firstOrCreate([
            'user_id'  => $googleUser->id,
            'name'     => $googleUser->name,
            'password' => $googleUser->id,
            'email'    => $googleUser->email,
            'avatar'   => $googleUser->avatar
        ]);
    
        // Login the user
        Auth::login($user, true);
    
        // Redirect to the dashboard
        return Redirect::intended('user/UserDashboard');
    }