To send an OTP (One-Time Password) from a Vue 3 application, you typically need to follow these steps:
- Generate the OTP on the server-side: This ensures that the OTP is secure and can be validated properly.
- Send the OTP to the user: This can be done via email or SMS.
- Create a form in Vue 3 to accept the OTP: This allows the user to input the OTP they received.
- 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.