-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Authentication tutorial #9179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
shannonbux
merged 15 commits into
gatsbyjs:master
from
ManoelLobo:docs/authentication-tutorial
Oct 24, 2018
Merged
Authentication tutorial #9179
Changes from 2 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c1db8eb
Starting Authentication tutorial
ManoelLobo f612e04
WIP: First sections of tutorial
ManoelLobo 6ceecf6
WIP: Added initial setup & introduced some concepts
ManoelLobo 948e06c
WIP: Private routes
ManoelLobo ddcd53c
First full draft
ManoelLobo 7ff7c12
A few wording changes
shannonbux 1a8725f
one missing letter
shannonbux 5fc79aa
two punctuation edits
shannonbux 5dd4298
Revision - after feedback
ManoelLobo 39920ad
Fixed code snippet
ManoelLobo 61ecd88
Better intro to files and changes
ManoelLobo 49a73e0
Use localStorage API methods instead of change properties directly
ManoelLobo 179c0ce
Fixed login component - missing code
ManoelLobo e67157c
More references for further reading
ManoelLobo 2ed4e2b
Fixed lines changed - highlight
ManoelLobo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,100 @@ | |
| title: Making a site with user authentication | ||
| --- | ||
|
|
||
| This is a stub. Help our community expand it. | ||
| Sometimes, you need to create a site with gated content, available only to authenticated users. Using Gatsby, you may achieve this using the concept of [client-only routes](https://www.gatsbyjs.org/docs/building-apps-with-gatsby/#client-only-routes),to define which pages an user can view only after logging in. | ||
|
|
||
| Please use the [Gatsby Style Guide](/docs/gatsby-style-guide/) to ensure your | ||
| pull request gets accepted. | ||
| # Prerequisites | ||
|
|
||
| You should have already configured your environment to be able to use the gatsby-cli. A good starting point is the [main tutorial](https://www.gatsbyjs.org/tutorial/) | ||
|
|
||
| # Security notice | ||
|
|
||
| In production, you should use a tested and robust solution to handle the authentication. [Auth0](https://www.auth0.com), [Firebase](https://firebase.google.com), and [Passport.js](passportjs.org) are good examples. This tutorial will only cover the authentication workflow, but you should take the security of your app as seriously as possible. | ||
|
|
||
| # Building your Gatsby app | ||
|
|
||
| Start by creating a new Gatsby project: | ||
|
|
||
| ```shell | ||
| gatsby new gatsby-auth | ||
| cd gatsby-auth | ||
| ``` | ||
|
|
||
| Then, add a more apt title to your newly created site, changing the content of `gatsby-config.js`: | ||
|
|
||
| ```javascript:title=gatsby-config.js | ||
| module.exports = { | ||
| siteMetadata: { | ||
| title: "Gatsby Authentication Tutorial", | ||
| }, | ||
| plugins: ["gatsby-plugin-react-helmet", "gatsby-plugin-offline"], | ||
| } | ||
| ``` | ||
|
|
||
| ## Authentication service | ||
|
|
||
| For this tutorial you will use a hardcode user/password. Create the folder `src/services` and add the follwing content to the file `auth.js`: | ||
|
|
||
| ```javascript:title=src/services/auth.js | ||
| export const getUser = () => | ||
| window.localStorage.gatsbyUser | ||
| ? JSON.parse(window.localStorage.gatsbyUser) | ||
| : {} | ||
|
|
||
| const setUser = user => (window.localStorage.gatsbyUser = JSON.stringify(user)) | ||
|
|
||
| export const handleLogin = ({ username, password }) => { | ||
| if (username === `john` && password === `pass`) { | ||
| return setUser({ | ||
| username: `john`, | ||
| name: `Johnny`, | ||
| email: `[email protected]`, | ||
| }) | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| export const isLoggedIn = () => { | ||
| const user = getUser() | ||
|
|
||
| return !!user.username | ||
| } | ||
|
|
||
| export const logout = callback => { | ||
| setUser({}) | ||
| callback() | ||
| } | ||
| ``` | ||
|
|
||
| ## Creating client-only routes | ||
|
|
||
| Up until now, you created a common Gatsby site. But, using the [@reach/router](https://reach.tech/router/) library, you can create routes available only to logged-in users. This library is used by Gatsby under the hood, so you don't even have to install it. | ||
|
|
||
| First, edit `gatsby-node.js`. You will define that any route that starts with `/app/` is part of your restricted content and the page will be created on demand: | ||
|
|
||
| ```javascript:title=gatsby-config.js | ||
| // Implement the Gatsby API “onCreatePage”. This is | ||
| // called after every page is created. | ||
| exports.onCreatePage = async ({ page, actions }) => { | ||
| const { createPage } = actions | ||
|
|
||
| // page.matchPath is a special key that's used for matching pages | ||
| // only on the client. | ||
| if (page.path.match(/^\/app/)) { | ||
| page.matchPath = "/app/*" | ||
|
|
||
| // Update the page. | ||
| createPage(page) | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| WIP | ||
|
|
||
| # Further reading | ||
|
|
||
| If you want to learn more about using production-ready auth solutions, these links may help: | ||
|
|
||
| - [Building a blog with Gatsby, React and Webtask.io!](https://auth0.com/blog/building-a-blog-with-gatsby-react-and-webtask/) | ||
| - [JAMstack PWA — Let’s Build a Polling App. with Gatsby.js, Firebase, and Styled-components Pt. 2](https://medium.com/@UnicornAgency/jamstack-pwa-lets-build-a-polling-app-with-gatsby-js-firebase-and-styled-components-pt-2-9044534ea6bc) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.