krgm.so-manager-dev.com/app/Http/Controllers/Auth/ResetPasswordController.php
OU.ZAIKOU 5df6c31b86
All checks were successful
Deploy main / deploy (push) Successful in 23s
「パスワード忘れ」修正
2026-01-29 00:02:45 +09:00

96 lines
3.4 KiB
PHP
Raw 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\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use App\Models\Ope;
class ResetPasswordController extends Controller
{
public function showResetForm(Request $request)
{
$token = $request->query('token');
$email = $request->query('email');
// トークンのハッシュ化
$tokenHash = hash('sha256', $token);
// トークン・メール・24時間以内の有効性をチェック
$record = DB::table('password_reset_tokens')
->where('ope_mail', $email)
->where('token', $tokenHash)
->first();
if (!$record) {
return redirect()->route('forgot_password')
->withErrors(['email' => 'URLの有効期限24時間が切れました。再度お手続きを行ってください。']);
}
// 24時間チェック
$createdAt = \Carbon\Carbon::parse($record->created_at);
if ($createdAt->addHours(24)->isPast()) {
// 期限切れトークンを削除
DB::table('password_reset_tokens')
->where('ope_mail', $email)
->delete();
return redirect()->route('forgot_password')
->withErrors(['email' => 'URLの有効期限24時間が切れました。再度お手続きを行ってください。']);
}
return view('auth.reset-password', compact('token', 'email'));
}
public function reset(Request $request)
{
$request->validate([
'email' => 'required|email',
'token' => 'required',
'password' => 'required|confirmed|min:8',
]);
// トークンのハッシュ化
$tokenHash = hash('sha256', $request->token);
// トークン・メール・24時間以内の有効性をチェック
$record = DB::table('password_reset_tokens')
->where('ope_mail', $request->email)
->where('token', $tokenHash)
->first();
if (!$record) {
return back()->withErrors(['email' => 'URLの有効期限24時間が切れました。再度お手続きを行ってください。']);
}
// 24時間チェック
$createdAt = \Carbon\Carbon::parse($record->created_at);
if ($createdAt->addHours(24)->isPast()) {
// 期限切れトークンを削除
DB::table('password_reset_tokens')
->where('ope_mail', $request->email)
->delete();
return back()->withErrors(['email' => 'URLの有効期限24時間が切れました。再度お手続きを行ってください。']);
}
// パスワード更新
$user = Ope::where('ope_mail', $request->email)->first();
if (!$user) {
return back()->withErrors(['email' => 'ユーザーが見つかりません。']);
}
$user->password = Hash::make($request->password);
$user->updated_at = now();
// パスワード再設定時もope_pass_changed_atを更新
$user->ope_pass_changed_at = now();
$user->save();
// トークン削除
DB::table('password_reset_tokens')->where('ope_mail', $request->email)->delete();
// パスワード再設定成功画面へリダイレクト
return redirect()->route('password.change.success');
}
}