Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 26 additions & 24 deletions src/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class Smartcar {
// handler
this.messageHandler = (event) => {
// bail if message from unexpected source
if (!this.redirectUri.startsWith(event.origin)) {
if (this.redirectUri && !this.redirectUri.startsWith(event.origin)) {
return;
}

Expand Down Expand Up @@ -177,30 +177,29 @@ class Smartcar {
throw new TypeError('A client ID option must be provided');
}

if (!options.redirectUri) {
throw new TypeError('A redirect URI option must be provided');
}

if (options.redirectUri.startsWith('https://javascript-sdk.smartcar.com')) {
// require onComplete method with at least two parameters (error & code)
// when hosting on Smartcar CDN
if (!options.onComplete || options.onComplete.length < 2) {
throw new Error(
"When using Smartcar's CDN redirect an onComplete function with at" +
' least 2 parameters (error & code) is required to handle' +
' completion of Connect',
);
}
if (options.redirectUri) {
if (options.redirectUri.startsWith('https://javascript-sdk.smartcar.com')) {
// require onComplete method with at least two parameters (error & code)
// when hosting on Smartcar CDN
if (!options.onComplete || options.onComplete.length < 2) {
throw new Error(
"When using Smartcar's CDN redirect an onComplete function with at" +
' least 2 parameters (error & code) is required to handle' +
' completion of Connect',
);
}

const usesOldUriScheme = (/redirect-[0-9]+\.[0-9]+\.[0-9]+\?/).test(options.redirectUri);
const usesOldUriScheme = (/redirect-[0-9]+\.[0-9]+\.[0-9]+\?/).test(options.redirectUri);

if (usesOldUriScheme) {
// eslint-disable-next-line no-console
console.warn(
"\nThe Smartcar redirect URI you're using is outdated! To update it, please see:\nhttps://github.com/smartcar/javascript-sdk#1-register-a-javascript-sdk-redirect-uri\n",
);
if (usesOldUriScheme) {
// eslint-disable-next-line no-console
console.warn(
"\nThe Smartcar redirect URI you're using is outdated! To update it, please see:\nhttps://github.com/smartcar/javascript-sdk#1-register-a-javascript-sdk-redirect-uri\n",
);
}
}
}

}

/**
Expand Down Expand Up @@ -301,14 +300,17 @@ class Smartcar {
link += 'https://connect.smartcar.com/oauth/authorize';
link += `?response_type=${this.responseType}`;
link += `&client_id=${this.clientId}`;
link += `&redirect_uri=${encodeURIComponent(this.redirectUri)}`;

if (this.redirectUri) {
link += `&redirect_uri=${encodeURIComponent(this.redirectUri)}`;
}
Copy link

@thachdoSC thachdoSC Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just below this, can we update the comment:

    // If scope is not specified, Smartcar will default to requesting all scopes
    // from the user

To something like

    // If scope is not specified, Smartcar will default to requesting the application's vehicle access configuration if available or the default set of permissions


// map forcePrompt to approvalPrompt, two options: 'force' and 'auto'
const forcePrompt = options.forcePrompt === true;
link += `&approval_prompt=${forcePrompt ? 'force' : 'auto'}`;

// If scope is not specified, Smartcar will default to requesting all scopes
// from the user
// If scope is not specified, Smartcar will default to requesting the application's vehicle
// access configuration if available or the default set of permissions
if (this.scope) {
link += `&scope=${encodeURIComponent(this.scope.join(' '))}`;
}
Expand Down
28 changes: 22 additions & 6 deletions test/unit/sdk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ describe('sdk', () => {
const CDN_ORIGIN = 'https://javascript-sdk.smartcar.com';

describe('constructor', () => {
test('throws error if constructor called without redirectUri', () => {
expect(() => new Smartcar({clientId: 'uuid'})).toThrow(
'A redirect URI option must be provided',
);
});

test('throws error if constructor called without clientId', () => {
expect(() => new Smartcar({redirectUri: 'http://example.com'})).toThrow(
'A client ID option must be provided',
Expand Down Expand Up @@ -129,6 +123,28 @@ describe('sdk', () => {
expect(smartcar.onComplete).toBe(undefined);
});

test("doesn't break if redirect uri is not passed", () => {
const options = {
clientId: 'clientId',
scope: ['read_vehicle_info', 'read_odometer'],
};

const smartcar = new Smartcar(options);

const event = {
data: {
name: 'SmartcarAuthMessage',
isSmartcarHosted: false,
code: 'super-secret-code',
error: undefined,
state: getEncodedState(smartcar.instanceId, 'some-state'),
},
origin: 'https://selfhosted.com',
};

smartcar.messageHandler(event);
});

test("doesn't break if onComplete is not passed", () => {
const options = {
clientId: 'clientId',
Expand Down