How to Create a protected web app - Apps Script?
Securing Your Web Application with Google Apps Script: A Step-by-Step Guide.
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:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<center>
<h1>Index</h1>
After successful login.
</center>
</body>
</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>