question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Page refresh drops currentUser() using GoTrue with React (Gatsby)

See original GitHub issue

Hi all, I’m trying to set up GoTrue with Gatsby instead of using the Netlify Identity Widget so that I can build my own custom components.

Everything seems to be working fine so long as I only click <Link />s, but if I refresh a page or type a new in-site URL then auth.currentUser() returns undefined, even though I’ve set setCookie to true.

My src/utils/auth.js file looks like this:

import GoTrue from 'gotrue-js';

const auth = new GoTrue({
  APIUrl: "https://MY_URL/.netlify/identity",
  setCookie: true,
});

export default auth;

My Navbar component has the line import auth from '../../utils/auth'; and in its render() function I have the line console.log(auth.currentUser());. Right after a successful login, I see the user object printed correctly into the console, but as soon as I refresh it shows undefined.

When I push my site to Netlify, the build fails with the following error in the Deploy Log:

212 |       var json = localStorage.getItem(storageKey);
...

WebpackError: localStorage is not defined

- user.js?1d1d:212 Function.recoverSession
  ~/gotrue-js/lib/user.js?1d1d:212:1

- index.js?2889:158 GoTrue.currentUser
  ~/gotrue-js/lib/index.js?2889:158:1

...

This seems to be directly related, since reviving an existing session is where GoTrue seems to be having trouble.

Thanks for your help!

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
TomPichaudcommented, Oct 5, 2018

This is working just fine for me.

Must set the reminder to true on login (not referenced in the doc).

import React, { Component } from "react"
import GoTrue from "gotrue-js"

// Instantiate the GoTrue auth client with an optional configuration
let auth = new GoTrue({
  APIUrl: "https://YOURS/.netlify/identity",
  audience: "",
  setCookie: true
})

export default class extends Component {
  handleLogin = () => {
    auth
      .login("demo@demo.me", "demo", true)
      .then(response => {
        alert("Success!")
        console.log(JSON.stringify({ response }))
      })
      .catch(error => {
        alert("Failed...")
        console.log(JSON.stringify({ response }))
      })
  }
  render() {
    let user = auth.currentUser()
    console.log(user)
    return (
      <>
        <button onClick={this.handleLogin}>Login</button>
      </>
    )
  }
}

But I’m a bit confused about how this API handles it…

If login reminder is set to false the user would not be saved for the session. If cookie is set to true and login reminder is set to true the user would be stored in a cookie. If cookie is set to false and login reminder is set to true the user would be stored in a sessionStorage.

I’ve made the fix, but the doc need to be updated.

2reactions
Rezicommented, Nov 3, 2019

@TomPichaud 's advice with reminder flag worked for me for a Sapper app. Unfortunately the docs are still not updated year after and there are many pull requests pending. Shame on netlify for such a poor support for one of its business products.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Navigate to page based on useEffect dependency
Just use the useEffect with an empty dependency. ... Here's the solution: Netlify's gotrue-js will return null for currentUser() if there is ...
Read more >
Fast Refresh | Gatsby
With Fast Refresh, changes to the code for your React components immediately update in the browser, without losing component state. If you have...
Read more >
react-netlify-identity-gotrue - npm
Welcome to the fully-featured, easy to use Netlify Identity integration for React, built purely in React without any dependencies. NPM. Install.
Read more >
Create a static website with authentication tutorial with React ...
React is in fact a much simpler tool to deal with dynamic ... will listen if the current user has changed and updates...
Read more >
Build a Secure Blog with Gatsby, React, and Netlify
Create a page component for the Markdown files; Create static pages using Gatsby's Node.js createPage() API. Install a couple of Gatsby plugins ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found