我有一个具有标准用户表的Laravel应用程序,我正在实现Auth0登录。登录时,在数据库中使用给定的电子邮件创建用户记录。
我有一个CustomUserRepository.php文件:
<?php
namespace App\Repositories;
use App\Models\User;
use Illuminate\Contracts\Auth\Authenticatable;
class CustomUserRepository implements \Auth0\Laravel\Contract\Auth\User\Repository
{
public function fromSession(array $user): ?\Illuminate\Contracts\Auth\Authenticatable
{
return User::firstOrCreate(['email' => $user['email']]);
}
public function fromAccessToken(array $user): ?\Illuminate\Contracts\Auth\Authenticatable
{
// Simliar to above. Used for stateless application types.
return null;
}
public function getUserByUserInfo(array $userinfo) : Authenticatable
{
$user = $this->upsertUser( $userinfo['profile'] );
return new Auth0User( $user->getAttributes(), $userinfo['accessToken'] );
}
protected function upsertUser($profile)
{
return User::firstOrCreate(
[
'sub' => $profile['sub']
],
[
'email' => $profile['email'] ?? '',
'name' => $profile['name'] ?? '',
]
);
}
}我的auth.php文件:
<?php
return [
'defaults' => [
'guard' => 'auth0',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'auth0' => [
'driver' => 'auth0',
'provider' => 'auth0',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'auth0' => [
'driver' => 'auth0',
'repository' => App\Repositories\CustomUserRepository::class
],
],应用程序起作用了。我使用Auth0登录,创建了用户,除了测试之外,一切都像预期的那样工作。
$this->be(User::find(1));
$response = $this->get('/valid-url');
$response->assertStatus(200);
$response = $this->get('/another-valid-url');
$response->assertStatus(200);在本例中,PHPUnit似乎“忘记”了第二个get()请求的登录。第一个很好,状态200,一切正常。对于第二个请求(get或post),我总是将302返回到登录页面。
我该怎么解决这个问题?
发布于 2022-12-02 20:07:25
#在您的登录功能上,应该有以下内容
if(auth()->attempt($formFields)){
$request->session()->regenerate();https://stackoverflow.com/questions/74656730
复制相似问题