@@ -9,6 +9,7 @@ const openBrowser = require('./utils/open-browser')
9
9
const findRoot = require ( './utils/find-root' )
10
10
const { track, identify } = require ( './utils/telemetry' )
11
11
const merge = require ( 'lodash.merge' )
12
+ const { NETLIFY_AUTH_TOKEN } = process . env
12
13
13
14
// Netlify CLI client id. Lives in bot@netlify .com
14
15
// Todo setup client for multiple environments
@@ -22,7 +23,7 @@ class BaseCommand extends Command {
22
23
async init ( err ) {
23
24
const projectRoot = findRoot ( process . cwd ( ) )
24
25
// Grab netlify API token
25
- const token = getConfigToken ( )
26
+ const [ token ] = this . getConfigToken ( )
26
27
// Get site config from netlify.toml
27
28
const configPath = getConfigPath ( projectRoot )
28
29
// TODO: https://github.com/request/caseless to handle key casing issues
@@ -54,14 +55,6 @@ class BaseCommand extends Command {
54
55
}
55
56
}
56
57
57
- get clientToken ( ) {
58
- return this . netlify . api . accessToken
59
- }
60
-
61
- set clientToken ( token ) {
62
- this . netlify . api . accessToken = token
63
- }
64
-
65
58
async isLoggedIn ( ) {
66
59
try {
67
60
await this . netlify . api . getCurrentUser ( )
@@ -71,22 +64,38 @@ class BaseCommand extends Command {
71
64
}
72
65
}
73
66
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 )
76
92
if ( ! token ) {
77
93
return this . expensivelyAuthenticate ( )
78
94
} else {
79
95
return token
80
96
}
81
97
}
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
+
90
99
async expensivelyAuthenticate ( ) {
91
100
const webUI = process . env . NETLIFY_WEB_UI || 'https://app.netlify.com'
92
101
this . log ( `Logging into your Netlify account...` )
@@ -98,12 +107,15 @@ class BaseCommand extends Command {
98
107
99
108
// Open browser for authentication
100
109
const authLink = `${ webUI } /authorize?response_type=ticket&ticket=${ ticket . id } `
110
+
101
111
this . log ( `Opening ${ authLink } ` )
102
112
await openBrowser ( authLink )
103
113
104
114
const accessToken = await this . netlify . api . getAccessToken ( ticket )
105
115
106
- if ( ! accessToken ) this . error ( 'Could not retrieve access token' )
116
+ if ( ! accessToken ) {
117
+ this . error ( 'Could not retrieve access token' )
118
+ }
107
119
108
120
const user = await this . netlify . api . getCurrentUser ( )
109
121
const userID = user . id
@@ -134,6 +146,7 @@ class BaseCommand extends Command {
134
146
email : email
135
147
} )
136
148
} )
149
+
137
150
// Log success
138
151
this . log ( )
139
152
this . log ( `${ chalk . greenBright ( 'You are now logged into your Netlify account!' ) } ` )
@@ -146,23 +159,4 @@ class BaseCommand extends Command {
146
159
}
147
160
}
148
161
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