diff --git a/backend/.env.example b/backend/.env.example index 56d0dd35..b1ded5ec 100644 --- a/backend/.env.example +++ b/backend/.env.example @@ -2,7 +2,7 @@ APP_NAME=Laravel APP_ENV=local APP_KEY= APP_DEBUG=true -APP_URL=http://localhost +APP_URL=http://127.0.0.1 LOG_CHANNEL=stack LOG_LEVEL=debug diff --git a/backend/app/Http/Controllers/Api/FathersController.php b/backend/app/Http/Controllers/Api/FathersController.php index ade87976..85177bee 100644 --- a/backend/app/Http/Controllers/Api/FathersController.php +++ b/backend/app/Http/Controllers/Api/FathersController.php @@ -13,6 +13,7 @@ use Illuminate\Support\Facades\Storage; use App\Models\Father; use App\Models\FatherRelation; use App\Models\EmailActivation; +use App\Mail\FathersForgetPasswordMail; use App\Mail\FathersRegistrationTemporaryMail; use App\Mail\FathersRegistrationMainMail; @@ -38,7 +39,7 @@ class FathersController extends Controller { return ['status_code' => 422, 'error_messages' => $validate->errors()]; } - if ($result = Father::select('id')->where('email', $r->email)->first()) { + if (null === ($result = Father::select('id')->where('email', $r->email)->first())) { // メールアドレス照合に失敗 return ['status_code' => 400, 'error_messages' => ['メールアドレスが未登録です。入力した情報を確認してください。']]; } @@ -57,7 +58,7 @@ class FathersController extends Controller { EmailActivation::create($create); // メールを送ります。 - Mail::to($r->email)->send(new FathersRegistrationTemporaryMail($token)); + Mail::to($r->email)->send(new FathersForgetPasswordMail($token)); } catch (\Throwable $e) { // 失敗 Log::critical($e->getMessage()); @@ -400,17 +401,17 @@ class FathersController extends Controller { $father_id = $r->father_id; } + else if (isset($r->token)) { + if (null === ($ea = EmailActivation::select('father_id')->where('token', $r->token)->first())) { + return ['status_code' => 400, 'error_messages' => ['パスワードの更新に失敗しました。']]; + } + $father_id = (int)$ea->father_id; + } + if (is_null($father_id) && !isset($r->token)) { return ['status_code' => 400, 'error_messages' => ['パスワードの更新に失敗しました。']]; } - // if (isset($r->token)) { - // if (null === ($father_id = EmailActivation::select('father_id')->where('token', $r->token)->first())) { - // return ['status_code' => 400, 'error_messages' => ['パスワードの更新に失敗しました。']]; - // } - // $father_id = (int)$father_id->father_id; - // } - // バリデーションエラー $validate = Validator::make($r->all(), [ 'password' => 'required|min:8|max:72|confirmed', @@ -426,6 +427,10 @@ class FathersController extends Controller { try { Father::where('id', (int)$father_id)->update($update); + + if (isset($r->token)) { + EmailActivation::where('token', $r->token)->delete(); + } } catch (\Throwable $e) { // 失敗 Log::critical($e->getMessage()); diff --git a/backend/app/Mail/FathersForgetPasswordMail.php b/backend/app/Mail/FathersForgetPasswordMail.php new file mode 100644 index 00000000..dfc3541e --- /dev/null +++ b/backend/app/Mail/FathersForgetPasswordMail.php @@ -0,0 +1,23 @@ +token = $token; + } + + public function build () { + return $this->subject('パスワードリセットを依頼しました。')->markdown('emails.fathers.forgotpassword', [ + 'token' => $this->token + ]); + } +} diff --git a/backend/app/Models/EmailActivation.php b/backend/app/Models/EmailActivation.php index c4bb7a29..90839929 100644 --- a/backend/app/Models/EmailActivation.php +++ b/backend/app/Models/EmailActivation.php @@ -8,6 +8,6 @@ use Illuminate\Database\Eloquent\Model; class EmailActivation extends Model { use HasFactory; - protected $fillable = ['email', 'token', 'ttl']; + protected $fillable = ['type', 'father_id', 'email', 'token', 'ttl']; protected $attributes = ['type' => 0]; } diff --git a/backend/config/app.php b/backend/config/app.php index 2a25ae77..9fe825e4 100644 --- a/backend/config/app.php +++ b/backend/config/app.php @@ -52,7 +52,7 @@ return [ | */ - 'url' => env('APP_URL', 'http://localhost'), + 'url' => env('APP_URL', 'http://127.0.0.1'), 'asset_url' => env('ASSET_URL', null), diff --git a/backend/resources/views/emails/fathers/forgotpassword.blade.php b/backend/resources/views/emails/fathers/forgotpassword.blade.php new file mode 100644 index 00000000..c7d7ce50 --- /dev/null +++ b/backend/resources/views/emails/fathers/forgotpassword.blade.php @@ -0,0 +1,11 @@ +@component('mail::message') +# パスワードリセットを依頼しました。 + +サイトへのパスワードリセットを依頼しました。 + +以下のリンクからパスワードを再設定してください。 + +@component('mail::button', ['url' => url('/').'/p-account/forgot-password/reset/'.$token]) +{{ url('/') }}/p-account/forgot-password/reset/{{ $token }} +@endcomponent +@endcomponent diff --git a/backend/resources/views/emails/fathers/registration/main.blade.php b/backend/resources/views/emails/fathers/registration/main.blade.php index 384068d0..de7cbe09 100644 --- a/backend/resources/views/emails/fathers/registration/main.blade.php +++ b/backend/resources/views/emails/fathers/registration/main.blade.php @@ -5,7 +5,7 @@ 以下のリンクからログインしてくださいませ。 -@component('mail::button', ['url' => env('APP_URL').'/p-account/login']) -{{ env('APP_URL') }}/p-account/login +@component('mail::button', ['url' => url('/').'/p-account/login']) +{{ url('/') }}/p-account/login @endcomponent @endcomponent diff --git a/backend/resources/views/emails/fathers/registration/temporary.blade.php b/backend/resources/views/emails/fathers/registration/temporary.blade.php index 10a0853a..400f680a 100644 --- a/backend/resources/views/emails/fathers/registration/temporary.blade.php +++ b/backend/resources/views/emails/fathers/registration/temporary.blade.php @@ -5,7 +5,7 @@ 以下のリンクからログインして、本登録を完了させてください。 -@component('mail::button', ['url' => env('APP_URL').'/p-account/register/'.$token]) -{{ env('APP_URL') }}/p-account/register/{{ $token }} +@component('mail::button', ['url' => url('/').'/p-account/register/'.$token]) +{{ url('/') }}/p-account/register/{{ $token }} @endcomponent @endcomponent