Add 'register' and 'createRegisterUrl' methods to Javascript Adapter API. fixes #KEYCLOAK-1904

This commit is contained in:
Libor Krzyzanek 2015-10-07 13:53:25 +02:00
parent 7a3b4823b0
commit f29aff4bed
4 changed files with 45 additions and 1 deletions

View file

@ -209,6 +209,7 @@ new Keycloak({ url: 'http://localhost/auth', realm: 'myrealm', clientId: 'myApp'
<listitem>redirectUri - specifies the uri to redirect to after login</listitem> <listitem>redirectUri - specifies the uri to redirect to after login</listitem>
<listitem>prompt - can be set to 'none' to check if the user is logged in already (if not logged in, a login form is not displayed)</listitem> <listitem>prompt - can be set to 'none' to check if the user is logged in already (if not logged in, a login form is not displayed)</listitem>
<listitem>loginHint - used to pre-fill the username/email field on the login form</listitem> <listitem>loginHint - used to pre-fill the username/email field on the login form</listitem>
<listitem>action - if value is 'register' then user is redirected to registration page, otherwise to login page</listitem>
</itemizedlist> </itemizedlist>
</para> </para>
</simplesect> </simplesect>
@ -246,6 +247,20 @@ new Keycloak({ url: 'http://localhost/auth', realm: 'myrealm', clientId: 'myApp'
</para> </para>
</simplesect> </simplesect>
<simplesect>
<title>register(options)</title>
<para>Redirects to registration form. It's a shortcut for doing login with option action = 'register'</para>
<para>Options are same as login method but 'action' is overwritten to 'register'</para>
</simplesect>
<simplesect>
<title>createRegisterUrl(options)</title>
<para>Returns the url to registration page. It's a shortcut for doing createRegisterUrl with option action = 'register'</para>
<para>Options are same as createLoginUrl method but 'action' is overwritten to 'register'</para>
</simplesect>
<simplesect> <simplesect>
<title>accountManagement()</title> <title>accountManagement()</title>

View file

@ -12,6 +12,6 @@ Open the Keycloak admin console, click on Add Realm, click on 'Choose a JSON fil
Deploy the JS Console to Keycloak by running: Deploy the JS Console to Keycloak by running:
mvn install jboss-as:deploy mvn install wildfly:deploy
Open the console at http://localhost:8080/js-console and login with username: 'user', and password: 'password'. Open the console at http://localhost:8080/js-console and login with username: 'user', and password: 'password'.

View file

@ -7,6 +7,7 @@
<div> <div>
<button onclick="keycloak.login()">Login</button> <button onclick="keycloak.login()">Login</button>
<button onclick="keycloak.logout()">Logout</button> <button onclick="keycloak.logout()">Logout</button>
<button onclick="keycloak.register()">Register</button>
<button onclick="refreshToken(9999)">Refresh Token</button> <button onclick="refreshToken(9999)">Refresh Token</button>
<button onclick="refreshToken(30)">Refresh Token (if <30s validity)</button> <button onclick="refreshToken(30)">Refresh Token (if <30s validity)</button>
<button onclick="loadProfile()">Get Profile</button> <button onclick="loadProfile()">Get Profile</button>
@ -18,6 +19,7 @@
<button onclick="output(keycloak)">Show Details</button> <button onclick="output(keycloak)">Show Details</button>
<button onclick="output(keycloak.createLoginUrl())">Show Login URL</button> <button onclick="output(keycloak.createLoginUrl())">Show Login URL</button>
<button onclick="output(keycloak.createLogoutUrl())">Show Logout URL</button> <button onclick="output(keycloak.createLogoutUrl())">Show Logout URL</button>
<button onclick="output(keycloak.createRegisterUrl())">Show Register URL</button>
</div> </div>
<h2>Result</h2> <h2>Result</h2>

View file

@ -183,6 +183,18 @@
return url; return url;
} }
kc.register = function (options) {
return adapter.register(options);
}
kc.createRegisterUrl = function(options) {
if (!options) {
options = {};
}
options.action = 'register';
return kc.createLoginUrl(options);
}
kc.createAccountUrl = function(options) { kc.createAccountUrl = function(options) {
var url = getRealmUrl() var url = getRealmUrl()
+ '/account' + '/account'
@ -760,6 +772,11 @@
return createPromise().promise; return createPromise().promise;
}, },
register: function(options) {
window.location.href = kc.createRegisterUrl(options);
return createPromise().promise;
},
accountManagement : function() { accountManagement : function() {
window.location.href = kc.createAccountUrl(); window.location.href = kc.createAccountUrl();
return createPromise().promise; return createPromise().promise;
@ -858,6 +875,16 @@
return promise.promise; return promise.promise;
}, },
register : function() {
var registerUrl = kc.createRegisterUrl();
var ref = window.open(registerUrl, '_blank', 'location=no');
ref.addEventListener('loadstart', function(event) {
if (event.url.indexOf('http://localhost') == 0) {
ref.close();
}
});
},
accountManagement : function() { accountManagement : function() {
var accountUrl = kc.createAccountUrl(); var accountUrl = kc.createAccountUrl();
var ref = window.open(accountUrl, '_blank', 'location=no'); var ref = window.open(accountUrl, '_blank', 'location=no');