首页 文章

Laravel:如何检查用户是否使用oauth登录?

提问于
浏览
1

我正在使用我的laravel应用程序中的包oauth-4-laravel与oauth实现社交登录 . 按照包的说明我想要存储用户数据,但我仍然希望用户只有在使用facebook或google时才能访问页面 . 我必须检查用户是否在路由过滤器中使用oauth进行了登录 . 像这样的东西

Route::filter('auth', function()
{
    if (!OAuth::check()) return Redirect::guest('login');
});

我怎么做?或者我应该使用其他方法?

1 回答

  • 0

    通常,即使您使用OAuth作为身份验证方法,您的应用程序仍然必须存储user_id或名称,而他们的电子邮件不是吗?如果是这样,您仍然可以像这样应用laravel身份验证:

    //After the OAuth authentication
    $user = User::where('email', $email)->firstOrFail(); //$email is the email returned from OAuth
    
    //without acessing DB
    $user = new User();
    $user->email = $email; //this is an eg, can be replace with whatever user's properties you have.
    
    Auth::login($user); //login the user using laravel
    
    //To logout user manually
    Auth::logout();
    
    //In your filters.php
    Route::filter('auth', function()
    {
        if (Auth::guest()) 
            return Redirect::guest('login');
    });
    

相关问题