1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| function doPost(e) { var action = e.parameter.action; var email = e.parameter.email; var username = e.parameter.username; var password = e.parameter.password; var newPassword = e.parameter.newPassword; var token=e.parameter.token;
try { if (action == "register") { return registerUser(email, username, password); } else if (action == "login") { return loginUser(username, password); } else if (action == "forgotPassword") { return forgotPassword(email); } else if (action == "updatePassword") { return updatePassword(username,token, newPassword); } else { return ContentService.createTextOutput(JSON.stringify({ status: "error", message: "Invalid action" })).setMimeType(ContentService.MimeType.JSON); } } catch (error) { return ContentService.createTextOutput(JSON.stringify({ status: "error", message: error.message })).setMimeType(ContentService.MimeType.JSON); } }
function registerUser(email, username, password) { var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Users'); var data = sheet.getDataRange().getValues();
for (var i = 0; i < data.length; i++) { if (data[i][0] == email) { return ContentService.createTextOutput(JSON.stringify({ status: "error", message: "Email already registered" })).setMimeType(ContentService.MimeType.JSON); } }
sheet.appendRow([email, username, password]); return ContentService.createTextOutput(JSON.stringify({ status: "success", message: "User registered successfully" })).setMimeType(ContentService.MimeType.JSON); }
function loginUser(username, password) { var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Users'); var data = sheet.getDataRange().getValues();
for (var i = 0; i < data.length; i++) { if (data[i][1] == username && data[i][2] == password) { return ContentService.createTextOutput(JSON.stringify({ status: "success", message: "Login successful" })).setMimeType(ContentService.MimeType.JSON); } }
return ContentService.createTextOutput(JSON.stringify({ status: "error", message: "Invalid username or password" })).setMimeType(ContentService.MimeType.JSON); }
function updatePassword(email, token, newPassword) { var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('ResetTokens'); var data = sheet.getDataRange().getValues(); var now = new Date().getTime(); var tokenValid = false;
for (var i = 0; i < data.length; i++) { if (data[i][0] == email && data[i][1] == token) { var timestamp = data[i][2]; if (now - timestamp <= 24 * 60 * 60 * 1000) { tokenValid = true; sheet.getRange(i+1, 3).setValue(0); break; } } }
if (tokenValid) { var userSheet =SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Users'); var userData = userSheet.getDataRange().getValues();
for (var j = 0; j < userData.length; j++) { if (userData[j][0] == email) { userSheet.getRange(j + 1, 3).setValue(newPassword); return ContentService.createTextOutput(JSON.stringify({ status: "success", message: "Password updated successfully" })).setMimeType(ContentService.MimeType.JSON); } } }
return ContentService.createTextOutput(JSON.stringify({ status: "error", message: "Invalid token or email" })).setMimeType(ContentService.MimeType.JSON); } function forgotPassword(email) { var sheet =SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Users'); var data = sheet.getDataRange().getValues();
for (var i = 0; i < data.length; i++) { if (data[i][0] == email) { var token = generateToken();
storeResetToken(email, token);
sendResetEmail(email, token);
return ContentService.createTextOutput(JSON.stringify({ status: "success", message: "Password reset email sent" })).setMimeType(ContentService.MimeType.JSON); } }
return ContentService.createTextOutput(JSON.stringify({ status: "error", message: "Email not found" })).setMimeType(ContentService.MimeType.JSON); }
function generateToken() { var charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; var token = ''; for (var i = 0; i < 20; i++) { var randomIndex = Math.floor(Math.random() * charset.length); token += charset[randomIndex]; } return token; }
function storeResetToken(email, token) { var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('ResetTokens'); var timestamp = new Date().getTime(); sheet.appendRow([email, token, timestamp]); }
function sendResetEmail(email, resetUrl) { var subject = 'Password Reset Request'; var body = 'You requested a password reset. Click the link below to reset your password:\n\n' + resetUrl + '\n\nIf you did not request a password reset, please ignore this email.'; GmailApp.sendEmail(email, subject, body); }
|