@devvai/devv-code-backend
Version:
Backend SDK for Devv Code - Provides authentication, data management, email and AI capabilities
71 lines (70 loc) • 2.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DevvAuth = void 0;
const device_1 = require("./device");
const session_1 = require("./session");
const constants_1 = require("./constants");
class DevvAuth {
async sendOTP(email) {
const deviceId = (0, device_1.getEncryptedDeviceId)();
const response = await fetch(`${constants_1.BASE_URL}api/v1/project-auth/send-verification-email`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Device-Id': deviceId
},
body: JSON.stringify({ email })
});
if (!response.ok) {
const error = await response.json().catch(() => ({ error: 'Send OTP failed' }));
throw new Error(error.error || `Failed to send OTP (Status: ${response.status})`);
}
}
async verifyOTP(email, verificationCode) {
const deviceId = (0, device_1.getEncryptedDeviceId)();
const response = await fetch(`${constants_1.BASE_URL}api/v1/project-auth/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Device-Id': deviceId
},
body: JSON.stringify({
authType: 8,
email,
verificationCode
})
});
if (!response.ok) {
const error = await response.json().catch(() => ({ error: 'Login failed' }));
throw new Error(error.error || `Invalid verification code (Status: ${response.status})`);
}
const data = await response.json();
// Store the session ID if present
if (data.sid) {
(0, session_1.setSid)(data.sid);
}
return data;
}
async logout() {
const deviceId = (0, device_1.getEncryptedDeviceId)();
const sid = (0, session_1.getSid)();
const headers = {
'Content-Type': 'application/json',
'Device-Id': deviceId
};
if (sid) {
headers['sid'] = sid;
}
const response = await fetch(`${constants_1.BASE_URL}api/v1/project-auth/logout`, {
method: 'POST',
headers
});
if (!response.ok) {
const error = await response.json().catch(() => ({ error: 'Logout failed' }));
throw new Error(error.error || `Logout failed (Status: ${response.status})`);
}
// Clear the session ID on successful logout
(0, session_1.clearSid)();
}
}
exports.DevvAuth = DevvAuth;