-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: updated the way we handle identity fetching
- added get-identity.js file; to do the actual fetching - updated commands that required whoami.js - lib/stars.js: small refactoring of promise chain - lib/star.js: fixed missing comma
- Loading branch information
Michael Perrotte
committed
Mar 5, 2020
1 parent
5a36ee2
commit 924d85b
Showing
7 changed files
with
71 additions
and
61 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
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 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,43 @@ | ||
'use strict' | ||
|
||
const npmFetch = require('npm-registry-fetch') | ||
const npm = require('../npm') | ||
|
||
const needsAuthError = (msg) => | ||
Object.assign(new Error(msg, { code: 'ENEEDAUTH' })) | ||
|
||
module.exports = async (opts = {}, spec = null) => { | ||
const { registry } = opts | ||
if (!registry) { | ||
throw new Error('no default registry set') | ||
} | ||
|
||
// First, check if we have a user/pass-based auth | ||
const creds = npm.config.getCredentialsByURI(registry) | ||
const { username: usernameFromURI, token } = creds | ||
|
||
if (usernameFromURI) { | ||
// Found username; return it | ||
return Promise.resolve(usernameFromURI) | ||
} else if (token) { | ||
// No username, but we have a token; fetch the username from registry | ||
const registryData = await npmFetch.json('/-/whoami', { | ||
...opts, | ||
spec | ||
}) | ||
const { username: usernameFromRegistry } = registryData | ||
// Retrieved username from registry; return it | ||
if (usernameFromRegistry) { | ||
return Promise.resolve(usernameFromRegistry) | ||
} else { | ||
// Didn't get username from registry; bad token | ||
throw needsAuthError( | ||
'Your auth token is no longer valid. Please login again.' | ||
) | ||
} | ||
} else { | ||
// At this point, if they have a credentials object, it doesn't have a | ||
// token or auth in it. Probably just the default registry. | ||
throw needsAuthError('This command requires you to be logged in.') | ||
} | ||
} |
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