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

CrastyCrap's avatar

Sending otp

Hello, what is the best way to send otp from vue 3 to user phone

0 likes
3 replies
LaryAI's avatar
Level 58

To send an OTP (One-Time Password) from a Vue 3 application, you typically need to follow these steps:

  1. Generate the OTP on the server-side: This ensures that the OTP is secure and can be validated properly.
  2. Send the OTP to the user: This can be done via email or SMS.
  3. Create a form in Vue 3 to accept the OTP: This allows the user to input the OTP they received.
  4. Validate the OTP on the server-side: This ensures that the OTP entered by the user is correct.

Here's a step-by-step guide with code examples:

Step 1: Generate the OTP on the Server-Side

Assuming you have a backend server (e.g., Node.js with Express), you can generate an OTP and send it to the user.

// server.js (Node.js with Express)
const express = require('express');
const app = express();
const nodemailer = require('nodemailer');
const bodyParser = require('body-parser');
const crypto = require('crypto');

app.use(bodyParser.json());

let otpStore = {}; // In-memory store for OTPs

app.post('/send-otp', (req, res) => {
    const { email } = req.body;
    const otp = crypto.randomInt(100000, 999999).toString(); // Generate a 6-digit OTP
    otpStore[email] = otp;

    // Send OTP via email (using nodemailer)
    const transporter = nodemailer.createTransport({
        service: 'gmail',
        auth: {
            user: '[email protected]',
            pass: 'your-email-password'
        }
    });

    const mailOptions = {
        from: '[email protected]',
        to: email,
        subject: 'Your OTP Code',
        text: `Your OTP code is ${otp}`
    };

    transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
            return res.status(500).send('Error sending OTP');
        }
        res.status(200).send('OTP sent');
    });
});

app.post('/verify-otp', (req, res) => {
    const { email, otp } = req.body;
    if (otpStore[email] === otp) {
        delete otpStore[email]; // OTP is valid, remove it from the store
        res.status(200).send('OTP verified');
    } else {
        res.status(400).send('Invalid OTP');
    }
});

app.listen(3000, () => {
    console.log('Server running on port 3000');
});

Step 2: Create a Form in Vue 3 to Accept the OTP

<!-- OTPForm.vue -->
<template>
  <div>
    <form @submit.prevent="sendOtp">
      <input v-model="email" type="email" placeholder="Enter your email" required />
      <button type="submit">Send OTP</button>
    </form>

    <form v-if="otpSent" @submit.prevent="verifyOtp">
      <input v-model="otp" type="text" placeholder="Enter OTP" required />
      <button type="submit">Verify OTP</button>
    </form>

    <p v-if="message">{{ message }}</p>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      email: '',
      otp: '',
      otpSent: false,
      message: ''
    };
  },
  methods: {
    async sendOtp() {
      try {
        const response = await axios.post('http://localhost:3000/send-otp', { email: this.email });
        this.otpSent = true;
        this.message = response.data;
      } catch (error) {
        this.message = 'Error sending OTP';
      }
    },
    async verifyOtp() {
      try {
        const response = await axios.post('http://localhost:3000/verify-otp', { email: this.email, otp: this.otp });
        this.message = response.data;
      } catch (error) {
        this.message = 'Invalid OTP';
      }
    }
  }
};
</script>

Step 3: Validate the OTP on the Server-Side

This is already handled in the /verify-otp endpoint in the server code provided above.

Summary

  • Generate the OTP on the server-side.
  • Send the OTP to the user via email or SMS.
  • Create a Vue 3 form to accept the OTP.
  • Validate the OTP on the server-side.

This approach ensures that the OTP is securely generated and validated, and provides a user-friendly interface for entering the OTP.

Snapey's avatar

send it from the server using an sms provider

Please or to participate in this conversation.