diff --git a/app/Http/Controllers/Auth/GoogleAuthController.php b/app/Http/Controllers/Auth/GoogleAuthController.php index 456641e..2a84d9d 100644 --- a/app/Http/Controllers/Auth/GoogleAuthController.php +++ b/app/Http/Controllers/Auth/GoogleAuthController.php @@ -27,20 +27,22 @@ public function handleGoogleCallback() return redirect()->route('login')->with(['email' => 'Your Google account is missing names.']); } - $user = User::firstOrCreate( - ['email' => $googleUser->email], - [ + $user = User::where('email', $googleUser->user['email'])->first(); + + if (! $user) { + $user = User::create([ 'email' => $googleUser->email, 'password' => Str::random(), 'email_verified_at' => now(), - ] - ); - - $user->profile()->updateOrCreate([ - 'user_id' => $user->id, - 'first_name' => $googleUser->user['given_name'], - 'last_name' => $googleUser->user['family_name'] - ]); + 'social_auth_sign_up' => true, + ]); + + $user->profile()->updateOrCreate([ + 'user_id' => $user->id, + 'first_name' => $googleUser->user['given_name'], + 'last_name' => $googleUser->user['family_name'] + ]); + } Auth::login($user); diff --git a/app/Http/Controllers/Auth/PasswordController.php b/app/Http/Controllers/Auth/PasswordController.php index 7c1174d..bd8d7f5 100644 --- a/app/Http/Controllers/Auth/PasswordController.php +++ b/app/Http/Controllers/Auth/PasswordController.php @@ -4,7 +4,9 @@ use App\Http\Controllers\Controller; use App\Http\Requests\PasswordUpdateRequest; +use App\Http\Requests\SetPasswordOnGoogleSignUp; use Illuminate\Http\RedirectResponse; +use Illuminate\Http\Request; use Illuminate\Support\Facades\Hash; class PasswordController extends Controller @@ -12,11 +14,21 @@ class PasswordController extends Controller /** * Update the user's password. */ - public function update(PasswordUpdateRequest $request): RedirectResponse + public function update(Request $request): RedirectResponse { - $request->user()->update([ - 'password' => Hash::make($request->password), - ]); + $rules = $request->user()->social_auth_sign_up + ? app(SetPasswordOnGoogleSignUp::class)->rules() + : app(PasswordUpdateRequest::class)->rules(); + + $validatedData = $request->validate($rules); + + $updateData = ['password' => Hash::make($validatedData['password'])]; + + if ($request->user()->social_auth_sign_up) { + $updateData['social_auth_sign_up'] = false; + } + + $request->user()->update($updateData); return back(); } diff --git a/app/Http/Requests/SetPasswordOnGoogleSignUp.php b/app/Http/Requests/SetPasswordOnGoogleSignUp.php new file mode 100644 index 0000000..45c8536 --- /dev/null +++ b/app/Http/Requests/SetPasswordOnGoogleSignUp.php @@ -0,0 +1,27 @@ +user(); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array|string> + */ + public function rules(): array + { + return NewPasswordValidation::rules(); + } +} diff --git a/database/migrations/2024_10_06_190609_add_social_auth_column_user_table.php b/database/migrations/2024_10_06_190609_add_social_auth_column_user_table.php new file mode 100644 index 0000000..c69672a --- /dev/null +++ b/database/migrations/2024_10_06_190609_add_social_auth_column_user_table.php @@ -0,0 +1,28 @@ +boolean('social_auth_sign_up')->default(false)->after('email'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('users', function (Blueprint $table) { + $table->dropColumn('social_auth_sign_up'); + }); + } +}; diff --git a/resources/js/Pages/Profile/Partials/UpdatePasswordForm.vue b/resources/js/Pages/Profile/Partials/UpdatePasswordForm.vue index e340ec1..4129094 100644 --- a/resources/js/Pages/Profile/Partials/UpdatePasswordForm.vue +++ b/resources/js/Pages/Profile/Partials/UpdatePasswordForm.vue @@ -6,19 +6,14 @@ import { Label } from '@/Components/ui/label' import { useForm } from 'laravel-precognition-vue-inertia'; import { ref } from 'vue'; import { useToast } from '@/Components/ui/toast/use-toast' +import {router, usePage} from "@inertiajs/vue3"; const { toast } = useToast(); +const user = usePage().props.auth.user; const passwordInput = ref(null); const currentPasswordInput = ref(null); -/* -const form = useForm({ - current_password: '', - password: '', - password_confirmation: '', -}); -*/ const form = useForm('put', route('password.update'), { current_password: '', @@ -29,11 +24,16 @@ const form = useForm('put', route('password.update'), { const submit = () => form.submit({ preserveScroll: true, onSuccess: () => { + router.get(route('profile.edit'), {}, { + preserveScroll: true, + }); + toast({ title: 'Password Updated', description: 'Your password has been successfully updated.', variant: 'success', }); + form.reset(); }, onError: () => { @@ -43,7 +43,6 @@ const submit = () => form.submit({ } if (form.errors.current_password) { form.reset('current_password'); - // currentPasswordInput.value?.focus(); } }, }); @@ -56,12 +55,12 @@ const submit = () => form.submit({

Update Password

- Ensure your account is using a long, random password to stay secure. + Ensure to pick a strong password to stay secure.

-
+