UiApp: Persistent login for non-gmail users

2019-07-25 01:05发布

问题:

I am attempting to implement a Google Apps Script web service which requires users to log in using an account set up on our system.

The users will not necessarily have a gmail account, and should not be required to create one.

The web service must run using as the script owner, as it is necessary for it to be able to write to a spreadsheet and other resources which do not have shared write permission.

I have managed to implement the login screen, with reasonably strong security -- but the problem I encounter now is that users must log back in every time they visit, and even if they hit the refresh button.

Any ideas on how to implement this? Is there some way to store a cookie in the users browser, containing a session id? Or is there some other method which can work?

Thanks in advance!

Josh

回答1:

This is a very old post but as there is a solution, I think it is better to show it to help people with a similar need

Hi Josh,

I have developed such a system and there is indeed a way to do this.

You can indeed develop a cookie like system that is using the PrivateCache class: CacheService.getPrivateCache().

It works if the user reload the page or close it.

However with this solution when you close your browser it will not be possible to retrieve the information anymore.

Here are the functions that I use to prevent the problem you have underlined

Feel free to adapt them

function getCookie(){
  var cache=CacheService.getPrivateCache();
  var cached=cache.get("UserCookie");
  if(cached!=null){
    return Utilities.jsonParse(cached);
  }
  return -1;
}

function createCookie(data){
  var cache=CacheService.getPrivateCache();
  cache.put("UserCookie",Utilities.jsonStringify(data),1800); 
}

function removeCookie(){
  var cache=CacheService.getPrivateCache();
  cache.remove("UserCookie"); 
}

Another way would be to use UserProperties. In this case it will work even if you close your browser... I just tried it

the functions to use are therefore:

function getCookie(){
  var cached=UserProperties.getProperty('UserCookie');
  if(cached!=null){
    return Utilities.jsonParse(cached);
  }
  return -1;
}

function createCookie(data){
    UserProperties.setProperty('UserCookie',Utilities.jsonStringify(data));
}

function removeCookie(){
  UserProperties.deleteProperty("UserCookie"); 
}

I hope it will help anyone...

Cheers

Nicolas



回答2:

Persistent login are not possible with Apps Script as Apps Script can not interact with browser objects like cookies etc. Apps Script is intended to work only with Google Accounts.