diff --git a/README.md b/README.md
index 0a4f7109..1d645af7 100644
--- a/README.md
+++ b/README.md
@@ -24,7 +24,7 @@ npm install @smartcar/auth
### Smartcar CDN
```html
-
+
```
Before v2.2.0, the SDK was versioned as follows:
@@ -192,4 +192,4 @@ https://application-backend.com/page?error=access_denied&error_description=User+
[tag-image]: https://img.shields.io/github/tag/smartcar/javascript-sdk.svg
-[version]: 2.5.0
+[version]: 2.6.0
diff --git a/doc/README.md b/doc/README.md
index 10001bcc..15940ec4 100644
--- a/doc/README.md
+++ b/doc/README.md
@@ -28,6 +28,7 @@ Smartcar JavaScript SDK documentation.
* [.getAuthUrl(options)](#Smartcar+getAuthUrl) ⇒ String
* [.openDialog(options)](#Smartcar+openDialog)
* [.addClickHandler(options)](#Smartcar+addClickHandler)
+ * [.unmount()](#Smartcar+unmount)
* _static_
* [.AccessDenied](#Smartcar.AccessDenied) ⇐ Error
* [new Smartcar.AccessDenied(message)](#new_Smartcar.AccessDenied_new)
@@ -111,6 +112,16 @@ On-click event calls openDialog when the specified element is clicked.
| [options.vehicleInfo.make] | String
| | `vehicleInfo` is an object with an optional property `make`, which allows users to bypass the car brand selection screen. For a complete list of supported makes, please see our [API Reference](https://smartcar.com/docs/api#authorization) documentation. |
| [options.singleSelect] | Boolean
\| Object
| | An optional value that sets the behavior of the grant dialog displayed to the user. If set to `true`, `single_select` limits the user to selecting only one vehicle. If `single_select` is passed in as an object with the property `vin`, Smartcar will only authorize the vehicle with the specified VIN. See the [Single Select guide](https://smartcar.com/docs/guides/single-select/) for more information. |
+
+
+### smartcar.unmount()
+Remove Smartcar's listeners on the global window object.
+
+The Smartcar SDK uses a global 'message' event listener to recieve the
+authorization code from the pop-up dialog. Call this method to remove the
+event listener from the global window.
+
+**Kind**: instance method of [Smartcar
](#Smartcar)
### Smartcar.AccessDenied ⇐ Error
diff --git a/package-lock.json b/package-lock.json
index befafb23..15bc47dc 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
{
"name": "@smartcar/auth",
- "version": "2.5.0",
+ "version": "2.6.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
diff --git a/package.json b/package.json
index c40a0b40..7846588f 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "@smartcar/auth",
- "version": "2.5.0",
+ "version": "2.6.0",
"description": "javascript auth sdk for the smartcar",
"main": "dist/npm/sdk.js",
"license": "MIT",
diff --git a/src/sdk.js b/src/sdk.js
index 2831f31d..5cfce5fd 100644
--- a/src/sdk.js
+++ b/src/sdk.js
@@ -348,6 +348,17 @@ class Smartcar {
return false;
});
}
+
+ /**
+ * Remove Smartcar's listeners on the global window object.
+ *
+ * The Smartcar SDK uses a global 'message' event listener to recieve the
+ * authorization code from the pop-up dialog. Call this method to remove the
+ * event listener from the global window.
+ */
+ unmount() {
+ window.removeEventListener('message', this.messageHandler);
+ }
}
/**
diff --git a/test/unit/sdk.test.js b/test/unit/sdk.test.js
index ff01d8dc..66eafdd7 100644
--- a/test/unit/sdk.test.js
+++ b/test/unit/sdk.test.js
@@ -990,5 +990,21 @@ describe('sdk', () => {
document.getElementById(id).click();
expect(mockOpen).toHaveBeenCalled();
});
+
+ test('unmount removes the eventListener from the window object', () => {
+ const mockAddEventListener = jest.fn();
+ const mockRemoveEventListener = jest.fn();
+
+ window.addEventListener = mockAddEventListener;
+ window.removeEventListener = mockRemoveEventListener;
+
+ const smartcar = new Smartcar(options);
+ smartcar.unmount();
+
+ expect(mockAddEventListener)
+ .toHaveBeenCalledWith('message', smartcar.messageHandler);
+ expect(mockRemoveEventListener)
+ .toHaveBeenCalledWith('message', smartcar.messageHandler);
+ });
});
});