Registration, Login and Forgot Password using Google Spreadsheet

I am a beginner and I need your help.
I created a system: registration, login and forgotten password for applications using Apps Script. User data will be stored in Google Spreadsheet.
However, there was a problem.
Script (in Apps Script) and block designer as shown in the following image:
Please help me..







Block Login_1




I have made this a public topic so that others can help/learn.

You do not say what the problem is or when it occurs...

The problem that occurred was: When I tried to fill in the initial data for registration for the first time and I clicked on registration, the notification Email sudah ada ("Email already exists") appeared. Meanwhile, the Google spreadsheet is empty.
I did the above experiment repeatedly, the results were the same. As shown in the following two pictures:


Thank you very much for your attention

It may be much easier to use virtual screens?

See my example here:

Let me see what I can come up with for you.....

OK, I'll try it first, thank you.... :pray: :pray: :pray:

In the initialize global script url to abfuscated text block, I filled it in as shown in the following image:

in the block procedure (login and register) it says http://localhost/.

Will it be kept like that or replaced?

generally to access a webpage stored in the asset we use this path: http://localhost/YOURFILENAME.html

and some advice: never store a password as plain text in any database, always store the hash value of the password... there are several extensions around, which can provide hash values, for example my tools extension App Inventor Extensions: Tools | Pura Vida Apps

Taifun

Thank you very much Mr. Taifun for the advice and guidance

Try this:

GSRegLogPassBlank.aia (13.0 KB)

Uses a javascript function to generate the hmacsha256 hash on the app side.

Only downside is that the reset password is sent in plain text in the email...

There are better password generators out there...

SCRIPT
function doPost(e) {

var ss = SpreadsheetApp.openById('YOUR SPREADSHEET ID HERE');
var sh = ss.getSheetByName("YOUR SHEET NAME HERE");
var msg;

if (e.parameter.fn == "getusers" ) {
   var loginData = sh.getDataRange().getValues();
   var users = [];
   for ( var i = 1; i < loginData.length; i++ ) {
   users.push(loginData[i][0]);
   }
     msg = JSON.stringify(users);
 }
else if ( e.parameter.fn == "register" ) {
    sh.appendRow([e.parameter.email,e.parameter.pass]);
    msg = "user registered";  
  }
else if ( e.parameter.fn == "login" ) {
  var loginData = sh.getDataRange().getValues();
   for ( var i = 0; i < loginData.length; i++ ) {
     if ( e.parameter.email == loginData[i][0] && e.parameter.pass == loginData[i][1] ) {
       msg = "user logged in";
     }
   }
}
else if ( e.parameter.fn == "reset" ) {
  var newpass = makePasswd();
  var newHash = makeHash(newpass,e.parameter.key);
  //change hash for email in sheet
  var loginData = sh.getDataRange().getValues();
   for ( var i = 0; i < loginData.length; i++ ) {
     if ( e.parameter.email == loginData[i][0]) {
      var rngp = sh.getRange(i+1,2);
      rngp.setValue(newHash);
       msg = "password reset";
     }
   }
  //send email with new password
  GmailApp.sendEmail(e.parameter.email,"From AI2 App","Your new password is: " + newpass + "\n\n Use this new password to login.\n\n Thank you.");
}
return ContentService.createTextOutput(msg);

}

function makePasswd() {
  var passwd = '';
  var chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!.#*@_';
  for (i=1;i<8;i++) {
    var c = Math.floor(Math.random()*chars.length + 1);
    passwd += chars.charAt(c)
  }
  return passwd;
}

function makeHash(message,key){
  var getHash = Utilities.computeHmacSha256Signature(message,key).reduce(function(str,chr){
  chr = (chr < 0 ? chr + 256 : chr).toString(16);
  return str + (chr.length==1?'0':'') + chr;
  },'');;
  return getHash;
}
BLOCKS

SPREADSHEET

Thank you very much Mr/Mrs.
I will try, and because I am still a beginner, please help if I encounter obstacles. My hope is that Mr will be willing to help.

Thank you very much Mr/Mrs.
I have tried it, resetting the password also works well.


You expected a list back from the web, but you got an error message (<DOCTYPE...)

Show the complete web response to know what the error was.

Taifun

here's the script i am using below

function doPost(e) {
  var ss = SpreadsheetApp.openById('13FUnFMaNA-8kmLlIg06rlWw3IfXjqLV4a2x6c9wIl2g');
  var sh = ss.getSheetByName("Sheet1");
  var msg;

  if (e.parameter.fn == "getusers" ) {
    var loginData = sh.getDataRange().getValues();
    var users = [];
    for ( var i = 1; i < loginData.length; i++ ) {
      users.push(loginData[i][0]);
    }
    msg = JSON.stringify(users);
  }
  else if ( e.parameter.fn == "register" ) {
    sh.appendRow([e.parameter.email,e.parameter.pass]);
    msg = "user registered";  
  }
  else if ( e.parameter.fn == "login" ) {
    var loginData = sh.getDataRange().getValues();
    for ( var i = 0; i < loginData.length; i++ ) {
      if ( e.parameter.email == loginData[i][0] && e.parameter.pass == loginData[i][1] ) {
        msg = "user logged in";
      }
    }
  }
  else if ( e.parameter.fn == "reset" ) {
    var newpass = makePasswd();
    var newHash = makeHash(newpass,e.parameter.key);
    //change hash for email in sheet
    var loginData = sh.getDataRange().getValues();
    for ( var i = 0; i < loginData.length; i++ ) {
      if ( e.parameter.email == loginData[i][0]) {
        var rngp = sh.getRange(i+1,2);
        rngp.setValue(newHash);
        msg = "password reset";
      }
    }
    //send email with new password
    GmailApp.sendEmail(e.parameter.email,"From AI2 App","Your new password is: " + newpass + "\n\n Use this new password to login.\n\n Thank you.");
  }
  return ContentService.createTextOutput(msg);
}



function makePasswd() {
  var passwd = '';
  var chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!.#*@_';
  for (i=1;i<8;i++) {
    var c = Math.floor(Math.random()*chars.length + 1);
    passwd += chars.charAt(c)
  }
  return passwd;
}

function makeHash(message,key){
  var getHash = Utilities.computeHmacSha256Signature(message,key).reduce(function(str,chr){
    chr = (chr < 0 ? chr + 256 : chr).toString(16);
    return str + (chr.length==1?'0':'') + chr;
  },'');;
  return getHash;
}

here's the weblink i tried running , please help me, what to do, can we contact via google meet?

Well, you have a doPost(e) in your script, and you appear to be calling a doGet(e) ? (this will be the case if you run the script in your browser).

You do not show your relevant blocks...

Hey, i am using the same blocks that you have shared. i downloaded your project to test out how it works.

Can u please tell me how to solve the issue i am encountering?