156 lines
4.7 KiB
HTML
156 lines
4.7 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<title>Farm Auth Tester</title>
|
|
<style>
|
|
body { font-family: sans-serif; max-width: 600px; margin: 20px auto; }
|
|
h2 { margin-top: 24px; }
|
|
label { display: block; margin-top: 8px; }
|
|
input, select { width: 100%; padding: 6px; margin-top: 4px; }
|
|
button { margin-top: 10px; padding: 8px 12px; cursor: pointer; }
|
|
.section { border: 1px solid #ddd; padding: 12px; border-radius: 6px; margin-top: 16px; }
|
|
pre { background: #f7f7f7; padding: 8px; overflow-x: auto; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h1>Farm Auth Service Tester</h1>
|
|
|
|
<div class="section">
|
|
<h2>1. Request OTP</h2>
|
|
<label>Phone Number (E.164, e.g. +91XXXXXXXXXX)</label>
|
|
<input id="phone" type="text" placeholder="+91..." />
|
|
|
|
<button onclick="requestOtp()">Request OTP</button>
|
|
|
|
<pre id="requestOtpResult"></pre>
|
|
</div>
|
|
|
|
<div class="section">
|
|
<h2>2. Verify OTP</h2>
|
|
<label>OTP Code</label>
|
|
<input id="otp" type="text" placeholder="123456" />
|
|
|
|
<label>Device ID</label>
|
|
<input id="deviceId" type="text" value="web-test-device" />
|
|
|
|
<button onclick="verifyOtp()">Verify OTP</button>
|
|
|
|
<pre id="verifyOtpResult"></pre>
|
|
</div>
|
|
|
|
<div class="section">
|
|
<h2>3. Complete Profile (name + user_type)</h2>
|
|
<p>Uses the <code>access_token</code> from step 2</p>
|
|
|
|
<label>Name</label>
|
|
<input id="name" type="text" placeholder="Your name" />
|
|
|
|
<label>User Type</label>
|
|
<select id="userType">
|
|
<option value="seller">Seller</option>
|
|
<option value="buyer">Buyer</option>
|
|
<option value="service_provider">Service Provider</option>
|
|
</select>
|
|
|
|
<button onclick="updateProfile()">Update Profile</button>
|
|
|
|
<pre id="updateProfileResult"></pre>
|
|
</div>
|
|
|
|
<script>
|
|
let accessToken = null;
|
|
let refreshToken = null;
|
|
|
|
async function requestOtp() {
|
|
const phone = document.getElementById('phone').value.trim();
|
|
const out = document.getElementById('requestOtpResult');
|
|
out.textContent = 'Sending...';
|
|
|
|
try {
|
|
const res = await fetch('/auth/request-otp', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ phone_number: phone })
|
|
});
|
|
|
|
const data = await res.json();
|
|
out.textContent = JSON.stringify(data, null, 2);
|
|
} catch (err) {
|
|
out.textContent = 'Error: ' + err;
|
|
}
|
|
}
|
|
|
|
async function verifyOtp() {
|
|
const phone = document.getElementById('phone').value.trim();
|
|
const otp = document.getElementById('otp').value.trim();
|
|
const deviceId = document.getElementById('deviceId').value.trim() || 'web-test-device';
|
|
const out = document.getElementById('verifyOtpResult');
|
|
out.textContent = 'Verifying...';
|
|
|
|
try {
|
|
const res = await fetch('/auth/verify-otp', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
phone_number: phone,
|
|
code: otp,
|
|
device_id: deviceId,
|
|
device_info: {
|
|
platform: 'web',
|
|
model: 'browser',
|
|
os_version: navigator.userAgent,
|
|
app_version: 'web-test',
|
|
language_code: navigator.language,
|
|
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
|
}
|
|
})
|
|
});
|
|
|
|
const data = await res.json();
|
|
out.textContent = JSON.stringify(data, null, 2);
|
|
|
|
if (res.ok) {
|
|
accessToken = data.access_token;
|
|
refreshToken = data.refresh_token;
|
|
console.log('Access token set:', accessToken);
|
|
console.log('Refresh token set:', refreshToken);
|
|
alert('OTP verified. needs_profile = ' + data.needs_profile);
|
|
}
|
|
} catch (err) {
|
|
out.textContent = 'Error: ' + err;
|
|
}
|
|
}
|
|
|
|
async function updateProfile() {
|
|
const name = document.getElementById('name').value.trim();
|
|
const userType = document.getElementById('userType').value;
|
|
const out = document.getElementById('updateProfileResult');
|
|
|
|
if (!accessToken) {
|
|
alert('No access_token. Verify OTP first.');
|
|
return;
|
|
}
|
|
|
|
out.textContent = 'Updating profile...';
|
|
|
|
try {
|
|
const res = await fetch('/users/me', {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + accessToken,
|
|
},
|
|
body: JSON.stringify({ name, user_type: userType })
|
|
});
|
|
|
|
const data = await res.json();
|
|
out.textContent = JSON.stringify(data, null, 2);
|
|
} catch (err) {
|
|
out.textContent = 'Error: ' + err;
|
|
}
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|