Skip to content

Commit

Permalink
Refactored Password Update and GoogleAuth controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacHatilima committed Oct 6, 2024
1 parent 7144743 commit 62a81af
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 25 deletions.
24 changes: 13 additions & 11 deletions app/Http/Controllers/Auth/GoogleAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
20 changes: 16 additions & 4 deletions app/Http/Controllers/Auth/PasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,31 @@

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
{
/**
* 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();
}
Expand Down
27 changes: 27 additions & 0 deletions app/Http/Requests/SetPasswordOnGoogleSignUp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Http\Requests;

use App\Validations\NewPasswordValidation;
use Illuminate\Foundation\Http\FormRequest;

class SetPasswordOnGoogleSignUp extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return (bool) auth()->user();
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return NewPasswordValidation::rules();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->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');
});
}
};
19 changes: 9 additions & 10 deletions resources/js/Pages/Profile/Partials/UpdatePasswordForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLInputElement | null>(null);
const currentPasswordInput = ref<HTMLInputElement | null>(null);
/*
const form = useForm({
current_password: '',
password: '',
password_confirmation: '',
});
*/
const form = useForm('put', route('password.update'), {
current_password: '',
Expand All @@ -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: () => {
Expand All @@ -43,7 +43,6 @@ const submit = () => form.submit({
}
if (form.errors.current_password) {
form.reset('current_password');
// currentPasswordInput.value?.focus();
}
},
});
Expand All @@ -56,12 +55,12 @@ const submit = () => form.submit({
<h2 class="text-lg font-medium text-gray-900">Update Password</h2>

<p class="mt-1 text-sm text-gray-600">
Ensure your account is using a long, random password to stay secure.
Ensure to pick a strong password to stay secure.
</p>
</header>

<form @submit.prevent="submit" class="mt-6 space-y-6">
<div>
<div v-if="!user.social_auth_sign_up">
<Label for="current_password">Current Password</Label>

<Input
Expand Down
1 change: 1 addition & 0 deletions resources/js/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface User {
name: string;
email: string;
downloaded_code: boolean;
social_auth_sign_up: boolean;
email_verified_at?: string;
two_factor_secret?: string;
two_factor_confirmed_at?: string;
Expand Down

0 comments on commit 62a81af

Please sign in to comment.