diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 97d8ea27..d7fe89d9 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -248,20 +248,20 @@ class UserController extends Controller return json(trans('user.profile.password.success'), 0); case 'email': - $request->validate([ - 'new_email' => 'required|email', + $data = $request->validate([ + 'email' => 'required|email', 'password' => 'required|min:6|max:32', ]); - if (User::where('email', $request->new_email)->count() > 0) { + if (User::where('email', $data['email'])->count() > 0) { return json(trans('user.profile.email.existed'), 1); } - if (!$user->verifyPassword($request->input('password'))) { + if (!$user->verifyPassword($data['password'])) { return json(trans('user.profile.email.wrong-password'), 1); } - $user->email = $request->input('new_email'); + $user->email = $data['email']; $user->verified = false; $user->save(); diff --git a/resources/assets/src/views/user/profile/email.ts b/resources/assets/src/views/user/profile/email.ts index 0c43abf8..ff7a1272 100644 --- a/resources/assets/src/views/user/profile/email.ts +++ b/resources/assets/src/views/user/profile/email.ts @@ -11,7 +11,7 @@ export default async function handler(event: Event) { const { code, message }: ResponseBody = await post( '/user/profile?action=email', { - new_email: email, + email, password, }, ) diff --git a/resources/assets/tests/views/user/profile/email.test.ts b/resources/assets/tests/views/user/profile/email.test.ts index 80f40ae6..c57efeb4 100644 --- a/resources/assets/tests/views/user/profile/email.test.ts +++ b/resources/assets/tests/views/user/profile/email.test.ts @@ -30,7 +30,7 @@ test('change email', async () => { form.dispatchEvent(event) await flushPromises() expect(post).toBeCalledWith('/user/profile?action=email', { - new_email: 'a@b.c', + email: 'a@b.c', password: 'abc', }) expect(showModal).toBeCalledWith({ mode: 'alert', text: 'w' }) diff --git a/tests/HttpTest/ControllersTest/UserControllerTest.php b/tests/HttpTest/ControllersTest/UserControllerTest.php index aecb0745..437238bc 100644 --- a/tests/HttpTest/ControllersTest/UserControllerTest.php +++ b/tests/HttpTest/ControllersTest/UserControllerTest.php @@ -366,38 +366,38 @@ class UserControllerTest extends TestCase Event::fake(); $user = User::find($user->uid); - // Change email without `new_email` field + // Change email without `email` field $this->actingAs($user) ->postJson( '/user/profile', ['action' => 'email'] ) - ->assertJsonValidationErrors('new_email'); + ->assertJsonValidationErrors('email'); // Invalid email $this->postJson('/user/profile', [ 'action' => 'email', - 'new_email' => 'not_an_email', - ])->assertJsonValidationErrors('new_email'); + 'email' => 'not_an_email', + ])->assertJsonValidationErrors('email'); // Too short current password $this->postJson('/user/profile', [ 'action' => 'email', - 'new_email' => 'a@b.c', + 'email' => 'a@b.c', 'password' => '1', ])->assertJsonValidationErrors('password'); // Too long current password $this->postJson('/user/profile', [ 'action' => 'email', - 'new_email' => 'a@b.c', + 'email' => 'a@b.c', 'password' => Str::random(33), ])->assertJsonValidationErrors('password'); // Use a duplicated email $this->postJson('/user/profile', [ 'action' => 'email', - 'new_email' => $user->email, + 'email' => $user->email, 'password' => '87654321', ])->assertJson([ 'code' => 1, @@ -407,7 +407,7 @@ class UserControllerTest extends TestCase // Wrong password $this->postJson('/user/profile', [ 'action' => 'email', - 'new_email' => 'a@b.c', + 'email' => 'a@b.c', 'password' => '7654321', ])->assertJson([ 'code' => 1, @@ -417,7 +417,7 @@ class UserControllerTest extends TestCase // Change email successfully $this->postJson('/user/profile', [ 'action' => 'email', - 'new_email' => 'a@b.c', + 'email' => 'a@b.c', 'password' => '87654321', ])->assertJson([ 'code' => 0, @@ -428,7 +428,7 @@ class UserControllerTest extends TestCase $this->assertEquals($uid, $user->uid); $this->assertEquals('email', $action); $this->assertEquals([ - 'new_email' => 'a@b.c', + 'email' => 'a@b.c', 'password' => '87654321', ], $addition);