GOOD SHELL MAS BOY
Server: Apache/2.4.52 (Ubuntu)
System: Linux vmi1836763.contaboserver.net 5.15.0-130-generic #140-Ubuntu SMP Wed Dec 18 17:59:53 UTC 2024 x86_64
User: www-data (33)
PHP: 8.4.10
Disabled: NONE
Upload Files
File: /var/www/console.fixgini.com/app/Http/Controllers/Onboarding/EmailValidation.php
<?php

namespace App\Http\Controllers\Onboarding;

use App\Models\User;
use App\Mail\OtpMail;
use App\Services\OtpService;
use Illuminate\Http\Request;
use App\Models\OtpVerification;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Mail;
use Illuminate\Validation\ValidationException;

class EmailValidation extends Controller
{
    public function send(Request $request)
    {
        try {
            $validatedData = $request->validate([
                'email' => 'required|email',
            ]);

            $email = $validatedData['email'];


            // Check if the email exists and is not verified (i.e., email_verified_at is null)
            $user = User::where('email', $email)->first();

            // If user exists and email is verified, return an error message
            if ($user && $user->email_verified_at !== null) {
                return response()->json([
                    'status' => 'failed',
                    'message' => 'Email already verified.',
                ], 404);
            }

            // If the user does not exist or email is not verified, send OTP
            // if ($user) {
            //     return response()->json([
            //         'status' => 'failed',
            //         'message' => 'Email already used by another user.',
            //     ], 404);
            // }
            // Send OTP
            $otpService = app(OtpService::class);
            $otpService->sendOtpToEmail($email);

            return response()->json([
                'status' => 'success',
                'message' => 'Email OTP sent.',
            ], 200);
        } catch (\Throwable $th) {
            Log::error($th->getMessage());
            return response()->json([
                'status' => 'failed',
                'message' => $th->getMessage(),
            ], 400);
        }
    }
 
    public function verifyNewEmail(Request $request)
    {
        try {
            // Validate the request
            $validatedData = $request->validate([
                'email' => 'required|email|exists:otp_verifications,email',
                'otp' => 'required|digits:6',
                'user_id' => 'required|exists:users,id',
            ]);

            $email = $validatedData['email'];
            $otp = $validatedData['otp'];

            // Check if OTP is valid and unverified
            $otpRecord = OtpVerification::where([
                ['email', '=', $email],
                ['otp', '=', $otp],
                ['status', '=', 'unverified']
            ])->first();

            if (!$otpRecord) {
                return response()->json(['message' => 'Invalid OTP. Please try again', 'status' => 'error'], 401);
            }

            // Mark the OTP as verified
            $otpRecord->update(['status' => 'verified']);

            $user_id = $validatedData['user_id'];
            // Find the user with the old email (you may need a separate field to store new email if needed)
            $user = User::where('id', $user_id)->first(); // assuming OTP records are linked by user_id
            if ($user) {
                // Update the user's email with the new one and verify it
                $user->update([
                    'email_verified_at' => now(),
                    'email' => $email,  // This is the new email being verified
                    'status' => 'active',
                ]);
            }

            // Delete the OTP record to avoid reuse
            $otpRecord->delete();

            return response()->json(['message' => 'Email verified and updated', 'status' => 'success', 'data' => $user], 200);
        } catch (ValidationException $e) {
            Log::error($e->getMessage());
            return response()->json(['status' => 'error', 'message' => $e->getMessage()], 422);
        }
    }
}