Skip to content

Commit

Permalink
replace isloginrequired with no identity available function
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleysmithTTD committed Dec 6, 2024
1 parent 69ae6f7 commit 6bc7d44
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 25 deletions.
13 changes: 6 additions & 7 deletions examples/cstg/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,17 @@
$('#targeted_advertising_ready').text(sdk.getAdvertisingToken() ? 'yes' : 'no');
$('#advertising_token').text(String(sdk.getAdvertisingToken()));
$('#login_required').text(
sdk.isLoginRequired() || sdk.isLoginRequired() === undefined ? 'yes' : 'no'
);
$(`#has_opted_out`).text(
sdk.hasOptedOut() ? 'yes' : 'no'
sdk.noIdentityAvailable() || sdk.noIdentityAvailable() === undefined ? 'yes' : 'no'
);
$(`#has_opted_out`).text(sdk.hasOptedOut() ? 'yes' : 'no');
$('#update_counter').text(callbackCounter);
$('#identity_state').text(String(JSON.stringify(state, null, 2)));

updateSharedGuiElements();
}

function updateSharedGuiElements() {
if (getUidSdk().isLoginRequired()) {
if (getUidSdk().noIdentityAvailable()) {
$('#login_form').show();
$('#logout_form').hide();
} else {
Expand All @@ -65,10 +63,11 @@
});

$('#login').click(async () => {
const email = $('#email').val();
//const email = $('#email').val();
const phone = $('#email').val();

try {
await getUidSdk().setIdentityFromEmail(email, clientSideIdentityOptions);
await getUidSdk().setIdentityFromPhoneHash(phone, clientSideIdentityOptions);
} catch (e) {
console.error('setIdentityFromEmail failed', e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ The following table outlines and annotates the steps you can take to test and ex
| 3 | Click the **Back to the main page** link. | On the updated application main page, note the newly populated **UID2 Advertising Token** value and a video player. While the [page view](views/index.html) is loading, [GPT](https://developers.google.com/publisher-tag/reference#googletag) auto-loads the Secure Signal UID2 script which pushes the advertising token to GPT local storage, and the [IMA](https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side) makes an ad request which transmits the encoded signal in the request. The [page view](views/index.html) calls the [init()](https://unifiedid.com/docs/sdks/client-side-identity#initopts-object-void) function again, but this time without passing an explicit identity. Instead, the identity is loaded from the first-party cookie. |
| 4 | Click **Play**. | This triggers AdsManager to insert the ad returned from the ad request, for display. The ad tag used in this example contains a 10-second pre-roll ad. |
| 5 | Keep the application main page open, or refresh it after a while, and note the UID2 identity state, updated counter, and login information values. | In the background, the UID2 SDK continuously validates whether the advertising token is up to date, and refreshes it automatically when needed. If the refresh succeeds, the user opts out, or the refresh token expires, the callback function is invoked, and the UI elements are updated with the current state of the UID2 identity. For details, see [Workflow Overview](https://unifiedid.com/docs/sdks/client-side-identity#workflow-overview) and [Background Token Auto-Refresh](https://unifiedid.com/docs/sdks/client-side-identity#background-token-auto-refresh). |
| 6 | To exit the application, click **Log Out**. | This calls the UID2 SDK [`disconnect()`](https://unifiedid.com/docs/sdks/client-side-identity#disconnect-void) function, which clears the UID2 session and the first-party cookie and calls the Secure Signal [`clearAllCache()`](https://developers.google.com/publisher-tag/reference#googletag.secureSignals.SecureSignalProvidersArray_clearAllCache) function to clear all cached signals. This call also makes the UID2 SDK [`isLoginRequired()`](https://unifiedid.com/docs/sdks/client-side-identity#isloginrequired-boolean) function return `true`, which presents the user with the login form again.<br/> NOTE: The page displays the **Log Out** button as long as the user identity is valid and refreshable. |
| 6 | To exit the application, click **Log Out**. | This calls the UID2 SDK [`disconnect()`](https://unifiedid.com/docs/sdks/client-side-identity#disconnect-void) function, which clears the UID2 session and the first-party cookie and calls the Secure Signal [`clearAllCache()`](https://developers.google.com/publisher-tag/reference#googletag.secureSignals.SecureSignalProvidersArray_clearAllCache) function to clear all cached signals. This call also makes the UID2 SDK [`noIdentityAvailable()`](https://unifiedid.com/docs/sdks/client-side-identity#noIdentityAvailable-boolean) function return `true`, which presents the user with the login form again.<br/> NOTE: The page displays the **Log Out** button as long as the user identity is valid and refreshable. |
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
__uid2.getAdvertisingToken() ? "yes" : "no"
);
$("#advertising_token").html(String(__uid2.getAdvertisingToken()));
$("#login_required").html(__uid2.isLoginRequired() ? "yes" : "no");
$("#login_required").html(__uid2.noIdentityAvailable() ? "yes" : "no");
$("#update_counter").html(callbackCounter);
$("#identity_state").html(String(JSON.stringify(payload, null, 2)));

if (__uid2.isLoginRequired()) {
if (__uid2.noIdentityAvailable()) {
$("#login_form").show();
$("#logout_form").hide();
$('#googleAdContainer').hide();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!doctype html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
Expand All @@ -18,7 +18,7 @@
});
}
if (eventType === 'InitCompleted') {
if (__uid2.isLoginRequired()) __uid2.setIdentity(<%- JSON.stringify(identity) %>)
if (__uid2.noIdentityAvailable()) __uid2.setIdentity(<%- JSON.stringify(identity) %>)
}
});
</script>
Expand Down
16 changes: 8 additions & 8 deletions setupJest.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,47 @@ expect.extend({
expect(uid2.getAdvertisingToken()).toBeNonEmptyString();
}

expect(uid2.isLoginRequired()).toEqual(false);
expect(uid2.noIdentityAvailable()).toEqual(false);

return {
pass: true,
message: () =>
'Expected getAdvertisingToken() returns a token and isLoginRequired() returns false',
'Expected getAdvertisingToken() returns a token and noIdentityAvailable() returns false',
};
},

toBeInTemporarilyUnavailableState(uid2) {
expect(uid2.getAdvertisingToken()).toBeUndefined();
expect(uid2.isLoginRequired()).toEqual(false);
expect(uid2.noIdentityAvailable()).toEqual(false);

return {
pass: true,
message: () =>
'Expected getAdvertisingToken() returns undefined and isLoginRequired() returns false',
'Expected getAdvertisingToken() returns undefined and noIdentityAvailable() returns false',
};
},

toBeInUnavailableState(uid2) {
expect(uid2.getAdvertisingToken()).toBeUndefined();
expect(uid2.isLoginRequired()).toEqual(true);
expect(uid2.noIdentityAvailable()).toEqual(true);
expect(uid2.hasOptedOut()).toEqual(false);

return {
pass: true,
message: () =>
'Expected getAdvertisingToken() returns undefined and isLoginRequired() returns true',
'Expected getAdvertisingToken() returns undefined and noIdentityAvailable() returns true',
};
},

toBeInOptoutState(uid2) {
expect(uid2.getAdvertisingToken()).toBeUndefined();
expect(uid2.isLoginRequired()).toEqual(false);
expect(uid2.noIdentityAvailable()).toEqual(false);
expect(uid2.hasOptedOut()).toEqual(true);

return {
pass: true,
message: () =>
'Expected getAdvertisingToken() returns undefined and isLoginRequired() returns false',
'Expected getAdvertisingToken() returns undefined and noIdentityAvailable() returns false',
};
},
});
Expand Down
15 changes: 10 additions & 5 deletions src/sdkBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,22 @@ export abstract class SdkBase {
}

/**
* Deprecated
* @deprecated in version 3.9.0. Use noIdentityAvailable() instead
*/
public isLoginRequired() {
return this.hasIdentity();
public noIdentityAvailable() {
return this.noIdentityAvailable();
}

public hasIdentity() {
public noIdentityAvailable() {
if (!this._initComplete) return undefined;
return !(this.isLoggedIn() || this._apiClient?.hasActiveRequests());
}

// public hasIdentity() {
// if (!this._initComplete) return undefined;
// return !(this.isLoggedIn() || this._apiClient?.hasActiveRequests());
// }

public hasOptedOut() {
if (!this._initComplete) return undefined;
return isOptoutIdentity(this._identity);
Expand Down Expand Up @@ -439,7 +444,7 @@ export abstract class SdkBase {
clearTimeout(this._refreshTimerId);
}
this._refreshTimerId = setTimeout(() => {
if (this.isLoginRequired()) return;
if (this.noIdentityAvailable()) return;
const validatedIdentity = this.validateAndSetIdentity(
this._storageManager?.loadIdentity() ?? null
);
Expand Down

0 comments on commit 6bc7d44

Please sign in to comment.