Aurelia and Cookie Setting/Reading

I am trying to use the plugin: https://github.com/Vheissu/aurelia-cookie to set cookies if a user successfully logs in.

My code:

AureliaCookie.set( 'name', 'value', {
                    expiry: '24h', 
                    domain: 'http://[ip address]',
                    path: '/',
                    secure: false,
                    REMOTE_USER: data.username,
                  });

When I check the application tab in Chrome DevTools, there is no cookie set there for me to read.

So I tried setting the cookie via perl, which is my ajax login script, which comes from a different web server.

That code:

 my $cookie = $cgi->cookie( -name => 'ARIS',
                            -value => 'REMOTE_USER=' . $validUser,
                            -expires => '+24h',
                            'SameSite' => 'None',
                            -path => 'http://[ip address]/' );

I have to set SameSite, or I get the SameSite cookie error, and no cookie is set.

However, once I do that, the domain is set to [login webserver ip address] and not my actual domain that I am running Aurelia from.

My question:

Is there a way to read the cookie that I have set via perl, or is there a plugin that actually works to set and read cookies from in Aurelia?

Thanks

1 Like

Update:

I am using the javascript function: document.cookie to set the cookie, and then read the cookie to decide on whether to show the login or the main page.

It’s working, but maybe there is a better way?

1 Like

Looks okay to me. Don’t see the advantage of using a plugin just for that.

I have a little module to handle cookies in a simplistic way for a particular app:

const ExpirationDays = 28;
/*
 * General utils for managing cookies in Typescript.
 */
export function setCookie(name: string, val: string) {
  const date = new Date();
  const value = val;

  // Set it expire in 28 days
  date.setTime(date.getTime() + (ExpirationDays * 24 * 60 * 60 * 1000));

  // Set it
  document.cookie = name+"="+value+"; expires="+date.toUTCString()+"; path=/";
}

export function getCookie(name: string) {
  const value = "; " + document.cookie;
  const parts = value.split("; " + name + "=");
  
  if (parts.length == 2) {
      return parts.pop().split(";").shift();
  }
}

export function deleteCookie(name: string) {
  const date = new Date();

  // Set it expire in -1 days
  date.setTime(date.getTime() + (-1 * 24 * 60 * 60 * 1000));

  // Set it
  document.cookie = name+"=; expires="+date.toUTCString()+"; path=/";
}
1 Like

That looks great. I will be using something similar next time I have to set cookies.

So far, this one just needs to work for the time they have the browser open, and then log in again the next day.

1 Like