-
-
Save mcmatrix/ce439ad8332efc031adf44faceeab300 to your computer and use it in GitHub Desktop.
ExtJS 6: JSON Web Token API Login with Promises
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Ext.define('App.security.Firewall', { | |
| singleton: true, | |
| requires: [ | |
| 'App.security.TokenStorage' | |
| ], | |
| login: function(username, password) { | |
| var deferred = new Ext.Deferred(); | |
| Ext.Ajax.request({ | |
| url: App.server.Router.generate('authenticate'), | |
| method: 'POST', | |
| jsonData: { | |
| 'username': username, | |
| 'password': password | |
| }, | |
| success: function (response) { | |
| var data = Ext.decode(response.responseText); | |
| if (data.token) { | |
| App.security.TokenStorage.save(data.token); | |
| deferred.resolve(data, response); | |
| } else { | |
| deferred.reject(data, response); | |
| } | |
| }, | |
| failure: function (response) { | |
| var data = Ext.decode(response.responseText); | |
| App.security.TokenStorage.clear(); | |
| deferred.reject(data, response); | |
| } | |
| }); | |
| return deferred.promise; | |
| }, | |
| logout: function(callback) { | |
| App.security.TokenStorage.clear(); | |
| callback(); | |
| } | |
| }, function () { | |
| Ext.Ajax.on('beforerequest', function(conn, options) { | |
| if (App.security.Firewall.isLoggedIn()) { | |
| options.headers = options.headers || {}; | |
| options.headers['Authorization'] = 'Bearer ' + App.security.TokenStorage.retrieve(); | |
| } | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Ext.define('App.view.login.LoginController', { | |
| extend: 'Ext.app.ViewController', | |
| alias: 'controller.login', | |
| onLoginClick: function() { | |
| var data = this.getView().down('form').getValues(); | |
| App.security.Firewall.login(data.username, data.password).then(function() { | |
| this.getView().destroy(); | |
| Ext.create({ | |
| xtype: 'app-main' | |
| }); | |
| }.bind(this), function(data) { | |
| Ext.Msg.alert('Error', data.message || 'An error occurred while logging in.'); | |
| }); | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Ext.define('App.security.TokenStorage', { | |
| singleton: true, | |
| storageKey: 'json-web-token', | |
| clear: function () { | |
| localStorage.removeItem(this.storageKey); | |
| }, | |
| retrieve: function() { | |
| return localStorage.getItem(this.storageKey); | |
| }, | |
| save: function (token) { | |
| localStorage.setItem(this.storageKey, token); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment