schibsted / account-sdk-browser

Schibsted Account SDK for browsers
https://schibsted.github.io/account-sdk-browser/
MIT License
16 stars 11 forks source link

Varnish cookie won't be set #22

Closed fredrikstave closed 6 years ago

fredrikstave commented 6 years ago

Hi!

I have set up a demo app to debug an issue I have with the SDK. The Varnish cookie won't be set, even though I'm calling the enableVarnishCookie function on the Identity instance.

When a user I log in with my own user, I'm registered as logged in, the local storage data it set correctly, but if I check the Cookies section in Google Chrome I can't see the cookie being set.

Can anyone help me with this?

Local storage skjermbilde 2018-04-19 kl 11 52 55

Cookies skjermbilde 2018-04-19 kl 11 53 05

React implementation

import React, { Component } from 'react';
import Identity from '@schibsted/account-sdk-browser/identity';

const identity = new Identity({
  clientId: 'xxx',
  redirectUri: 'http://localhost:3000',
  env: 'PRE',
  log: console.log,
});

identity.enableVarnishCookie();

class App extends Component {
  constructor(props) {
    super(props);

    this.state = {
      loggedIn: false,
      username: null,
    };
  }
  componentWillMount() {
    identity.hasSession()
      .then((res) => {
        if (res.result) {
          this.setState({
            loggedIn: true,
            username: res.displayName || 'Unknown',
          });
        }
      })
      .catch(() => {
        this.setState({
          loggedIn: false,
          username: null,
        })
      });
  }
  render() {
    if (this.state.loggedIn) {
      return (
        <p>
          Logged in as {this.state.username}.
          <button onClick={() => identity.logout()}>Logg ut</button>
        </p>
      );
    } else {
      return (
        <button onClick={() => identity.login({ newFlow: false, state: 'demo' })}>Logg inn</button>
      );
    }
  }
}

export default App;
fredrikstave commented 6 years ago

I found out that this problem only (*) occurs on localhost. Changing the host to a regular domain like e24.local, resolves the issue. If this is expected behavior, it should be clearly stated in the docs (especially now when GDPR is getting ever closer 😉).