Laravel sanctum - 刷新后未验证。为什么我未通过身份验证?

Laravel sanctum - unathenticated after refresh. Why am I being unauthenticated?

我有一个简单的身份验证系统,其中我使用 vue 处理前端,Laravel 处理后端。 Laravel Sanctum 正在用于身份验证。

当我登录时,它工作正常并且在 Laravel 内我可以转储登录用户。

然而,当我刷新页面时,我不再通过身份验证。

如你所见,我将使用 app.blade.php 来告诉 javascript 登录用户是谁,但是 isLoggedin 总是 false.This 我觉得不对.我错过了什么吗?

我们提供任何帮助。

api.php

Route::post('login', [UserController::class, 'login']);

UsersController@login

public function login(Request $request)
    {
        $credentials = [
            'email' => $request->email,
            'password' => $request->password,
        ];

        if (Auth::attempt($credentials)) {
            $request->session()->regenerate();
            $success = true;
            $message = 'User login successfully';
            $user = auth()->user();
        } else {
            $success = false;
            $message = 'Unauthorised';
            $user = null;
        }

        // response
        $response = [
            'success' => $success,
            'message' => $message,
            'user' => $user
        ];
        return response()->json($response);
    }

.env

APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DRIVER=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=cookie
SANCTUM_STATEFUL_DOMAIN=localhost
SESSION_DOMAIN=localhost
SESSION_LIFETIME=120
SESSION_SECURE_COOKIE=false;

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

app.blade.php

<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-token" value="{{ csrf_token() }}"/>
    <title>{{env('APP_NAME')}}</title>
    <link href="{{ mix('css/app.css') }}" type="text/css" rel="stylesheet"/>
</head>
<body>
@if (Auth::check())
    @php
    $user_auth_data = [
        'isLoggedin' => true,
        'user' =>  Auth::user()
    ];
    @endphp
@else
    @php
    $user_auth_data = [
        'isLoggedin' => false
    ];
    @endphp
@endif
<script>
    window.Laravel = JSON.parse(atob('{{ base64_encode(json_encode($user_auth_data)) }}'));
    console.log(window.Laravel);
</script>

<div id="app">
</div>
<script src="{{ mix('js/app.js') }}" type="text/javascript"></script>
</body>
</html>```

已修复。

改变SANCTUM_STATEFUL_DOMAIN=localhost 收件人:SANCTUM_STATEFUL_DOMAINS=localhost:9192

您需要端口号。