Skip to content

Instantly share code, notes, and snippets.

@ckenst
Last active December 5, 2025 19:58
Show Gist options
  • Select an option

  • Save ckenst/1adb00423639398691684122d2feacfc to your computer and use it in GitHub Desktop.

Select an option

Save ckenst/1adb00423639398691684122d2feacfc to your computer and use it in GitHub Desktop.
Testing Email-Based OTP Login
const { test, expect } = require(‘@playwright/test’);
const axios = require(‘axios’);
test(‘Login using OTP code via email’, async ({ page }) => {
// Generate a unique inbox name
const inbox = `testuser_${Date.now()}`;
const emailAddress = `${inbox}@your-private-domain.mailinator.com`; // replace this with your priviate domain
// Navigate to the login page and initiate login
await page.goto(‘https://yourapp.com/login’);
await page.fill(‘#email’, emailAddress);
await page.click(‘button[type=”submit”]’);
// Wait for the OTP email to arrive
const apiToken = ‘YOUR_MAILINATOR_API_TOKEN’;
const inboxResponse = await axios.get(`https://api.mailinator.com/api/v2/domains/private/inboxes/${inbox}`, {
headers: { ‘Authorization’: apiToken }
});
const messages = inboxResponse.data.msgs;
const otpEmail = messages.find(msg => msg.subject.includes(‘Your OTP Code’));
// Fetch the full message content
const messageResponse = await axios.get(`https://api.mailinator.com/api/v2/domains/private/messages/${otpEmail.id}`, {
headers: { ‘Authorization’: apiToken }
});
const otpCodeMatch = messageResponse.data.parts[0].body.match(/\d{6}/);
const otpCode = otpCodeMatch ? otpCodeMatch[0] : null;
// Enter the OTP and complete login
await page.fill(‘#otp’, otpCode);
await page.click(‘button[type=”submit”]’);
// Verify successful login
await expect(page).toHaveURL(‘https://yourapp.com/dashboard’);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment