Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

karu's avatar
Level 1

How to check if values match with DB and update column in Vue? Laravel API

I'm sending a code to the user when signing up to their email. The code has its own column in the database saved with the users other info. Before transporting the user to the VerifyPage I'm storing the associated email with Vuex so its displayed when the user reach the VerifyPage for adding the code to change the accounts status from 0 to 1.

The data from the api shows in consol.log but i cant match the database data with the code that is manually typed in and the stored email that follow the user from the signup page.

the code i provide down here give Error: response.data.find is not a function . Tried many different codes but all shows errors or nothing at all. No idea how to get around this. Feels so simple but cant find the solution.

Tried GET and PATCH with the api Route too and it doesnt work.

Route::match(['GET', 'POST'], '/users', [AuthController::class, 'users']);

<script setup>
import { ref } from 'vue';
import { useStore } from 'vuex';
import axios from 'axios';
import { useRouter } from 'vue-router';
import { toastController } from '@ionic/vue';

const router = useRouter();
const store = useStore();
const verificationCode = ref('');
const userEmail = store.getters.userEmail;

const verifyCode = async () => {
    try {
        // Call the API
        const response = await axios.get('http://localhost:8000/api/users');

        // Get the user data from response
        const user = response.data.find(u => u.email === userEmail);

        // Verify email and code match
        if (user && user.email === userEmail && 
									user.verificationCode === verificationCode.value) {
            // Call API to update status
            await axios.get(`http://localhost:8000/api/users/${user.id}`, {
                status: 1
            });

            // Redirect to confirmation page
            router.push('/login');
        } else {
            // Not verified, show error
            showToast('Error: Invalid verification code');
        }
    } catch (error) {
        console.error('Error:', error.message);
        showToast('Error: Failed to verify signup');
    }
}

const showToast = async (message) => {
    const toast = await toastController.create({
        message: message,
        position: 'top',
        duration: 5000, // Duration in milliseconds
        color: 'danger' // Customize color
    });
    await toast.present();
};
</script>

TABLE `users` (`id`, `name`, `email`, `email_verified_at`, `password`, `gender`, `preference`, `answer`, `age`, `profileimg`, `country`, `city`, `minage`, `maxage`, `language`, `code`, `status`, `remember_token`, `created_at`, `updated_at`) VALUES
(1, 'kicki', '[email protected]', NULL, 'password', 'female', 'both', 'a', '1995', 'profile_1712877770.jpg', 'Albania', 'a', '22', '25', 'Afrikaans', '256654', 0, NULL, '2024-04-11 21:22:50', '2024-04-11 21:22:50');

0 likes
4 replies
MaverickChan's avatar
  1. Give the signed up user a logged in status.
  2. Let the logged in user permission to update his/her profile data. Don't mix.
karu's avatar
Level 1

@Tray2 Yeah was thinking about that when i went to bed. This is a mess and its better to let Laravel handle it.....

karu's avatar
Level 1

SOLVED

I send email with code to the backend and it verified it for me. I love Laravel its so easy to setup anything. But to put food on the table i am forced to deal with this js shenanigan's :(

public function verify(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'email' => ['required', 'string', 'email'],
            'code' => ['required', 'string', 'regex:/^\d{6}$/'],
        ]);

        if ($validator->fails()) {
            return response()->json(['errors' => $validator->errors()], 422);
        }

        $user = User::where('email', $request->email)->first();

        if (!$user) {
            return response()->json(['error' => 'User not found'], 404);
        }

        if ($user->code !== $request->code) {
            return response()->json(['error' => 'Invalid verification code'], 422);
        }

        // Update user status to verified
        $user->status = 1;
        $user->save();

       // Return the frontend login URL
        return response()->json(['login_url' => 'http://localhost:8100/login']);
    }

Please or to participate in this conversation.