Custom notification for AUTH_ERROR, status 403 - admin-on-rest

In authClient.js
if (type === AUTH_ERROR) {
const { status } = params;
if (status === 401) {
localStorage.removeItem('token');
return Promise.reject();
}
if (status === 403) {
// Show custom notification here on the Snackbar
}
return Promise.resolve();
}
Any tips on how to do show a custom notification will be greatly appreciated.

maybe you could use follow link as a stage to resolve your problem https://github.com/marmelab/admin-on-rest/issues/180
Good luck!

Related

Can I create a custom error at Cypress.io?

I have tried all sorts of techniques but none create a custom error message.
Has anyone managed to crack the problem?
const { statusCode, body } = serverRequest.response;
if (statusCode !== 200 || statusCode !== 201) {
const { errorKey, subErrorKey, errorMsg } = body?.error || {};
Cypress.runner.stop();
throw new Error(`${errorKey}->${subErrorKey}-${errorMsg}`);
}
throw new Error(...) by itself will stop the current current test, or all tests if it's in a before() or beforeEach().
before(() => {
...
const { statusCode, body } = serverRequest.response;
if (statusCode !== 200 || statusCode !== 201) {
const { errorKey, subErrorKey, errorMsg } = body?.error || {};
throw new Error(`${errorKey}->${subErrorKey}-${errorMsg}`);
}
...
})
Cypress will stop with something like "because the error occurred in 'before()' we will not perform any tests".
Throwing the new error is enough to stop the case. I think your code just needs to remove Cypress.runner.stop();, because it shuts everything down.
const { statusCode, body } = serverRequest.response;
if (statusCode !== 200 || statusCode !== 201) {
const { errorKey, subErrorKey, errorMsg } = body?.error || {};
throw new Error(`${errorKey}->${subErrorKey}-${errorMsg}`);
}

Circular dependencies in Titanium/ RequireJs

I have some circular dependencies in my Titanium application like so:
index.js
var Auth = require('Auth')
Auth.js
var PopUp = require('PopUp');
function isLoggedIn() {
// some logic e.g. return userName !== null
};
function authorise() {
if (isLoggedIn()) {
return true;
} else {
return PopUp.authorise();
}
}
PopUp
var Auth = require("Auth");
function authorise() {
// some code asking user to login
}
function showSecurePopUp() {
if (Auth.isLoggedIn()) {
// show secure pop up
}
}
As you can see we have a circular dependency. Auth needs PopUp and PopUp needs Auth.
This creates a circular dependency and thus the following error message:
[ERROR] [iphone, 10.3.3, 192.168.0.64]
Type: RangeError
Message: Maximum call stack size exceeded.
File: /iphone/Auth.js.js
Line: 24
How can I solve the issue of circular dependencies in a Titanium Alloy app?
I think this could be the way, you do the following changes in you project and this should solve the problem.
Alloy.js
var Auth = require("Auth");
var PopUp = require('PopUp');
Index.js
Auth.authorise();
Auth.js
var isLoggedIn = function() {
// some logic e.g. return userName !== null
Ti.API.info('isLoggedIn');
return false;
};
exports.authorise = function() {
if (isLoggedIn()) {
Ti.API.info('authorize isloggedIn');
return true;
} else {
Ti.API.info('authorize not logged In');
return PopUp.authorise();
}
};
exports.isLoggedIn = isLoggedIn;
PopUp.js
exports.authorise =function () {
// some code asking user to login
Ti.API.info('authorize funcition popup ' + Auth.isLoggedIn());
};
function showSecurePopUp() {
if (Auth.isLoggedIn()) {
// show secure pop up
Ti.API.info('isLoggedIn show secure popup');
}
}
Let me know if this works fine and if this is what you wanted. Also if you have some other approach that solves the problem, then let me know that also.
Good Luck & Cheers
Ashish Sebastian

Retrieving of Restful web service values in android for Titanium

We are using the same restful web service code from serviceutility.js for both android and ios. But the service is getting hit and values are retrieved only in ios. The same code is not working in android and we are getting the following error:
[ERROR] : TiExceptionHandler: (main) [2,821093] - In alloy/controllers/home.js:25,32
[ERROR] : TiExceptionHandler: (main) [0,821093] - Message: Uncaught TypeError: Cannot read property 'status' of null
[ERROR] : TiExceptionHandler: (main) [0,821093] - Source: if ("1" == response.status) alert(response.message); else if ("0"
[ERROR] : V8Exception: Exception occurred at alloy/controllers/home.js:25: Uncaught TypeError: Cannot read property 'status' of null.
Titanium SDK is 5.1.2 GA
exports.login = function(user, cb) {
var response = null;
if (Ti.Network.online) {
var xhr = Ti.Network.createHTTPClient({
timeout : 10000,
validatesSecureCertificate : false
});
xhr.onload = function() {// Onload
var responseTxt = this.responseText == '' ? '{}' : this.responseText;
try {
response = JSON.parse(responseTxt);
cb(response, 'SUCCESS');
} catch(e) {
cb(response, 'ERROR');
}
};
xhr.onerror = function(e) {
if (xhr.status === 0) {
cb(response, 'TIMEDOUT');
} else {
cb(response, 'ERROR');
}
};
url = "https://";
var postData = {
employeeId : user.employeeId,
password : user.password
};
xhr.open('POST', url);
xhr.setTimeout(10000);
xhr.setRequestHeader('employeeId', user.employeeId);
xhr.setRequestHeader('password', user.password);
xhr.send();} else {
cb(response, 'NO_NETWORK');
}};
The below code is for index.js file where the actual retrieval of values happen.
if (Ti.Network.online) {
loginUtil.login(user, function(response, status) {
Ti.API.info("status----" + status);
if (response.status == "0") {
Ti.API.info("status== " + response.status);
Ti.App.role = response.role;
Alloy.createController('home', {employeeId:$.userTextField.value,password:$.passwordTextField.value,from:"index"}).getView().open();
} else if (response.status == '1') {
alert(response.message);
} else {
alert("Please enter the correct credentials");
}
});
}
Please help us on this.
Looks like you are ONLY returning a string value instead of the entire response object. Then in your controller you attempt to access the .status property of the response object.
//this line returns the string responseTxt
response = JSON.parse(responseTxt);
Try returning the entire response object instead.
response = JSON.parse(this);
Then in your index.js controller use/ display the status property
alert(response.status);
Your index.js expected response to be an object, but that is only the case where you call callback like this:
response = JSON.parse(responseTxt);
cb(response, 'SUCCESS');
All other places where you call callback the response variable is null, since that is what you initialise it with on the second line.
Your callback returns two parameters, response & status, the second param is never used.
From reading the login function code, you only get to access the response object if status == "SUCCESS"
if(status === "SUCCESS"){
if (response.status == "0") {
Ti.API.info("status== " + response.status);
Ti.App.role = response.role;
Alloy.createController('home', {employeeId:$.userTextField.value,password:$.passwordTextField.value,from:"index"}).getView().open();
} else if (response.status == '1') {
alert(response.message);
} else {
alert("Please enter the correct credentials");
}
}
else {
alert("whoops, please try again !"); // a more generic message.
}

Prevent from route change in AngularJS

$rootScope.$on("$locationChangeStart", function(event, next, current) {
var partsOfUrl = next.split('/');
var isLogin = false;
if(partsOfUrl.indexOf("signin") > 0) {
isLogin = true;
}
var myDataPromise = loginService.getData();
myDataPromise.then(function(data) { // this is only run after $http completes
if(!isLogin) {
if(data.logout) {
$location.url("pages/signin");
event.preventDefault();
} else{}
} else {
if(data.logout) {
} else {
}
}
});
console.log(next);
});
This is the code i used to check user authentication and prevent the protected areas. But problem here is if a user try to access protected then immediately browser shows the secure page and then get back to login page instead of redirecting to login page first. I think that's because of user authentication process is done through an Ajax call so the program never holds for the response. What's the wrong here and how should i get rid of it ?
Try with httpInterceptor (from mean.io stack)
btw the server should response with a 401 status
'use strict';
angular.module('mean-factory-interceptor',[])
.factory('httpInterceptor', ['$q','$location',function ($q,$location) {
return {
'response': function(response) {
if (response.status === 401) {
$location.path('/signin');
return $q.reject(response);
}
return response || $q.when(response);
},
'responseError': function(rejection) {
if (rejection.status === 401) {
$location.url('/signin');
return $q.reject(rejection);
}
return $q.reject(rejection);
}
};
}
])
//Http Intercpetor to check auth failures for xhr requests
.config(['$httpProvider',function($httpProvider) {
$httpProvider.interceptors.push('httpInterceptor');
}]);

Facebook App/Page Tab with xmlHttpRequest doesn't work in Firefox

I have e really big problem with firefox and facebook.
I mad an application on my webserver which uses xmlHttpRequest. I added this application to a facebook tab on a test facebook page. It works with IE, Chrome, Safari but not with firefox.
The request just keeps loading until timeout.
The JS functions i'm using:
function createXmlHttpRequest() {
try {
if (typeof ActiveXObject != 'undefined') {
return new ActiveXObject('Microsoft.XMLHTTP');
} else if (window["XMLHttpRequest"]) {
return new XMLHttpRequest();
}
} catch (e) {
changeStatus(e);
}
return null;
};
function downloadUrl(url, callback) {
var status = -1;
var request = createXmlHttpRequest();
if (!request) {
return false;
}
request.onreadystatechange = function() {
if (request.readyState == 4) {
try {
status = request.status;
} catch (e) {
}
if (status == 200) {
callback(request.responseXML, request.status);
request.onreadystatechange = function() {};
}
}
}
request.open('GET', url, f);
try {
request.send(null);
} catch (e) {
changeStatus(e);
}
};
function xmlParse(str) {
if (typeof ActiveXObject != 'undefined' && typeof GetObject != 'undefined') {
var doc = new ActiveXObject('Microsoft.XMLDOM');
doc.loadXML(str);
return doc;
}
if (typeof DOMParser != 'undefined') {
return (new DOMParser()).parseFromString(str, 'text/xml');
}
return createElement('div', null);
}
function downloadScript(url) {
var script = document.createElement('script');
script.src = url;
document.body.appendChild(script);
}
i call it through downloadUrl()
The Headers from the requested files:
header('Access-Control: allow <*>');
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET');
header('Access-Control-Allow-Headers: X-PINGOTHER');
header("Content-type: text/xml");
i really tried everything, but it won't work in firefox...
what i've noticed: by observing firebug while loading this app in the facebook tab i could see that facebook is not requesting the adress given in the source, but other ones like: https://0-317.channel.facebook.com/pull?channel=p_1495135952&seq=389&partition=7&clientid=420773d2&cb=682&idle=0&state=active
i think it's surely firefox cross domain policy... but how can i solve this problem?
Anyone had the same problems ?
I thank you in advance.
Greetings
ok, found the problem.
Facebook is using UTF-8, but my page wasn't. So the stopped at the umlauts.
So it wasn't the Request at all.

Resources