Laivel 12 & HolyPhp

Кемеровские программисты php общаются здесь
Ответить
Anonymous
 Laivel 12 & Holy

Сообщение Anonymous »

Я использую Laravel 12 с Sanctum и попытаюсь получить вход через Angular Frontend.
До сих пор, я могу зарегистрироваться, а также получить положительный ответ на токен CSRF. Сообщение, но не помогло. У кого -нибудь есть подсказки для меня?login(data: { email: string; password: string }): Observable {
const headers = new HttpHeaders({
'Accept': 'application/json',
'Content-Type': 'application/json'
});
return this.http.get("http://localhost:8000/sanctum/csrf-cookie", { headers: headers, withCredentials: true }).pipe(
switchMap(() =>
this.http.post(`${this.baseUrl}/login`, data, {
headers: headers,
withCredentials: true,
})
)
);}
< /code>
Это мой маршрутизатор api.php < /p>

Route::post('/login', function (Request $request) {
$credentials = $request->only('email', 'password');

if (Auth::attempt($credentials)) {
$request->session()->regenerate();

return response()->json([
'message' => 'Login erfolgreich',
'user' => Auth::user() // Aktuell eingeloggter User
]);
}

return response()->json(['message' => 'Unauthorized'], 401);
});
< /code>
Это мое промежуточное программное обеспечение внутри app.php < /p>
->withMiddleware(function () {

return [

// 1. Laravel interner Support für Precognitive-Requests
Illuminate\Foundation\Http\Middleware\HandlePrecognitiveRequests::class,

// 2. Session starten (wichtig für Sanctum + Login)
Illuminate\Session\Middleware\StartSession::class,

// 3. Sanctum Middleware, die API-Requests als "stateful" behandelt
Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,

// 5. Eigene globale Middleware (z. B. zusätzliche CORS Header)
function (Illuminate\Http\Request $request, Closure $next) {
$response = $next($request);

$response->headers->set('Access-Control-Allow-Origin', 'http://spa.localhost:4200');
$response->headers->set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
$response->headers->set('Access-Control-Allow-Credentials', 'true');

return $response;
},
];
})
< /code>
my cors.php < /p>


Подробнее здесь: https://stackoverflow.com/questions/797 ... 12-sanctum
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Php»