Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google app script web app, how to create a login button?

I have create script backed by a Google Spreadsheet in Google Drive and published it as a web app, setting "Execute the app as: me" and "Who has access to the app: Anyone, even anonymous". The main page serves html content from the spreadsheet.

The basic functionalities are: a "random internet user" is able to see some informations, while an user logged with his Google Account is able to submit information as well.

The issues is - if I set the permission to "anyone" people are first redirected to the standard google login page, and once logged in have to accept the script permissions. Once that's done, navigating to the web app will give the "logged in user" version as long as you are logged in with your google account.

However - if the web app is set to allow "anonymous users" there doesn't seem to be any (at least obvious way) to allow anonymous users to login and accept the app permissions.

What I have tried until now:

  • a link to the url google redirects me to if I am not logged on if the web app require the user to be logged in. That works, but I feel it is not the right way and also, once logged in, there is no way to logout or to switch account; it seems that once you are logged in, you are logged in forever
  • to use https://developers.google.com/apps-script/reference/script/authorization-info#getAuthorizationUrl() to get the auth url - but I don't know how to use the url it returns (navigating the user to that page in the web app frame on the top frame both result in a blank page)

How can I get a login (and possibly a switch login) url to allow anonymous users to login in my web app?

like image 451
thedayofcondor Avatar asked Nov 09 '22 21:11

thedayofcondor


1 Answers

Created sample code for 2 .gs web app projects: one - no auth, runs as owner, anonymous can access two - needs auth, runs as user, anyone can access

...as I have no idea what your "I got it to work redirecting the user to the URL" statement looks like in code.

The 1st file has a button with a click listener to act as the login button, but it's just calling for a window.top.location.href change. The 2nd app is using ScriptApp.invalidateAuth(); to log users out after being called from a client-side button click.

Demo - 1st web app url

I'd still vote to just offer a 2nd web app as a link that pops open in a new window though and as I note in the comment, the setup above forces users to re-authorize each time and after the signout.

like image 122
Bryan P Avatar answered Nov 14 '22 23:11

Bryan P