Skip to content

Instantly share code, notes, and snippets.

@rattanchauhan
Last active October 13, 2017 15:13
Show Gist options
  • Save rattanchauhan/22cc7900e7461489555c2d89a0cf1e5f to your computer and use it in GitHub Desktop.
Save rattanchauhan/22cc7900e7461489555c2d89a0cf1e5f to your computer and use it in GitHub Desktop.
Securing routes in Extjs 6
Ext.define('App.controller.MainController', {
extend: Ext.app.Controller,
routes: {
'home': {
before: function(action) {
Ext.Ajax.request({
url: Config.endpoints.user,
method: 'GET',
scope: this,
failure: function(data, operation) {
console.dir(data);
console.dir(operation);
if (data.status === 403) {
Ext.MessageBox.alert('Error', 'User is Unauthorized/Forbidden to access this application!');
} else if (data.status !== 401) {
Ext.MessageBox.alert('Error', 'An error has occurred while loading application.Please try again later!');
}
action.stop(true);
},
success: function(data) {
console.log('success getting user roles');
var response = Ext.JSON.decode(data.responseText);
var authorized = false;
for (var i = 0; i < response.rights.length; i++) {
if (response.rights[i] === Config.userRole) {
authorized = true;
break;
}
}
if (authorized === true) {
if (!ConfigUtils.configuration) {
ConfigUtils.loadConfig();
}
action.resume();
} else {
Ext.MessageBox.alert('Error', 'User is not Authorized to access this application!');
}
}
});
},
action: 'showMainView'
},
'login': 'showLogin'
},
showMainView: function() {
Ext.create('App.view.main.MainView').show();
},
showLogin: function() {
var login = Ext.ComponentQuery.query('window[itemId=loginwindow]');
if (login.length === 0) {
Ext.create('App.view.main.LoginWindow').show();
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment