Skip to content

Commit

Permalink
Add support for Auth Token to SDK (#1566)
Browse files Browse the repository at this point in the history
- update Answers (answers-umd and answers-search-bar)and Core class to support token field pass in from config.
- update readme

J=SLAP-1627
TEST=manual & auto

see that token is in header when making a request
  • Loading branch information
yen-tt committed Oct 12, 2021
1 parent 821736b commit 5461cf6
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 14 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ Add the Javascript library and placeholder elements for [Answers components](#co
<div id="UniversalResultsContainer"></div>
```

Add an initialization script with an apiKey, experienceKey and onReady function. In the example below, we've initialized two
basic components: [SearchBar](#searchbar-component) and [UniversalResults](#universal-results-component).
Add an initialization script with an apiKey or token, experienceKey and onReady function. In the example below, we've initialized two
basic components with apiKey: [SearchBar](#searchbar-component) and [UniversalResults](#universal-results-component).
```js
function initAnswers() {
ANSWERS.init({
Expand Down Expand Up @@ -125,8 +125,10 @@ The configuration provided here is configuration that is shared across component
```js
function initAnswers() {
ANSWERS.init({
// Required, your Yext Answers API key
// Required*, your Yext Answers API key. *Do NOT provide apiKey if token is used.
apiKey: '<API_KEY_HERE>',
// Required*, custom auth token. *Do NOT provide token if apiKey is used.
token: '<TOKEN_HERE>',
// Required, the key used for your Answers experience
experienceKey: '<EXPERIENCE_KEY_HERE>',
// Optional, visitor interacting with the experience, see Visitor Configuration below for details
Expand Down
2 changes: 1 addition & 1 deletion THIRD-PARTY-NOTICES
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ MIT License

The following NPM package may be included in this product:

- @yext/answers-core@1.3.3-beta.0
- @yext/answers-core@1.3.3-beta.1

This package contains the following license and notice below:

Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
],
"dependencies": {
"@mapbox/mapbox-gl-language": "^0.10.1",
"@yext/answers-core": "^1.3.3-beta.0",
"@yext/answers-core": "^1.3.3-beta.1",
"@yext/answers-storage": "^1.1.0",
"@yext/rtf-converter": "^1.5.0",
"cross-fetch": "^3.1.4",
Expand Down
6 changes: 4 additions & 2 deletions src/answers-search-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class AnswersSearchBar {
}

this.core = new Core({
token: parsedConfig.token,
apiKey: parsedConfig.apiKey,
storage: storage,
experienceKey: parsedConfig.experienceKey,
Expand Down Expand Up @@ -251,11 +252,12 @@ class AnswersSearchBar {
}
parsedConfig.sessionTrackingEnabled = sessionTrackingEnabled;

const authIdKey = parsedConfig.apiKey ? 'apiKey' : 'token';
const sandboxPrefix = `${SANDBOX}-`;
parsedConfig.apiKey.includes(sandboxPrefix)
parsedConfig[authIdKey].includes(sandboxPrefix)
? parsedConfig.environment = SANDBOX
: parsedConfig.environment = PRODUCTION;
parsedConfig.apiKey = parsedConfig.apiKey.replace(sandboxPrefix, '');
parsedConfig[authIdKey] = parsedConfig[authIdKey].replace(sandboxPrefix, '');

return parsedConfig;
}
Expand Down
14 changes: 10 additions & 4 deletions src/answers-umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ class Answers {
}

this.core = new Core({
token: parsedConfig.token,
apiKey: parsedConfig.apiKey,
storage: storage,
experienceKey: parsedConfig.experienceKey,
Expand Down Expand Up @@ -406,11 +407,12 @@ class Answers {
}
parsedConfig.sessionTrackingEnabled = sessionTrackingEnabled;

const authIdKey = parsedConfig.apiKey ? 'apiKey' : 'token';
const sandboxPrefix = `${SANDBOX}-`;
parsedConfig.apiKey.includes(sandboxPrefix)
parsedConfig[authIdKey].includes(sandboxPrefix)
? parsedConfig.environment = SANDBOX
: parsedConfig.environment = PRODUCTION;
parsedConfig.apiKey = parsedConfig.apiKey.replace(sandboxPrefix, '');
parsedConfig[authIdKey] = parsedConfig[authIdKey].replace(sandboxPrefix, '');

return parsedConfig;
}
Expand All @@ -423,8 +425,12 @@ class Answers {
validateConfig (config) {
// TODO (tmeyer): Extract this method into it's own class. Investigate the use of JSON schema
// to validate these configs.
if (typeof config.apiKey !== 'string') {
throw new Error('Missing required `apiKey`. Type must be {string}');
if (typeof config.apiKey !== 'string' && typeof config.token !== 'string') {
throw new Error('Missing required `apiKey` or `token`. Type must be {string}');
}

if (typeof config.apiKey === 'string' && typeof config.token === 'string') {
throw new Error('Both apiKey and token are present. Only one authentication method should be provided');
}

if (typeof config.experienceKey !== 'string') {
Expand Down
8 changes: 8 additions & 0 deletions src/core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ import SearchStates from './storage/searchstates';
*/
export default class Core {
constructor (config = {}) {
/**
* A reference to the auth token used for all requests
* @type {string}
* @private
*/
this._token = config.token;

/**
* A reference to the client API Key used for all requests
* @type {string}
Expand Down Expand Up @@ -129,6 +136,7 @@ export default class Core {
*/
init (config) {
const params = {
token: this._token,
apiKey: this._apiKey,
experienceKey: this._experienceKey,
locale: this._locale,
Expand Down

0 comments on commit 5461cf6

Please sign in to comment.