1

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;
    }
JohnAl
  • 113
  • 1
  • 11
  • I think you might want to look at [PropertyService](https://developers.google.com/apps-script/reference/properties/properties-service) – Cooper May 15 '17 at 02:05
  • @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 May 15 '17 at 05:33
  • Tried PropertiesService and it acts as CacheService even on setUserProperties, probably as the script executes as myself (script owner).. – JohnAl May 15 '17 at 08:13
  • Have you tried [this solution](http://stackoverflow.com/questions/15668119/linking-to-another-html-page-in-google-apps-script/16697525#16697525)? – Brian May 15 '17 at 11:30

1 Answers1

0

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()

Jordan Rhea
  • 1,186
  • 15
  • 34
  • The webapp is open for anyone, so it's not only signed in Google customers accessing the webapp but anyone.. So unfortunately won't this do either =/ – JohnAl May 15 '17 at 08:10
  • I see. I changed the code above to reflect using the name parameter provided in the query string to the doGet function. Does that help? – Jordan Rhea May 15 '17 at 08:18
  • The URL doesnt have any parameters from start, what I want to achieve is to programatically add a parameter to the URL in a later function.. I'm a bit new on Stackoverflow but will edit my original post to include some code to show.. give me 5 mins – JohnAl May 15 '17 at 09:17