Problem with decoding token - jwtHelper - ANgularjs - angularjs

I have a problem with jwtHelper.
After coding the data, I want to decode using:
myServices.service('checkToken', function( store, jwtHelper){
this.payload = function(){
var token = store.get ('token');
token = jwtHelper.decodeToken(token);
return token;
}
});
However, the console receives such an error:
TypeError: Cannot read property 'split' of undefined
at Object.decodeToken (angular-jwt.js:233)
at Object.payload (services.js:11)
Line 11: token = jwtHelper.decodeToken(token);

Are you sure the store is returning the token? Also I see a space between get and the paranthesis.

Related

gRPC Any type - Uncaught TypeError: deserialize is not a function

I'm having an issue in gRPC that's been driving me crazy. I have a .NET gRPC service that my ReactJS client is connecting to - this works fine. I'm subscribing to my stream, and I get data over the stream as expected. I'm having an issue deserializing "google.protobuf.Any" types that exist in my messages. The one I'm receiving looks like this:
message PointNotification {
KindsOfPointNotification Kind = 1;
google.protobuf.Any NotificationData = 2;
}
Inside of the ReactJS client I am doing the following:
useEffect(() => {
console.log("Subscribing to point stream");
var pointStream = client.subscribeToNotificationStream(new Empty(), {});
pointStream.on("data", (response) => {
switch (response.getKind())
{
case KindsOfPointNotification.KINDS_OF_POINT_NOTIFICATION_POINT_ADDED_OR_UPDATED:
const any = response.getNotificationdata();
console.log(any);
const bar = any.unpack(NotificationPointAddedOrUpdated.deserializeBinary, any.getTypeName());
console.log(bar);
break;
case KindsOfPointNotification.KINDS_OF_POINT_NOTIFICATION_POINT_REMOVED:
console.log("Point removed");
break;
}
});
The "any" variable looks right in the browser logs. The payload, and the typeName are correct. However when I go to unpack "any" I get the following error (screenshot):
TypeError in browser
Uncaught TypeError: deserialize is not a function
This is despite the fact that the generated _pb file has the following method:
/**
* Deserializes binary data (in protobuf wire format).
* #param {jspb.ByteSource} bytes The bytes to deserialize.
* #return {!proto.Point.NotificationPointAddedOrUpdated}
*/
proto.Point.NotificationPointAddedOrUpdated.deserializeBinary = function(bytes) {
var reader = new jspb.BinaryReader(bytes);
var msg = new proto.Point.NotificationPointAddedOrUpdated;
return proto.Point.NotificationPointAddedOrUpdated.deserializeBinaryFromReader(msg,
reader);
};
According to The documentation this is correct way to do it. What am I missing? Why am I getting a deserialize error?
Edit: I have one other thing to add. If I change the following line:
const bar = any.unpack(NotificationPointAddedOrUpdated.deserializeBinary, any.getTypeName());
To:
const bar = any.unpack(NotificationPointAddedOrUpdated.deserializeBinary(), any.getTypeName());
The error I get in the browser changes to:
Uncaught TypeError: _proto_pointdata_pb_js__WEBPACK_IMPORTED_MODULE_3___default(...).deserializeBinary is not a function
The following issue led me to the solution: https://github.com/protocolbuffers/protobuf/issues/5482
The previous dev was not encoding data in base64, and that was causing deserialize issues in JavaScript.

ReactJS - You may need an appropriate loader to handle this file type

I'm getting the error when I'm using '?'. What should i use instead?
The error is:
Module parse failed: Unexpected token You may need an appropriate loader to handle this file type.
const accessToken = user?.accessToken
if(accessToken){
const decodedAccessToken = decode(accessToken)
if(decodedAccessToken.exp * 1000 < new Date().getTime()){
console.log(decodedAccessToken.exp);
renewAccessToken(user.user._id)
}
}
I solved the problem. We have to use like this:
const accessToken = user ? user.accessToken : null;

angular.js:14195 Error: [$resource:badmember] Dotted member path "#" is invalid

I'm using angular.resource js
There I'm trying to access get service which has query param triggered from a form search.
If user search with only "#" in input field, it goes as query param which starts with "#" character then getting above exception
Thanks in advance.
Because of below code in angular.resource js
// Helper functions and regex to lookup a dotted path on an object
// stopping at undefined/null. The path must be composed of ASCII
// identifiers (just like $parse)
var MEMBER_NAME_REGEX = /^(\.[a-zA-Z_$#][0-9a-zA-Z_$#]*)+$/;
function isValidDottedPath(path) {
return (path != null && path !== '' && path !== 'hasOwnProperty' &&
MEMBER_NAME_REGEX.test('.' + path));
}
function lookupDottedPath(obj, path) {
if (!isValidDottedPath(path)) {
throw $resourceMinErr('badmember', 'Dotted member path "#{0}" is invalid.', path);
}
var keys = path.split('.');
for (var i = 0, ii = keys.length; i < ii && angular.isDefined(obj); i++) {
var key = keys[i];
obj = (obj !== null) ? obj[key] : undefined;
}
return obj;
}
If you really want ot make this as your url params. Try escape the # char as this has special meaning in $resource library
Normally it happens when we have only # as the value, where it searches for the value of string after "#" in the request body.
As it is a get method, where angular resource ignores it. Very weird behavior.
AngularJs doc: https://code.angularjs.org/1.5.11/docs/api/ngResource/service/$resource
If the parameter value is prefixed with #, then the value for that parameter will be extracted from the corresponding property on the data object (provided when calling a "non-GET" action method). For example, if the defaultParam object is {someParam: '#someProp'} then the value of someParam will be data.someProp. Note that the parameter will be ignored, when calling a "GET" action method (i.e. an action method that does not accept a request body)
So before calling any method in resource try escape the # symbol:
paramValue = paramValue.replace(/#/gi, '\\#');
And again you can remove the scape before api call happens in request method of interceptor service for $httpProvider.
configParams = configParams.replace(/\\#/gi, '#');
Let me know if need any more help.
Thanks

AngularJS: Persistent Error with $sessionStorage

I am trying to store the count of the score using $sessionStorage, for some reason, the assignment operation. Also, I cannot assign $sessionStorage variable with anything else as well.
$sessionStorage.score = parseInt($sessionStorage.score) + 1;//gives error
$scope.x = 0;
$sessionStorage.score = $scope.x; // also gives error
Also,
$sessionStorage.score = JSON.stringify($scope.x);// gives error
how to resolve this?
Use "sessionStorage" instead of "$sessionStorage".

"Cannot read property 'transport' of undefined " in karma-jasmine Unit Test

I built a project with AngularJs and made unit test with Karma and jasmine.
One of my controller in Angular project need to refer socket.io to transport data with server. And i wrote the unit test case for this controller. Ana add 'socket.io.js' file into 'karma.conf.js', When i tested it with karma, an error throw out:
Uncaught TypeError: Cannot read property 'transport' of undefined, this error throw in below statement in adapter.js in karma-jasmine node package:
var getCurrentTransport = function() {
// probably running in debug.html (there's no socket.io)
if (!window.parent.io) {
return null;
}
var location = window.parent.location;
return window.parent.io.sockets[location.protocol + '//' + location.host].transport.name;
};
i'm not quit understand what this code mean, i found that the window.parent.io.sockets is an empty object, so added another statement to return null so that it won't throw an error.
var location = window.parent.location;
if(!window.parent.io.sockets[location.protocol + '//' + location.host]){
return null;
}
return window.parent.io.sockets[location.protocol + '//' + location.host].transport.name;
i'm not sure whether this change is right? Anyone know how to fix this problem?
#Zacho add these statement before return... in your adapter.js .
if(!window.parent.io.sockets[location.protocol + '//' + location.host]){
return null;
}
these just hack method,Although I don't think this is a good solution, i didn't find why this problem occur.

Resources