so-manager-dev.com/app/Http/Traits/AuthenticatesUser.php

49 lines
1.7 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Http\Traits;
trait AuthenticatesUser
{
/**
* セッション切れ時のログインリダイレクト(マルチテナント対応)
*
* @param string|null $screenName ログに記録する画面名
* @param bool $saveIntended ログイン後に元のURLに戻るかtrue: 戻る, false: マイページ)
* @return \Illuminate\Http\RedirectResponse|null
*/
protected function handleSessionExpired(?string $screenName = null, bool $saveIntended = true)
{
$userId = session('user_id');
if ($userId) {
return null; // 認証済み
}
// 運営元コード取得
$management = session('management');
$managementCode = $management->management_code ?? request()->segment(1);
if (!$managementCode) {
\Log::error("[ERROR] " . now()->format('Y-m-d H:i:s') . " 運営元取得失敗 url=" . request()->fullUrl());
abort(404);
}
// ログ記録
$logMessage = "[INFO] " . now()->format('Y-m-d H:i:s') . " 未認証ユーザーアクセス(セッション切れ)";
if ($screenName) {
$logMessage .= " {$screenName}画面";
}
$logMessage .= " management_code=" . $managementCode;
\Log::info($logMessage);
if ($saveIntended) {
// ログイン後に元の画面に戻る
return redirect()->guest(route('login_input', ['management_code' => $managementCode]));
} else {
// ログイン後はマイページトップへ
session()->forget('url.intended');
return redirect()->route('login_input', ['management_code' => $managementCode]);
}
}
}