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

Commit 0fafea3

Browse files
authored
Merge pull request #10 from netlify/refactorTokenLookup
Refactor token lookup
2 parents 6af91ae + d325599 commit 0fafea3

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/index.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ class BaseCommand extends Command {
2222
async init(err) {
2323
const projectRoot = findRoot(process.cwd())
2424
// Grab netlify API token
25-
const token = this.configToken
26-
25+
const token = getConfigToken()
2726
// Get site config from netlify.toml
2827
const configPath = getConfigPath(projectRoot)
2928
// TODO: https://github.com/request/caseless to handle key casing issues
@@ -63,11 +62,6 @@ class BaseCommand extends Command {
6362
this.netlify.api.accessToken = token
6463
}
6564

66-
get configToken() {
67-
const userId = globalConfig.get('userId')
68-
return globalConfig.get(`users.${userId}.auth.token`)
69-
}
70-
7165
async isLoggedIn() {
7266
try {
7367
await this.netlify.api.getCurrentUser()
@@ -77,8 +71,8 @@ class BaseCommand extends Command {
7771
}
7872
}
7973

80-
async authenticate(authToken) {
81-
const token = authToken || process.env.NETLIFY_AUTH_TOKEN || this.configToken
74+
async authenticate(authTokenFromFlag) {
75+
const token = getConfigToken(authTokenFromFlag)
8276
if (!token) {
8377
return this.expensivelyAuthenticate()
8478
} else {
@@ -152,4 +146,23 @@ class BaseCommand extends Command {
152146
}
153147
}
154148

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+
155168
module.exports = BaseCommand

0 commit comments

Comments
 (0)