Jake Fairbairn
Jake Fairbairn

Reputation: 223

Cookies or Session on Google Apps Script Webservice

I've created websites before where the server-side code has access to either Cookies on the browser or some sort of Session variables. I have not figured out how to do this with GAS. I can host a website with GAS, but I have no way of seeing the login session when a new page is loaded. How do I do this?

I would expect to see this information in the doGet() event, like this:

function doGet(e){
  e.session.userid; //or something like this
}

NOTE: Anyone can access my site, even anonymous. So I can't rely on Google logins.

Upvotes: 2

Views: 3345

Answers (1)

Alexander Mena
Alexander Mena

Reputation: 23

Google has a Session method.

https://developers.google.com/apps-script/reference/base/session

 // Log the email address of the person running the script (visiting the page).
 var email = Session.getActiveUser().getEmail();
 Logger.log(email);

Edit:

In regard to the comment about cookie based identification.

I am no expert at this but my thought was to do something like this

Client JS

$(function(){
    var clientCookie = document.cookie
    google.script.run
        .withSuccessHandler(function(cookieString){
        if(cookieString){
            document.cookie= cookieString
        }
    })
    .craeteCookie(clientCookie) // server function to create cryptographic cookie string  
})

Server Side

function createCookie(clientCookie){
    if(clientCookie){
        //check cookie is valid
    }
    else{
        // create client cookie
    }
}

Upvotes: 1

Related Questions