Login through Email - Google web App

 How to Create a protected web app - Apps Script?

Securing Your Web Application with Google Apps Script: A Step-by-Step Guide.

Google Apps Script is a cloud-based scripting language that provides easy ways to automate tasks across Google products and third party services. In this blog, we will be taking a closer look at a code written in Google Apps Script that checks the permission of a user to access a web application.

The code starts by opening a Google Spreadsheet using its unique ID and getting its sheet named 'Users'.

The doGet() function is a trigger that is automatically executed when the web application is accessed. The function starts by getting the username and email of the active user using the Session.getActiveUser() method. Then, the function calls the checkUser() function and passes the email of the active user as a parameter.

The checkUser() function checks if the email of the active user exists in the first column of the 'Users' sheet. If the email is found, the function returns 'true' and the user is allowed to access the web application. On the other hand, if the email is not found, the function returns 'false' and the user is not allowed to access the web application.

The doGet() function then evaluates the permission of the user by checking the value returned by the checkUser() function. If the permission is 'true', a template file named 'index' is evaluated and the user's username is passed as a parameter to the template. If the permission is 'false', a template file named 'fail' is evaluated and a message indicating that the user has no permission to access the web application is passed as a parameter to the template.

In conclusion, the code demonstrated in this blog provides a way to check the permission of a user to access a web application. The code makes use of the Google Apps Script, Google Spreadsheets, and the Session.getActiveUser() method to achieve this.

Describe files are below:

Code.gs
var ss = SpreadsheetApp.openById(' spreadsheet id ') var ws = ss.getSheetByName('Users'); function doGet(e) { var uN = Session.getActiveUser().getUsername(); var user = Session.getActiveUser().getEmail(); var uCheck = checkUser(user); if(uCheck=='true'){ var html = HtmlService.createTemplateFromFile('index'); html.inf = uN; return html.evaluate(); }else{var html = HtmlService.createTemplateFromFile('fail'); html.inf = 'Dear '+uN+', You have no permission to access this webapp.'; return html.evaluate(); }; } function checkUser(user){ var lr = ws.getLastRow(); var flag = 1 ; for(var i = 1;i<=lr;i++){ if(user == ws.getRange(i, 1).getValue()){ flag = 0; return 'true'; }}if(flag==1){ return 'false'; }
};
index.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
   <body>
    <center>
    <h1>Index</h1>
     After successful login.
     </center>
   </body>
</html>

fail.html

<!DOCTYPE html>
<html>
<head>
    <base target="_top">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body align="center">
  <h1>Absence of permission!</h1>
   <?= inf ?>
  </body>
  </html>

---------------------------Have a nice day-----------------------

Post a Comment

Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.