-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(examples): Fix functions examples + add google oauth (#31953)
Co-authored-by: LekoArts <lekoarts@gmail.com>
- Loading branch information
1 parent
6edfc22
commit 66c7b79
Showing
59 changed files
with
574 additions
and
224 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
AIRTABLE_KEY= | ||
AIRTABLE_DB= |
This file contains 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
This file contains 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
This file contains 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
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,7 +1,4 @@ | ||
module.exports = { | ||
flags: { | ||
FUNCTIONS: true, | ||
}, | ||
siteMetadata: { | ||
title: "form", | ||
}, | ||
|
This file contains 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
GATSBY_GOOGLE_CLIENT_ID= | ||
CLIENT_SECRET= |
This file contains 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
5 changes: 2 additions & 3 deletions
5
...es/functions-google-auth/gatsby-config.js → ...s/functions-google-gmail/gatsby-config.js
This file contains 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
This file contains 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
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
GOOGLE_CLIENT_ID= | ||
GOOGLE_CLIENT_SECRET= | ||
GOOGLE_REDIRECT_URI="http://localhost:8000/api/googleAccessToken" | ||
APP_REDIRECT_URI="http://localhost:8000/auth-redirect" | ||
APP_HOSTNAME="http://localhost:8000" |
This file contains 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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Gatsby Functions + Google OAuth Example | ||
|
||
This example illustrates logging in with Google 0Auth via Gatsby Functions and defining a [private client route behind this authentication](https://www.gatsbyjs.com/tutorial/authentication-tutorial/). The primary focus is on the Functions aspect, not the client side routing/authentication. [This article](https://soshace.com/react-user-login-authentication-using-usecontext-and-usereducer/#crayon-60c920bddc105062139412) has a good example for setting up the client side auth provider you could layer on top of this example. | ||
|
||
![Image of Google Auth Gatsby App](./localhost_8000.png) | ||
|
||
## 🚀 Quick start | ||
|
||
1. **Set up Google OAuth Client.** | ||
|
||
This sample uses google oAuth client to authenticate into the Google inbox api. Obtain an oAuth credential from the Google Cloud [API & Services Credentails Page](https://console.cloud.google.com/apis/credentials?) | ||
|
||
- Generate an Oauth Client ID. You can follow the steps highlighted in [Account Authentication - Service Account](https://theoephraim.github.io/node-google-spreadsheet/#/getting-started/authentication?id=oauth). Make sure to set up at least one **Authorized redirect URI** to be `http://localhost:8000` | ||
|
||
2. **Add the Google Oauth Credentials to your `.env.development` file** | ||
|
||
There are **2** environment variable you'll need to add to your project: | ||
|
||
- `GOOGLE_CLIENT_ID`: Fetch the ClientId from the newly created OAuth 2.0 Client ID. Yuo can find it in the [Google Credentials Console](https://console.cloud.google.com/apis/credentials) | ||
- `GOOGLE_CLIENT_SECRET`: Fetch the Client Secret from the newly created OAuth 2.0 Client ID. Yuo can find it in the [Google Credentials Console](https://console.cloud.google.com/apis/credentials) | ||
|
||
You'll also want to add these as environment variables when deploying to Gatsby Cloud. | ||
|
||
3. **Start developing.** | ||
|
||
To get started, run `npm install` to add all necessary packages. | ||
|
||
When developing locally, you include environment variables to your `.env.development`. Read more about how Gatsby handles `.env` files and environment variables in the [Gatsby Docs](https://www.gatsbyjs.com/docs/how-to/local-development/environment-variables/) | ||
|
||
```shell | ||
cd examples/functions-google-oauth | ||
npm install | ||
npm run develop | ||
``` | ||
|
||
4. **Open the code and start customizing!** | ||
|
||
Your site is now running at http://localhost:8000! You can use the UI on the index page to test the functions or directly access them at http://localhost:8000/api/{function_name} | ||
|
||
Edit `src/pages/index.js` to see your site update in real-time! | ||
|
||
5. **Deploy** | ||
|
||
You can deploy this example on Gatsby Cloud by copying the example into a new repo and [connecting that to Gatsby Cloud](https://www.gatsbyjs.com/docs/how-to/previews-deploys-hosting/deploying-to-gatsby-cloud/#set-up-an-existing-gatsby-site). **Note** The live URLs of your site will need to be set in the [Google Cloud allowed URLs](https://console.cloud.google.com/apis/credentials) settings for your Client ID and the environment variables need to be set in Gatsby Cloud for: | ||
|
||
- GOOGLE_REDIRECT_URI="{YOUR_DOMAIN}/api/googleAccessToken" | ||
- APP_REDIRECT_URI="{YOUR_DOMAIN}/auth-redirect" | ||
- APP_HOSTNAME="{YOUR_DOMAIN}" |
This file contains 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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
require("dotenv").config() | ||
|
||
module.exports = { | ||
siteMetadata: { | ||
title: "Gatsby Functions with Google OAuth Example", | ||
}, | ||
plugins: ["gatsby-plugin-gatsby-cloud", `gatsby-plugin-sass`], | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "gatsby-functions-google-oauth-example", | ||
"version": "1.0.0", | ||
"private": true, | ||
"description": "Example of using Gatsby Functions with Google OAuth", | ||
"author": "Grayson Hicks", | ||
"keywords": [ | ||
"gatsby" | ||
], | ||
"scripts": { | ||
"develop": "gatsby develop", | ||
"start": "gatsby develop", | ||
"build": "gatsby build", | ||
"serve": "gatsby serve", | ||
"clean": "gatsby clean" | ||
}, | ||
"dependencies": { | ||
"babel-preset-gatsby": "^1.5.0", | ||
"gatsby": "next", | ||
"gatsby-plugin-gatsby-cloud": "next", | ||
"gatsby-plugin-sass": "next", | ||
"googleapis": "^73.0.0", | ||
"node-fetch": "^2.6.1", | ||
"query-string": "^7.0.0", | ||
"react": "^17.0.1", | ||
"react-dom": "^17.0.1", | ||
"sass": "^1.32.13" | ||
}, | ||
"devDependencies": { | ||
"@babel/preset-react": "^7.13.13" | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
examples/functions-google-oauth/src/api/googleAccessToken.js
This file contains 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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const { google } = require("googleapis") | ||
|
||
const clientId = process.env.GOOGLE_CLIENT_ID | ||
const clientSecret = process.env.GOOGLE_CLIENT_SECRET | ||
|
||
// Initialize our Google OAuth client, same as in login function. | ||
const oauth2Client = new google.auth.OAuth2( | ||
clientId, | ||
clientSecret, | ||
process.env.GOOGLE_REDIRECT_URI | ||
) | ||
|
||
google.options({ auth: oauth2Client }) | ||
|
||
const googleAccessToken = async (req, res) => { | ||
// Get the code appended from Google generateAuthUrl. | ||
const code = req.query.code | ||
|
||
// Get the auth token. | ||
const token = await oauth2Client.getToken(code) | ||
|
||
// http://localhost:8000/auth-redirect | ||
// Redirect to auth page and set the token in the URL. | ||
return res.redirect( | ||
`${process.env.APP_REDIRECT_URI}?token=${JSON.stringify(token?.tokens)}` | ||
) | ||
} | ||
|
||
export default googleAccessToken |
Oops, something went wrong.