KEYCLOAK-15113: Move away from deprecated Promise.success()/error()

This commit is contained in:
testn 2020-08-12 16:06:03 +07:00 committed by Stan Silvert
parent 0001a930b4
commit 0362d3a430
14 changed files with 43 additions and 43 deletions

View file

@ -546,13 +546,13 @@ declare namespace Keycloak {
* still valid, or if the token is no longer valid. * still valid, or if the token is no longer valid.
* @example * @example
* ```js * ```js
* keycloak.updateToken(5).success(function(refreshed) { * keycloak.updateToken(5).then(function(refreshed) {
* if (refreshed) { * if (refreshed) {
* alert('Token was successfully refreshed'); * alert('Token was successfully refreshed');
* } else { * } else {
* alert('Token is still valid'); * alert('Token is still valid');
* } * }
* }).error(function() { * }).catch(function() {
* alert('Failed to refresh the token, or the session has expired'); * alert('Failed to refresh the token, or the session has expired');
* }); * });
*/ */

View file

@ -20,7 +20,7 @@ var module = angular.module('app', []);
angular.element(document).ready(function ($http) { angular.element(document).ready(function ($http) {
var keycloakAuth = new Keycloak('keycloak.json'); var keycloakAuth = new Keycloak('keycloak.json');
keycloakAuth.init({ onLoad: 'login-required' }).success(function () { keycloakAuth.init({ onLoad: 'login-required' }).then(function () {
module.factory('Auth', function() { module.factory('Auth', function() {
var Auth = {}; var Auth = {};
@ -59,8 +59,8 @@ angular.element(document).ready(function ($http) {
deferred.resolve(config); deferred.resolve(config);
if (keycloakAuth.token) { if (keycloakAuth.token) {
keycloakAuth.updateToken(5).success(function() { keycloakAuth.updateToken(5).then(function() {
}).error(function() { }).catch(function() {
deferred.reject('Failed to refresh token'); deferred.reject('Failed to refresh token');
}); });
} }

View file

@ -20,7 +20,7 @@ var module = angular.module('app', []);
angular.element(document).ready(function ($http) { angular.element(document).ready(function ($http) {
var keycloakAuth = new Keycloak('keycloak.json'); var keycloakAuth = new Keycloak('keycloak.json');
keycloakAuth.init({ onLoad: 'login-required' }).success(function () { keycloakAuth.init({ onLoad: 'login-required' }).then(function () {
module.factory('Auth', function() { module.factory('Auth', function() {
var Auth = {}; var Auth = {};
@ -59,8 +59,8 @@ angular.element(document).ready(function ($http) {
deferred.resolve(config); deferred.resolve(config);
if (keycloakAuth.token) { if (keycloakAuth.token) {
keycloakAuth.updateToken(5).success(function() { keycloakAuth.updateToken(5).then(function() {
}).error(function() { }).catch(function() {
deferred.reject('Failed to refresh token'); deferred.reject('Failed to refresh token');
}); });
} }
@ -87,7 +87,7 @@ angular.element(document).ready(function ($http) {
}); });
angular.bootstrap(document, ["app"]); angular.bootstrap(document, ["app"]);
}).error(function () { }).catch(function () {
window.location.reload(); window.location.reload();
}); });
}); });

View file

@ -20,7 +20,7 @@ var module = angular.module('app', []);
angular.element(document).ready(function ($http) { angular.element(document).ready(function ($http) {
var keycloakAuth = new Keycloak('keycloak.json'); var keycloakAuth = new Keycloak('keycloak.json');
keycloakAuth.init({ onLoad: 'login-required' }).success(function () { keycloakAuth.init({ onLoad: 'login-required' }).then(function () {
module.factory('Auth', function() { module.factory('Auth', function() {
var Auth = {}; var Auth = {};
@ -53,8 +53,8 @@ angular.element(document).ready(function ($http) {
deferred.resolve(config); deferred.resolve(config);
if (keycloakAuth.token) { if (keycloakAuth.token) {
keycloakAuth.updateToken(5).success(function() { keycloakAuth.updateToken(5).then(function() {
}).error(function() { }).catch(function() {
deferred.reject('Failed to refresh token'); deferred.reject('Failed to refresh token');
}); });
} }
@ -81,7 +81,7 @@ angular.element(document).ready(function ($http) {
}); });
angular.bootstrap(document, ["app"]); angular.bootstrap(document, ["app"]);
}).error(function () { }).catch(function () {
window.location = keycloakAuth.createLoginUrl({ window.location = keycloakAuth.createLoginUrl({
idpHint: 'saml-identity-provider' idpHint: 'saml-identity-provider'
}) })

View file

@ -20,7 +20,7 @@ var module = angular.module('app', []);
angular.element(document).ready(function ($http) { angular.element(document).ready(function ($http) {
var keycloakAuth = new Keycloak('keycloak.json'); var keycloakAuth = new Keycloak('keycloak.json');
keycloakAuth.init({ onLoad: 'login-required' }).success(function () { keycloakAuth.init({ onLoad: 'login-required' }).then(function () {
module.factory('Auth', function() { module.factory('Auth', function() {
var Auth = {}; var Auth = {};
@ -63,8 +63,8 @@ angular.element(document).ready(function ($http) {
deferred.resolve(config); deferred.resolve(config);
if (keycloakAuth.token) { if (keycloakAuth.token) {
keycloakAuth.updateToken(5).success(function() { keycloakAuth.updateToken(5).then(function() {
}).error(function() { }).catch(function() {
deferred.reject('Failed to refresh token'); deferred.reject('Failed to refresh token');
}); });
} }
@ -91,7 +91,7 @@ angular.element(document).ready(function ($http) {
}); });
angular.bootstrap(document, ["app"]); angular.bootstrap(document, ["app"]);
}).error(function () { }).catch(function () {
window.location.reload(); window.location.reload();
}); });
}); });

View file

@ -60,7 +60,7 @@
responseMode: 'query', responseMode: 'query',
onLoad: 'check-sso', onLoad: 'check-sso',
redirectUri: 'android-app://org.keycloak.examples.cordova/https/keycloak-cordova-example.github.io/login' redirectUri: 'android-app://org.keycloak.examples.cordova/https/keycloak-cordova-example.github.io/login'
}).success(updateState).error(error); }).then(updateState).catch(error);
}, false); }, false);
</script> </script>
<style> <style>

View file

@ -55,7 +55,7 @@
} }
document.addEventListener("deviceready", function() { document.addEventListener("deviceready", function() {
keycloak.init({ onLoad: "check-sso" }).success(updateState).error(error); keycloak.init({ onLoad: "check-sso" }).then(updateState).catch(error);
}, false); }, false);
</script> </script>
<style> <style>

View file

@ -31,7 +31,7 @@ angular.element(document).ready(function ($http) {
var keycloakAuth = new Keycloak('keycloak.json'); var keycloakAuth = new Keycloak('keycloak.json');
auth.loggedIn = false; auth.loggedIn = false;
keycloakAuth.init({ onLoad: 'login-required' }).success(function () { keycloakAuth.init({ onLoad: 'login-required' }).then(function () {
console.log('here login'); console.log('here login');
auth.loggedIn = true; auth.loggedIn = true;
auth.authz = keycloakAuth; auth.authz = keycloakAuth;
@ -40,7 +40,7 @@ angular.element(document).ready(function ($http) {
return auth; return auth;
}); });
angular.bootstrap(document, ["product"]); angular.bootstrap(document, ["product"]);
}).error(function () { }).catch(function () {
alert("failed to login"); alert("failed to login");
}); });
@ -112,12 +112,12 @@ module.factory('authInterceptor', function($q, Auth) {
request: function (config) { request: function (config) {
var deferred = $q.defer(); var deferred = $q.defer();
if (Auth.authz.token) { if (Auth.authz.token) {
Auth.authz.updateToken(5).success(function() { Auth.authz.updateToken(5).then(function() {
config.headers = config.headers || {}; config.headers = config.headers || {};
config.headers.Authorization = 'Bearer ' + Auth.authz.token; config.headers.Authorization = 'Bearer ' + Auth.authz.token;
deferred.resolve(config); deferred.resolve(config);
}).error(function() { }).catch(function() {
deferred.reject('Failed to refresh token'); deferred.reject('Failed to refresh token');
}); });
} }

View file

@ -30,7 +30,7 @@ angular.element(document).ready(function ($http) {
var keycloakAuth = new Keycloak('keycloak.json'); var keycloakAuth = new Keycloak('keycloak.json');
auth.loggedIn = false; auth.loggedIn = false;
keycloakAuth.init({ onLoad: 'login-required' }).success(function () { keycloakAuth.init({ onLoad: 'login-required' }).then(function () {
auth.loggedIn = true; auth.loggedIn = true;
auth.authz = keycloakAuth; auth.authz = keycloakAuth;
auth.logoutUrl = keycloakAuth.authServerUrl + "/realms/demo/protocol/openid-connect/logout?redirect_uri=/angular-product/index.html"; auth.logoutUrl = keycloakAuth.authServerUrl + "/realms/demo/protocol/openid-connect/logout?redirect_uri=/angular-product/index.html";
@ -38,7 +38,7 @@ angular.element(document).ready(function ($http) {
return auth; return auth;
}); });
angular.bootstrap(document, ["product"]); angular.bootstrap(document, ["product"]);
}).error(function () { }).catch(function () {
window.location.reload(); window.location.reload();
}); });
@ -62,12 +62,12 @@ module.factory('authInterceptor', function($q, Auth) {
request: function (config) { request: function (config) {
var deferred = $q.defer(); var deferred = $q.defer();
if (Auth.authz.token) { if (Auth.authz.token) {
Auth.authz.updateToken(5).success(function() { Auth.authz.updateToken(5).then(function() {
config.headers = config.headers || {}; config.headers = config.headers || {};
config.headers.Authorization = 'Bearer ' + Auth.authz.token; config.headers.Authorization = 'Bearer ' + Auth.authz.token;
deferred.resolve(config); deferred.resolve(config);
}).error(function() { }).catch(function() {
deferred.reject('Failed to refresh token'); deferred.reject('Failed to refresh token');
}); });
} }

View file

@ -98,8 +98,8 @@ User <b id="subject"></b> made this request.
} }
keycloak.init({ onLoad: 'login-required' }) keycloak.init({ onLoad: 'login-required' })
.success(reloadData) .then(reloadData)
.error(function(errorData) { .catch(function(errorData) {
document.getElementById('customers').innerHTML = '<b>Failed to load data. Error: ' + JSON.stringify(errorData) + '</b>'; document.getElementById('customers').innerHTML = '<b>Failed to load data. Error: ' + JSON.stringify(errorData) + '</b>';
}); });

View file

@ -90,13 +90,13 @@
} }
function refreshToken(minValidity) { function refreshToken(minValidity) {
keycloak.updateToken(minValidity).success(function(refreshed) { keycloak.updateToken(minValidity).then(function(refreshed) {
if (refreshed) { if (refreshed) {
output(keycloak.tokenParsed); output(keycloak.tokenParsed);
} else { } else {
output('Token not refreshed, valid for ' + Math.round(keycloak.tokenParsed.exp + keycloak.timeSkew - new Date().getTime() / 1000) + ' seconds'); output('Token not refreshed, valid for ' + Math.round(keycloak.tokenParsed.exp + keycloak.timeSkew - new Date().getTime() / 1000) + ' seconds');
} }
}).error(function() { }).catch(function() {
output('Failed to refresh token'); output('Failed to refresh token');
}); });
} }
@ -173,9 +173,9 @@
flow: 'standard' flow: 'standard'
}; };
keycloak.init(initOptions).success(function(authenticated) { keycloak.init(initOptions).then(function(authenticated) {
output('Init Success (' + (authenticated ? 'Authenticated' : 'Not Authenticated') + ')'); output('Init Success (' + (authenticated ? 'Authenticated' : 'Not Authenticated') + ')');
}).error(function() { }).catch(function() {
output('Init Error'); output('Init Error');
}); });

View file

@ -48,7 +48,7 @@ angular.element(document).ready(function ($http) {
var keycloakAuth = new Keycloak('keycloak.json'); var keycloakAuth = new Keycloak('keycloak.json');
auth.loggedIn = false; auth.loggedIn = false;
keycloakAuth.init({ onLoad: 'login-required' }).success(function () { keycloakAuth.init({ onLoad: 'login-required' }).then(function () {
console.log('here login'); console.log('here login');
auth.loggedIn = true; auth.loggedIn = true;
auth.authz = keycloakAuth; auth.authz = keycloakAuth;
@ -57,7 +57,7 @@ angular.element(document).ready(function ($http) {
return auth; return auth;
}); });
angular.bootstrap(document, ["product"]); angular.bootstrap(document, ["product"]);
}).error(function () { }).catch(function () {
alert("failed to login"); alert("failed to login");
}); });
@ -117,12 +117,12 @@ module.factory('authInterceptor', function($q, Auth) {
request: function (config) { request: function (config) {
var deferred = $q.defer(); var deferred = $q.defer();
if (Auth.authz.token) { if (Auth.authz.token) {
Auth.authz.updateToken(5).success(function() { Auth.authz.updateToken(5).then(function() {
config.headers = config.headers || {}; config.headers = config.headers || {};
config.headers.Authorization = 'Bearer ' + Auth.authz.token; config.headers.Authorization = 'Bearer ' + Auth.authz.token;
deferred.resolve(config); deferred.resolve(config);
}).error(function() { }).catch(function() {
deferred.reject('Failed to refresh token'); deferred.reject('Failed to refresh token');
}); });
} }

View file

@ -702,9 +702,9 @@ public class JavascriptAdapterTest extends AbstractJavascriptTest {
@Test @Test
public void testRefreshTokenWithDeprecatedPromiseHandles() { public void testRefreshTokenWithDeprecatedPromiseHandles() {
String refreshWithDeprecatedHandles = "var callback = arguments[arguments.length - 1];" + String refreshWithDeprecatedHandles = "var callback = arguments[arguments.length - 1];" +
" window.keycloak.updateToken(9999).success(function (refreshed) {" + " window.keycloak.updateToken(9999).then(function (refreshed) {" +
" callback('Success handle');" + " callback('Success handle');" +
" }).error(function () {" + " }).catch(function () {" +
" callback('Error handle');" + " callback('Error handle');" +
" });"; " });";

View file

@ -96,7 +96,7 @@ angular.element(document).ready(function () {
return auth; return auth;
}); });
keycloakAuth.init({ onLoad: 'login-required', pkceMethod: 'S256' }).success(function () { keycloakAuth.init({ onLoad: 'login-required', pkceMethod: 'S256' }).then(function () {
auth.authz = keycloakAuth; auth.authz = keycloakAuth;
whoAmI(function(data) { whoAmI(function(data) {
@ -117,7 +117,7 @@ angular.element(document).ready(function () {
}); });
loadSelect2Localization(); loadSelect2Localization();
}).error(function () { }).catch(function () {
window.location.reload(); window.location.reload();
}); });
}); });
@ -128,12 +128,12 @@ module.factory('authInterceptor', function($q, Auth) {
if (!config.url.match(/.html$/)) { if (!config.url.match(/.html$/)) {
var deferred = $q.defer(); var deferred = $q.defer();
if (Auth.authz.token) { if (Auth.authz.token) {
Auth.authz.updateToken(5).success(function () { Auth.authz.updateToken(5).then(function () {
config.headers = config.headers || {}; config.headers = config.headers || {};
config.headers.Authorization = 'Bearer ' + Auth.authz.token; config.headers.Authorization = 'Bearer ' + Auth.authz.token;
deferred.resolve(config); deferred.resolve(config);
}).error(function () { }).catch(function () {
location.reload(); location.reload();
}); });
} }