如何使用Google OAuth在Laravel中设置记住令牌 [英] How do I set the Remember Token in Laravel with Google OAuth
问题描述
标题几乎总结了一下.在使用Google验证用户身份时,我无法使用Auth::attempt
.
The title pretty much sums it up. I'm having trouble using Auth::attempt
when authenticating users with Google.
我尝试将以下内容放在我的User类上,但是似乎没有用.
I tried putting the following on my User class, but it didn't seem to work.
public function getAuthPassword()
{
return $this->some_field;
}
有没有办法手动设置记住令牌?还是有办法告诉Laravel在另一个字段中输入密码?
Is there a way to manually set the remember token? Or is there a way to tell Laravel to look at a different field for the password?
我将尝试扩展Auth::();
我们将看看情况如何.
I'm going to attempt to extend Auth::();
We'll see how that goes.
推荐答案
好的,我知道了.我做了几件事.
Alright, I figured it out. I did a few things.
1.创建一个App/Extensions文件夹,并在该文件夹中创建一个新类.
新课程ExampleAuthProvider extends EloquentUserProvider
这里最棘手的部分是筛选较早的问题和文档,以获取要使用的类的正确列表.这是我的清单.
The tricky part here was sifting through older questions and documentation to get the right list of classes to use. Here's my list.
use App\User;
use Illuminate\Support\Str;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
use Illuminate\Auth\EloquentUserProvider;
2.我重写了retrieveByCredentials()
和validateCredentials()
2. I rewrote retrieveByCredentials()
and validateCredentials()
这里是retrieveByCredentials()
:
public function retrieveByCredentials(array $credentials)
{
$query = $this->createModel()->newQuery();
foreach ($credentials as $key => $value) {
if (! Str::contains($key, 'value_to_replace_password')) {
$query->where($key, $value);
}
}
return $query->first();
}
和validateCredentials()
:
public function validateCredentials(UserContract $user, array $credentials)
{
$plain = $credentials['value_to_replace_password'];
if ($plain != $user->getAuthPassword()) {
return false;
}
return true;
}
现在,这只是功能,但是我需要在这个新字段上实现哈希,如果您查看validateCredentials()
的EloquentUserProvider
s方法,您会发现它会进行哈希检查.
Now, this is just functional, but I need to implement hashing on this new field, and if you look at EloquentUserProvider
s method for validateCredentials()
, you'll see that it does a hash check.
3.创建新的服务提供商.
php artisan make:provider YourNewServiceProvider
在启动方法中,根据文档进行以下操作. >
In the boot method, do the following, based on the documentation.
public function boot()
{
Auth::extend('customAuth', function($app)
{
return new OAuthProvider(new User);
});
}
一旦重新添加哈希方法,我还将把new Hasher
传递给OauthProvider
.
Once I add the hash method back in, I'll also pass new Hasher
to OauthProvider
.
请确保导入您的课程.
use Illuminate\Contracts\Hashing\Hasher;
use App\User;
use \Auth;
use App\Extensions\OAuthProvider;
use Illuminate\Support\ServiceProvider;
4.将您的服务提供商添加到config/app.php
App\Providers\CustomAuthProvider::class,
我可能应该在提供者名称中包含Service.
I probably should have included Service in the provider name.
我认为这就是一切.这是一次有趣的经历.另外,我将第一个承认我是一个非常新的开发人员,也许我搞砸了.
I think that's everything. It was an interesting experience. Also, I will be the first to admit that I'm a very new developer, and maybe I messed something up.
这篇关于如何使用Google OAuth在Laravel中设置记住令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!