My absolute favorite way to build in authentication in a React application is with the [Context API][react-context]. This enables any component nested beneath an authenticated child to be able to grab authentication data, methods, etc. in a clean, intuitive way. You better believe this works with Gatsby too; it is just a React application after all!
For these examples, presume that we have some third-party JavaScript library that implements helpful authentication features and provides API methods to check authentication status and perform logins and logouts. Of course, you can roll this yourself, as well, but I merely want to introduce the general idea! We use this library, and provide it in a utils folder like so:
import Auth from 'some-auth-library';
export default new Auth({