Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.