-
Notifications
You must be signed in to change notification settings - Fork 10.3k
wip(gatsby-cli): add login, logout, whoami commands #28251
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
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9ceca9b
add first wip implementation of login command
gillkyle f416c97
add first wip implementation of login command
gillkyle 818fa9c
add first wip implementation of login command
gillkyle c146f40
add first wip implementation of login command
gillkyle 7a81b3c
add first wip implementation of login command
gillkyle ec6f817
add first wip implementation of login command
gillkyle 6b8fcc5
add first wip implementation of login command
gillkyle 869870b
add first wip implementation of login command
gillkyle 0475792
Merge branch 'cli/login' of github.com:gatsbyjs/gatsby into cli/login
gillkyle fcdac5d
Merge branch 'master' into cli/login
gillkyle 778c0e9
add logout and whoami commands
gillkyle 23220f6
update urls
gillkyle 30d21be
wrap commands in experimental flag
gillkyle 6654b7c
Merge branch 'master' into cli/login
gillkyle 18c6cef
fix linting errors
gillkyle b3cace2
use backticks
gillkyle 53cdfbd
more linting issues, I should really just check this locally
gillkyle fbff3ba
code review suggestions and refactors
gillkyle 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
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 |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import fetch from "node-fetch" | ||
| import opn from "better-opn" | ||
| import reporter from "./reporter" | ||
| import { getToken, setToken } from "./util/manage-token" | ||
|
|
||
| interface ITicket { | ||
| verified: boolean | ||
| token?: string | null | ||
| expiration?: string | null | ||
| } | ||
|
|
||
| const createTicket = async (): Promise<string> => { | ||
| let ticketId | ||
| try { | ||
| const ticketResponse = await fetch( | ||
| `https://auth.gatsbyjs.com/auth/tickets/create`, | ||
| { | ||
| method: `post`, | ||
| } | ||
| ) | ||
| const ticketJson = await ticketResponse.json() | ||
| ticketId = ticketJson.ticketId | ||
| } catch (e) { | ||
| reporter.panic( | ||
| `We had trouble connecting to Gatsby Cloud to create a login session. | ||
| Please try again later, and if it continues to have trouble connecting file an issue.` | ||
| ) | ||
| } | ||
|
|
||
| return ticketId | ||
| } | ||
|
|
||
| const getTicket = async (ticketId: string): Promise<ITicket> => { | ||
| let ticket: ITicket = { | ||
| verified: false, | ||
| } | ||
| try { | ||
| const ticketResponse = await fetch( | ||
| `https://auth.gatsbyjs.com/auth/tickets/${ticketId}` | ||
| ) | ||
| const ticketJson = await ticketResponse.json() | ||
| ticket = ticketJson | ||
| } catch (e) { | ||
| reporter.error(e) | ||
| } | ||
|
|
||
| return ticket | ||
| } | ||
|
|
||
| const handleOpenBrowser = (url): void => { | ||
| // TODO: this will break if run from the CLI | ||
| // for ideas see https://github.com/netlify/cli/blob/908f285fb80f04bf2635da73381c94387b9c8b0d/src/utils/open-browser.js | ||
| console.log(``) | ||
| reporter.info(`Opening Gatsby Cloud for you to login from, copy this`) | ||
| reporter.info(`url into your browser if it doesn't open automatically:`) | ||
| console.log(``) | ||
| console.log(url) | ||
| opn(url) | ||
| } | ||
|
|
||
| /** | ||
| * Main function that logs in to Gatsby Cloud using Gatsby Cloud's authentication service. | ||
| */ | ||
| export async function login(): Promise<void> { | ||
| const tokenFromStore = await getToken() | ||
|
|
||
| if (tokenFromStore) { | ||
| reporter.info(`You are already logged in!`) | ||
| return | ||
| } | ||
|
|
||
| const webUrl = `https://gatsbyjs.com` | ||
| reporter.info(`Logging into your Gatsby Cloud account...`) | ||
|
|
||
| // Create "ticket" for auth (like an expiring session) | ||
| const ticketId = await createTicket() | ||
|
|
||
| // Open browser for authentication | ||
| const authUrl = `${webUrl}/dashboard/login?authType=EXTERNAL_AUTH&ticketId=${ticketId}&noredirect=1` | ||
|
|
||
| await handleOpenBrowser(authUrl) | ||
|
|
||
| // Poll until the ticket has been verified, and should have the token attached | ||
| function pollForTicket(): Promise<ITicket> { | ||
| return new Promise(function (resolve): void { | ||
| // eslint-disable-next-line consistent-return | ||
| async function verify(): Promise<void> { | ||
| const ticket = await getTicket(ticketId) | ||
| if (ticket.verified) return resolve(ticket) | ||
| setTimeout(verify, 3000) | ||
| } | ||
|
|
||
| verify() | ||
| }) | ||
| } | ||
|
|
||
| console.log(``) | ||
| reporter.info(`Waiting for login from Gatsby Cloud...`) | ||
|
|
||
| const ticket = await pollForTicket() | ||
|
|
||
| if (ticket?.token && ticket?.expiration) { | ||
| await setToken(ticket.token, ticket.expiration) | ||
| } | ||
| reporter.info(`You have been logged in!`) | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import reporter from "./reporter" | ||
| import { setToken } from "./util/manage-token" | ||
|
|
||
| /** | ||
| * Main function that logs out of Gatsby Cloud by removing the token from the config store. | ||
| */ | ||
| export async function logout(): Promise<void> { | ||
| await setToken(null, ``) | ||
| reporter.info(`You have been logged out of Gatsby Cloud from this device.`) | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { getConfigStore } from "gatsby-core-utils" | ||
| import report from "../reporter" | ||
|
|
||
| const tokenKey = `cli.token` | ||
| const tokenExpirationKey = `cli.tokenExpiration` | ||
|
|
||
| const getExpiration = (): string => getConfigStore().get(tokenExpirationKey) | ||
| export const getToken = async (): Promise<string> => { | ||
| const expiration = await getExpiration() | ||
| const tokenHasExpired = new Date() > new Date(expiration) | ||
| if (tokenHasExpired) { | ||
| report.warn(`Your token has expired, you may need to login again`) | ||
| } | ||
| return getConfigStore().get(tokenKey) | ||
| } | ||
|
|
||
| export const setToken = (token: string | null, expiration: string): void => { | ||
| getConfigStore().set(tokenKey, token) | ||
| // we would be able to decode an expiration off the JWT, but the auth service isn't set up to attach it to the token | ||
| getConfigStore().set(tokenExpirationKey, expiration) | ||
gillkyle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import fetch from "node-fetch" | ||
| import reporter from "./reporter" | ||
| import { getToken } from "./util/manage-token" | ||
|
|
||
| const getUsername = async (token: string): Promise<string> => { | ||
| let currentUsername | ||
| const query = `query { | ||
| currentUser { | ||
| name | ||
| } | ||
| }` | ||
| try { | ||
| const usernameResponse = await fetch(`https://api.gatsbyjs.com/graphql`, { | ||
| method: `post`, | ||
| body: JSON.stringify({ query }), | ||
| headers: { | ||
| Authorization: `Bearer ${token}`, | ||
| "content-type": `application/json`, | ||
| }, | ||
| }) | ||
| const resJson = await usernameResponse.json() | ||
| currentUsername = resJson.data.currentUser.name | ||
| } catch (e) { | ||
| reporter.error(e) | ||
| } | ||
|
|
||
| return currentUsername | ||
| } | ||
|
|
||
| /** | ||
| * Reports the username of the logged in user if they are logged in. | ||
| */ | ||
| export async function whoami(): Promise<void> { | ||
| const tokenFromStore = await getToken() | ||
|
|
||
| if (!tokenFromStore) { | ||
| reporter.info(`You are not currently logged in!`) | ||
| return | ||
| } | ||
|
|
||
| const username = await getUsername(tokenFromStore) | ||
| reporter.info(username) | ||
| } |
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.