1
votes

Tried hours on this one now - is there any way to add a parameter to the URL on server side Google Script in a deployed web app, that is open for anyone (not only Google accounts)?

I want to store a key for when a user is logged in, so that the user doesn't have to log in again each time the browser is refreshed.. Say my current url is: https://script.google.com/a/macros.../exec

Can I in some way add in a GAS function at login so that the URL gets modified to something like https://script.google.com/a/macros.../exec?key=abcdef&name=John%20Doe

So that if the doGet(e) function runs again (website reloaded) and if the temporary generated key matches the username in my database, the user will be directed to page B, and if the key doesn't match (or have been deleted on a time trigger) the user will be directed to page A (the login page)?

I also tried to find ways of putting something in the local cache but can't figure that out either.. Google CacheService seem to only store keys on me as a user and not client side.

Example of what I want to achieve:

  function doGet(e){
    var name = e.parameter.name, output;
    if(name === undefined){
      /*/
      Add parameter name=john to URL.. 
      obviously what I intend to is to create a HtmlOutput on a login-page
      and then in a later (function login()) add this parameter there and not
      here, but if anyone can work this out for me I got the solution
     /*/
     } else {
     output = HtmlService.createHtmlOutput('Welcome back ' + name);
     }
    return output;
    }
1
I think you might want to look at PropertyService - Cooper
@Cooper thank you I will try this as soon as I can - been trying to google it but can't find a comparison on PropertiesService and CacheService - both have userCache /userProperties but when I used it on CacheService the userCache acted as the executer of the script (me) as that is how my web app are setup - even though the App is deployed so "Anyone, even anonymous" have access to it.. Maybe I should set the web app to act as "User accessing the script"..? - JohnAl
Tried PropertiesService and it acts as CacheService even on setUserProperties, probably as the script executes as myself (script owner).. - JohnAl
Have you tried this solution? - Brian

1 Answers

0
votes

Here is a possible solution that saves usernames to the PropertiesService based on the URL parameters provided, and if already exists, gets the key.

function doGet(e) {
  //use the session service to get the active users email
  var currentUser = e.parameter.name;

  var key = e.parameter.key;

  var props = PropertiesService.getUserProperties().getProperty(currentUser);

  if(props) {
    //do something with the key
  } else {
    //if there is no username key in the properties service, then add it
    PropertiesService.getUserProperties().setProperty(currentUser, key);
  }
}

Here are the docs:

https://developers.google.com/apps-script/reference/properties/properties-service#getUserProperties()