feat: locale
This commit is contained in:
@@ -65,6 +65,10 @@ class AuthController extends Controller
|
||||
], 401);
|
||||
}
|
||||
|
||||
if (isset($data['locale']) && $user->locale !== $data['locale']) {
|
||||
$user->forceFill(['locale' => $data['locale']])->save();
|
||||
}
|
||||
|
||||
if (! $user->hasVerifiedEmail()) {
|
||||
return response()->json([
|
||||
'message' => __('api.auth.email_not_verified'),
|
||||
@@ -162,9 +166,13 @@ class AuthController extends Controller
|
||||
|
||||
public function showResetPasswordForm(Request $request, string $token): View
|
||||
{
|
||||
$email = (string) $request->query('email', '');
|
||||
$locale = $this->localeForEmail($email);
|
||||
|
||||
return view('auth.reset-password', [
|
||||
'appName' => config('app.name'),
|
||||
'email' => (string) $request->query('email', ''),
|
||||
'email' => $email,
|
||||
'locale' => $locale,
|
||||
'passwordReset' => false,
|
||||
'token' => $token,
|
||||
]);
|
||||
@@ -172,6 +180,9 @@ class AuthController extends Controller
|
||||
|
||||
public function resetPasswordFromView(ResetPasswordRequest $request): RedirectResponse|View
|
||||
{
|
||||
$locale = $this->localeForEmail((string) $request->validated('email'));
|
||||
app()->setLocale($locale);
|
||||
|
||||
$status = $this->resetPasswordFor($request->safe());
|
||||
|
||||
if ($status !== Password::PASSWORD_RESET) {
|
||||
@@ -183,6 +194,7 @@ class AuthController extends Controller
|
||||
return view('auth.reset-password', [
|
||||
'appName' => config('app.name'),
|
||||
'email' => strtolower($request->validated('email')),
|
||||
'locale' => $locale,
|
||||
'passwordReset' => true,
|
||||
'token' => '',
|
||||
]);
|
||||
@@ -298,6 +310,11 @@ class AuthController extends Controller
|
||||
);
|
||||
}
|
||||
|
||||
private function localeForEmail(string $email): string
|
||||
{
|
||||
return User::where('email', strtolower($email))->value('locale') ?: 'en';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
* @return array<string, mixed>
|
||||
@@ -307,6 +324,7 @@ class AuthController extends Controller
|
||||
$attributes = Arr::only($data, [
|
||||
'name',
|
||||
'avatar_url',
|
||||
'locale',
|
||||
'height',
|
||||
'bio',
|
||||
'sex',
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class LoginRequest extends FormRequest
|
||||
{
|
||||
@@ -23,7 +24,24 @@ class LoginRequest extends FormRequest
|
||||
{
|
||||
return [
|
||||
'email' => ['required', 'string'],
|
||||
'locale' => ['sometimes', 'string', Rule::in(config('app.supported_locales', ['fr', 'en']))],
|
||||
'password' => ['required', 'string'],
|
||||
];
|
||||
}
|
||||
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
if ($this->has('locale')) {
|
||||
$this->merge([
|
||||
'locale' => $this->normalizeLocale((string) $this->input('locale')),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
private function normalizeLocale(string $locale): string
|
||||
{
|
||||
$locale = strtolower(str_replace('_', '-', $locale));
|
||||
|
||||
return explode('-', $locale)[0];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ class RegisterRequest extends FormRequest
|
||||
'unique:users,name',
|
||||
],
|
||||
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
|
||||
'locale' => ['sometimes', 'string', Rule::in(config('app.supported_locales', ['fr', 'en']))],
|
||||
'password' => ['required', 'string', 'min:8'],
|
||||
'avatar' => ['sometimes', 'nullable', 'image', 'max:2048'],
|
||||
'bio' => ['nullable', 'string'],
|
||||
@@ -56,4 +57,20 @@ class RegisterRequest extends FormRequest
|
||||
'avatar.max' => __('api.validation.image_max_2mb'),
|
||||
];
|
||||
}
|
||||
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
if ($this->has('locale')) {
|
||||
$this->merge([
|
||||
'locale' => $this->normalizeLocale((string) $this->input('locale')),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
private function normalizeLocale(string $locale): string
|
||||
{
|
||||
$locale = strtolower(str_replace('_', '-', $locale));
|
||||
|
||||
return explode('-', $locale)[0];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ class UpdateUserRequest extends FormRequest
|
||||
return [
|
||||
'bio' => ['sometimes', 'nullable', 'string'],
|
||||
'height' => ['sometimes', 'nullable', 'integer'],
|
||||
'locale' => ['sometimes', 'string', Rule::in(config('app.supported_locales', ['fr', 'en']))],
|
||||
'avatar' => ['sometimes', 'nullable', 'image', 'max:2048'],
|
||||
'nutritionGoals' => ['sometimes', 'array'],
|
||||
'nutritionGoals.calories' => ['required_with:nutritionGoals', 'integer', 'min:0', 'max:100000'],
|
||||
@@ -46,4 +47,20 @@ class UpdateUserRequest extends FormRequest
|
||||
'nutritionGoals.*.max' => __('api.validation.nutrition_goals_max'),
|
||||
];
|
||||
}
|
||||
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
if ($this->has('locale')) {
|
||||
$this->merge([
|
||||
'locale' => $this->normalizeLocale((string) $this->input('locale')),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
private function normalizeLocale(string $locale): string
|
||||
{
|
||||
$locale = strtolower(str_replace('_', '-', $locale));
|
||||
|
||||
return explode('-', $locale)[0];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ class UserResource extends JsonResource
|
||||
return [
|
||||
'name' => $this->name,
|
||||
'email' => $this->email,
|
||||
'locale' => $this->locale,
|
||||
'emailVerified' => $this->hasVerifiedEmail(),
|
||||
'emailVerifiedAt' => $this->email_verified_at?->toISOString(),
|
||||
'height' => $this->height,
|
||||
|
||||
Reference in New Issue
Block a user