Skip to content
This repository was archived by the owner on Aug 10, 2020. It is now read-only.

Commit c90aa73

Browse files
authored
Merge pull request #11 from netlify/updateTokenLookup
Update token lookup
2 parents 80911c0 + bbc87f7 commit c90aa73

File tree

1 file changed

+34
-40
lines changed

1 file changed

+34
-40
lines changed

src/index.js

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const openBrowser = require('./utils/open-browser')
99
const findRoot = require('./utils/find-root')
1010
const { track, identify } = require('./utils/telemetry')
1111
const merge = require('lodash.merge')
12+
const { NETLIFY_AUTH_TOKEN } = process.env
1213

1314
// Netlify CLI client id. Lives in bot@netlify.com
1415
// Todo setup client for multiple environments
@@ -22,7 +23,7 @@ class BaseCommand extends Command {
2223
async init(err) {
2324
const projectRoot = findRoot(process.cwd())
2425
// Grab netlify API token
25-
const token = getConfigToken()
26+
const [ token ] = this.getConfigToken()
2627
// Get site config from netlify.toml
2728
const configPath = getConfigPath(projectRoot)
2829
// TODO: https://github.com/request/caseless to handle key casing issues
@@ -54,14 +55,6 @@ class BaseCommand extends Command {
5455
}
5556
}
5657

57-
get clientToken() {
58-
return this.netlify.api.accessToken
59-
}
60-
61-
set clientToken(token) {
62-
this.netlify.api.accessToken = token
63-
}
64-
6558
async isLoggedIn() {
6659
try {
6760
await this.netlify.api.getCurrentUser()
@@ -71,22 +64,38 @@ class BaseCommand extends Command {
7164
}
7265
}
7366

74-
async authenticate(authTokenFromFlag) {
75-
const token = getConfigToken(authTokenFromFlag)
67+
/**
68+
* Get user netlify API token
69+
* @param {string} - [tokenFromFlag] - value passed in by CLI flag
70+
* @return {[string, string]} - tokenValue & location of resolved Netlify API token
71+
*/
72+
getConfigToken(tokenFromFlag) {
73+
// 1. First honor command flag --auth
74+
if (tokenFromFlag) {
75+
return [ tokenFromFlag, 'flag' ]
76+
}
77+
// 2. then Check ENV var
78+
if (NETLIFY_AUTH_TOKEN && NETLIFY_AUTH_TOKEN !== 'null') {
79+
return [ NETLIFY_AUTH_TOKEN, 'env' ]
80+
}
81+
// 3. If no env var use global user setting
82+
const userId = globalConfig.get('userId')
83+
const tokenFromConfig = globalConfig.get(`users.${userId}.auth.token`)
84+
if (tokenFromConfig) {
85+
return [ tokenFromConfig, 'config' ]
86+
}
87+
return [ null, 'not found']
88+
}
89+
90+
async authenticate(tokenFromFlag) {
91+
const [ token ] = this.getConfigToken(tokenFromFlag)
7692
if (!token) {
7793
return this.expensivelyAuthenticate()
7894
} else {
7995
return token
8096
}
8197
}
82-
async expensivelyCheckToken(token) {
83-
// this used to be inside authenticate() but was slowing down everything
84-
// https://github.com/netlify/cli/issues/286
85-
// we split it out so you have to be mindful of where you incur cost
86-
this.clientToken = token
87-
await this.netlify.api.getCurrentUser()
88-
return token
89-
}
98+
9099
async expensivelyAuthenticate() {
91100
const webUI = process.env.NETLIFY_WEB_UI || 'https://app.netlify.com'
92101
this.log(`Logging into your Netlify account...`)
@@ -98,12 +107,15 @@ class BaseCommand extends Command {
98107

99108
// Open browser for authentication
100109
const authLink = `${webUI}/authorize?response_type=ticket&ticket=${ticket.id}`
110+
101111
this.log(`Opening ${authLink}`)
102112
await openBrowser(authLink)
103113

104114
const accessToken = await this.netlify.api.getAccessToken(ticket)
105115

106-
if (!accessToken) this.error('Could not retrieve access token')
116+
if (!accessToken) {
117+
this.error('Could not retrieve access token')
118+
}
107119

108120
const user = await this.netlify.api.getCurrentUser()
109121
const userID = user.id
@@ -134,6 +146,7 @@ class BaseCommand extends Command {
134146
email: email
135147
})
136148
})
149+
137150
// Log success
138151
this.log()
139152
this.log(`${chalk.greenBright('You are now logged into your Netlify account!')}`)
@@ -146,23 +159,4 @@ class BaseCommand extends Command {
146159
}
147160
}
148161

149-
/**
150-
* Get user netlify API token
151-
* @param {string} authTokenFromFlag - value passed in by CLI flag
152-
* @return {string} - resolved Netlify API token
153-
*/
154-
function getConfigToken(authTokenFromFlag) {
155-
// 1. First honor command flag --auth
156-
if (authTokenFromFlag) {
157-
return authTokenFromFlag
158-
}
159-
// 2. then Check ENV var
160-
if (process.env.NETLIFY_AUTH_TOKEN) {
161-
return process.env.NETLIFY_AUTH_TOKEN
162-
}
163-
// 3. If no env var use global user setting
164-
const userId = globalConfig.get('userId')
165-
return globalConfig.get(`users.${userId}.auth.token`)
166-
}
167-
168-
module.exports = BaseCommand
162+
module.exports = BaseCommand

0 commit comments

Comments
 (0)