Push notifications (Parse/Cloud Code) not showing username for comments, or any likes - javascript

I'm using Parse and Cloud Code for the first time to update a photo-sharing app that was built with Parse and Objective-C for iOS 8. I've followed Parse's instructions for setting up push notifications and was successfully able to receive a notification when a photo was commented on, but it is only delivering the "else" statement that says "Someone has commented on your photo" (instead of giving user's name). Aside from that, 'like' notifications aren't being pushed at all.
Here's my cloud code (JavaScript) that I modeled after Parse's AnyPic tutorial. I also have an installation file and a "main" file that point to this one and the installation. What am I missing here?
Parse.Cloud.beforeSave('Activity', function(request, response) {
var currentUser = request.user;
var objectUser = request.object.get('fromUser');
if(!currentUser || !objectUser) {
response.error('An Activity should have a valid fromUser.');
} else if (currentUser.id === objectUser.id) {
response.success();
} else {
response.error('Cannot set fromUser on Activity to a user other than the current user.');
}
});
Parse.Cloud.afterSave('Activity', function(request) {
// Only sends push notifications for new activities.
if (request.object.existed()) {
return;
}
var toUser = request.object.get("toUser");
if (!toUser) {
throw "Undefined toUser. Skipping push for Activity " + request.object.get('type') + " : " + request.object.id;
return;
}
var query = new Parse.Query(Parse.Installation);
query.equalTo('user', toUser);
Parse.Push.send({
where: query, // Set our Installation query.
data: alertPayload(request)
}).then(function() {
// Push was successful
console.log('Sent push.');
}, function(error) {
throw "Push Error " + error.code + " : " + error.message;
});
});
var alertMessage = function(request) {
var message = "";
if (request.object.get("type") === "comment") {
if (request.user.get('displayName')) {
message = request.user.get('displayName') + ': ' + request.object.get('content').trim();
} else {
message = "Someone left a comment on your photo.";
}
} else if (request.object.get("type") === "like") {
if (request.user.get('displayName')) {
message = request.user.get('displayName') + ' likes your photo.';
} else {
message = 'Someone likes your photo.';
}
}
// Trim our message to 140 characters.
if (message.length > 140) {
message = message.substring(0, 140);
}
return message;
}
var alertPayload = function(request) {
var payload = {};
if (request.object.get("type") === "comment") {
return {
alert: alertMessage(request), // Sets the alert message.
badge: 'Increment', // Increments the target device's badge count.
// The following keys help Cosplace to load the photo corresponding to this push notification.
p: 'a', // Payload Type: Activity
t: 'c', // Activity Type: Comment
fu: request.object.get('fromUser').id, // From User
pid: request.object.id // Photo Id
};
} else if (request.object.get("type") === "like") {
return {
alert: alertMessage(request), // Sets the alert message.
// The following keys help Cosplace to load the photo corresponding to this push notification.
p: 'a', // Payload Type: Activity
t: 'l', // Activity Type: Like
fu: request.object.get('fromUser').id, // From User
pid: request.object.id // Photo Id
};
}
}
Thanks!
EDIT: Here's the Objective-C code from my AppDelagate.m file (I took out the Parse IDs):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[Parse setApplicationId: #""
clientKey:#""];
[PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];
// Reset application icon badge number when app is launched.
if (application.applicationIconBadgeNumber != 0) {
application.applicationIconBadgeNumber = 0;
[[PFInstallation currentInstallation] saveInBackground];
}
// Registers the current device for push notifications.
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
if (application.applicationState != UIApplicationStateBackground) {
// Track an app open here if we launch with a push, unless
// "content_available" was used to trigger a background push (introduced
// in iOS 7). In that case, we skip tracking here to avoid double
// counting the app-open.
BOOL preBackgroundPush = ![application respondsToSelector:#selector(backgroundRefreshStatus)];
BOOL oldPushHandlerOnly = ![self respondsToSelector:#selector(application:didReceiveRemoteNotification:fetchCompletionHandler:)];
BOOL noPushPayload = ![launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (preBackgroundPush || oldPushHandlerOnly || noPushPayload) {
[PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];
}
}
// If the registration is successful, this callback method will be executed. This method informs Parse about this new device.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Reset application icon badge number when app is launched.
if (application.applicationIconBadgeNumber != 0) {
application.applicationIconBadgeNumber = 0;
// Store the deviceToken in the current installation and save it to Parse.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
// [PFInstallation.currentInstallation setObject:PFUser.currentUser forKey:#"user"];
[currentInstallation setDeviceTokenFromData:deviceToken];
// Associate the device with a user
PFInstallation *installation = [PFInstallation currentInstallation];
installation[#"user"] = [PFUser currentUser];
[installation saveInBackground];
[currentInstallation saveInBackground];
// Create Parse Installation query
PFQuery *pushQuery = [PFInstallation query];
[pushQuery whereKey:#"deviceType" equalTo:#"ios"];
}
}
// So when logged out notifications stop being received.
- (void)logOut {
// Unsubscribe from push notifications by removing the user association from the current installation.
[[PFInstallation currentInstallation] removeObjectForKey:#"user"];
[[PFInstallation currentInstallation] saveInBackground];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[PFPush handlePush:userInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:#"AppDelegateApplicationDidReceiveRemoteNotification" object:nil userInfo:userInfo];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Clear badge and update installation for auto-incrementing badges.
if (application.applicationIconBadgeNumber != 0) {
application.applicationIconBadgeNumber = 0;
[[PFInstallation currentInstallation] saveInBackground];
}
}

Related

Simple MSAL Login/Authentication in JavaScript

I'm trying to do a simple login to Azure AD using the MSAL for JavaScript v2.0 library. We want users to be able to authenticate into our site with their work Microsoft accounts. All I need to do is be able to authenticate/login the user via Microsoft, and if they can login via their work Microsoft account, then they're granted access to our site.
I'm using the Javascript library and have followed the code from the Github page and while the login prompt is coming up, afterwards I have no idea how to check if the user is signed in.
Here's the code I'm using, which is basically what's in the sample code from Github:
<script type="text/javascript" src="https://alcdn.msauth.net/browser/2.15.0/js/msal-browser.min.js"></script>
<script type="text/javascript">
const msalConfig = {
auth: {
clientId: "[ClientID goes here]",
authority: "https://login.microsoftonline.com/[tenant ID]",
knownAuthorities: ["login.microsoftonline.com"],
protocolMode: "OIDC",
redirectUri: "[page on our site that doesn't have MSAL auth, listed in Azure Reply URLs]"
},
cache: {
cacheLocation: "sessionStorage", // This configures where your cache will be stored
storeAuthStateInCookie: true, // Set this to "true" if you are having issues on IE11 or Edge
},
system: {
loggerOptions: {
loggerCallback: (level, message, containsPii) => {
if (containsPii) {
return;
}
switch (level) {
case msal.LogLevel.Error:
console.error(message);
return;
case msal.LogLevel.Info:
console.info(message);
return;
case msal.LogLevel.Verbose:
console.debug(message);
return;
case msal.LogLevel.Warning:
console.warn(message);
return;
}
}
}
}
};
// Add here scopes for id token to be used at MS Identity Platform endpoints.
const loginRequest = {
scopes: ["User.Read"]
};
const silentRequest = {
scopes: ["openid", "profile", "User.Read"]
};
const ua = window.navigator.userAgent;
const msie = ua.indexOf("MSIE ");
const msie11 = ua.indexOf("Trident/");
const msedge = ua.indexOf("Edge/");
const isIE = msie > 0 || msie11 > 0;
const isEdge = msedge > 0;
let signInType;
let accountId = "";
let credType = "";
// Create the main myMSALObj instance
const myMSALObj = new msal.PublicClientApplication(msalConfig);
// Register Callbacks for Redirect flow
myMSALObj.handleRedirectPromise().then(handleResponse).catch((error) => {
console.log(error);
});
function handleResponse(resp) {
alert("beginning handleResponse");
if (resp !== null) {
accountId = resp.account.homeAccountId;
credType = resp.account.credentialType;
myMSALObj.setActiveAccount(resp.account);
alert("response not null (already auth), accountId: " + accountId + ", credType: " + credType);
}
else {
const currentAccounts = myMSALObj.getAllAccounts();
if (!currentAccounts || currentAccounts.length < 1) {
alert("currentAccounts null/empty, going to signIn");
signIn("loginRedirect");
//return;
}
else if (currentAccounts.length > 1) {
// add choose account code here
alert("currentAccounts has multiple");
}
else if (currentAccounts.length === 1) {
const activeAccount = currentAccounts[0];
myMSALObj.setActiveAccount(activeAccount);
accountId = activeAccount.homeAccountId;
credType = activeAccount.credentialType;
alert("currentAccounts == 1; accountId: " + accountId + ", credType: " + credType);
}
}
}
async function signIn(method) {
signInType = isIE ? "loginRedirect" : method;
if (signInType === "loginPopup") {
return myMSALObj.loginPopup(loginRequest).then(handleResponse).catch(function (error) {
console.log(error);
});
}
else if (signInType === "loginRedirect") {
return myMSALObj.loginRedirect(loginRequest);
}
}
function signOut() {
const logoutRequest = {
account: myMSALObj.getAccountByHomeId(accountId)
};
myMSALObj.logoutRedirect(logoutRequest);
}
async function getTokenPopup(request, account) {
request.account = account;
return await myMSALObj.acquireTokenSilent(request).catch(async (error) => {
console.log("silent token acquisition fails.");
if (error instanceof msal.InteractionRequiredAuthError) {
console.log("acquiring token using popup");
return myMSALObj.acquireTokenPopup(request).catch(error => {
console.error(error);
});
}
else {
console.error(error);
}
});
}
// This function can be removed if you do not need to support IE
async function getTokenRedirect(request, account) {
request.account = account;
return await myMSALObj.acquireTokenSilent(request).catch(async (error) => {
console.log("silent token acquisition fails.");
if (error instanceof msal.InteractionRequiredAuthError) {
// fallback to interaction when silent call fails
console.log("acquiring token using redirect");
myMSALObj.acquireTokenRedirect(request);
}
else {
console.error(error);
}
});
}
So what happens upon going to this page is I get the two alerts saying "beginning handleResponse" and then "currentAccounts null/empty, going to signIn."
Then I'm redirected to MS sign-in page which I do with my work MS account. This succeeds.
I'm then redirected to the site I have listed in Azure Reply URLs, another page on our site that isn't secure and has no Azure login code.
The problem is I have no idea where to check that the user is signed in. If I try and check immediately after the signIn("loginRedirect") call in the handleResponse() function on the first page, the code never gets hit apparently. If I try and check on the page I'm redirected to, by instantiating the MSAL object and calling getAllAccounts(), this returns null.
It seems maybe on the page I'm redirected to I could call the ssoSilent() function (seems like this can check if user is authenicated?), but this requires a username/AccountId parameter. Well I don't frickin know this if a user hasn't (possibly) been authenticated yet! I don't really understand that.
So I don't know. It's probably something stupid I'm doing but I'm a pretty basic JavaScript person and am pretty much a total noob with authenication stuff. Any help would be epic.

Function can't be called for some reason -- using localStorage for a script

What I'm trying to do is console log the amount of unread messages within a script. The rules are: if the chat window is minimized, add one when a message is received, and, when the chat window is maximized, set the count back to zero. All stored to be persistent so page reloads or navigation won't change the count. Here's my script.
count = 0;
let is_minimized = true;
var chatServiceOptions = {
license: "*SECRET*", // ie '1234567'
group: 4, // ie 22,
customer: {
name: "Development Account",
timezone: "Europe/London"
},
plugins: [
// this is where the code starts
function countAdd() {
localStorage.count = count++;
},
//this is just calling the API, when a chat is available
function (chatService) {
chatService.register('ready', function() {
// when chat window is closed, ensure is_minimized is true
chatService.events.on('LC_on_chat_window_minimized', data => {
is_minimized = true
});
// when chat window is opened, set is_minimized to false
chatService.events.on('LC_on_chat_window_opened', data => {
count = 0
is_minimized = false
});
chatService.events.on('LC_on_message', function(data) {
// set the window minimized to a variable
var LC_API = LC_API || {};
LC_API.on_chat_window_minimized = function() {
is_minimized = true;
};
// output the data of the incoming/outgoing message
console.log(data);
// check that it comes from an agent and the chat window is minimized
// if it is, add +1 to unread message count
if (data.user_type ==='agent' && is_minimized == true) {
countAdd(); }
// if its maximized, set unread count to zero
else { localStorage.count = 0; };
// display number of unread messages
console.log('unread messages: ' + localStorage.getItem('count'));
});
})
}
],
};
var LC_API = LC_API || {};
LC_API.on_message = function(data) {
console.log(data)
alert("Message " + data.text + " sent by " + data.user_type);
};
</script>
Console is giving me:
Uncaught ReferenceError: countAdd is not defined
You need to define countAdd and the other function first, then pass them into the plugins array. Since you are defining them inline, the second function isn't aware of countAdd
function countAdd() { }
function otherFn() {
// uses countAdd in here
}
var chatServiceOptions = {
license: "*SECRET*", // ie '1234567'
group: 4, // ie 22,
customer: {
name: "Development Account",
timezone: "Europe/London"
},
// provide both functions to the plugins property
plugins: [countAdd, otherFn]
}

ErrorAccessDenied delete attachments with microsoft graph

I'm trying to remove attachments with Microsoft Graph. I have the following function:
getAccessToken(function(accessToken) {
if (accessToken) {
// Create a Graph client
var client = MicrosoftGraph.Client.init({
authProvider: done => {
// Just return the token
done(null, accessToken);
}
});
console.log(files);
files.forEach(function(file) {
client
.api(
"/me/messages/" +
file.msg +
"/attachments/" +
file.attachment
)
.delete((err, res) => {
if (err) {
console.log(err);
return;
} else {
console.log(res);
$("#attachment_" + file.list).remove();
}
});
});
} else {
var error = {
responseText: "Could not retrieve access token"
};
}
});
// OAUTH FUNCTIONS =============================
function buildAuthUrl() {
// Generate random values for state and nonce
sessionStorage.authState = guid();
sessionStorage.authNonce = guid();
var authParams = {
response_type: "id_token token",
client_id: appId,
redirect_uri: redirectUri,
scope: scopes,
state: sessionStorage.authState,
nonce: sessionStorage.authNonce,
response_mode: "fragment"
};
return authEndpoint + $.param(authParams);
}
function handleTokenResponse(hash) {
// If this was a silent request remove the iframe
$("#auth-iframe").remove();
// clear tokens
sessionStorage.removeItem("accessToken");
sessionStorage.removeItem("idToken");
var tokenresponse = parseHashParams(hash);
// Check that state is what we sent in sign in request
if (tokenresponse.state != sessionStorage.authState) {
sessionStorage.removeItem("authState");
sessionStorage.removeItem("authNonce");
// Report error
window.location.hash =
"#error=Invalid+state&error_description=The+state+in+the+authorization+response+did+not+match+the+expected+value.+Please+try+signing+in+again.";
return;
}
sessionStorage.authState = "";
sessionStorage.accessToken = tokenresponse.access_token;
// Get the number of seconds the token is valid for,
// Subract 5 minutes (300 sec) to account for differences in clock settings
// Convert to milliseconds
var expiresin =
(parseInt(tokenresponse.expires_in) - 300) * 1000;
var now = new Date();
var expireDate = new Date(now.getTime() + expiresin);
sessionStorage.tokenExpires = expireDate.getTime();
sessionStorage.idToken = tokenresponse.id_token;
validateIdToken(function(isValid) {
if (isValid) {
// Re-render token to handle refresh
renderTokens();
// Redirect to home page
window.location.hash = "#";
} else {
clearUserState();
// Report error
window.location.hash =
"#error=Invalid+ID+token&error_description=ID+token+failed+validation,+please+try+signing+in+again.";
}
});
}
function validateIdToken(callback) {
// Per Azure docs (and OpenID spec), we MUST validate
// the ID token before using it. However, full validation
// of the signature currently requires a server-side component
// to fetch the public signing keys from Azure. This sample will
// skip that part (technically violating the OpenID spec) and do
// minimal validation
if (
null == sessionStorage.idToken ||
sessionStorage.idToken.length <= 0
) {
callback(false);
}
// JWT is in three parts seperated by '.'
var tokenParts = sessionStorage.idToken.split(".");
if (tokenParts.length != 3) {
callback(false);
}
// Parse the token parts
var header = KJUR.jws.JWS.readSafeJSONString(
b64utoutf8(tokenParts[0])
);
var payload = KJUR.jws.JWS.readSafeJSONString(
b64utoutf8(tokenParts[1])
);
// Check the nonce
if (payload.nonce != sessionStorage.authNonce) {
sessionStorage.authNonce = "";
callback(false);
}
sessionStorage.authNonce = "";
// Check the audience
if (payload.aud != appId) {
callback(false);
}
// Check the issuer
// Should be https://login.microsoftonline.com/{tenantid}/v2.0
if (
payload.iss !==
"https://login.microsoftonline.com/" +
payload.tid +
"/v2.0"
) {
callback(false);
}
// Check the valid dates
var now = new Date();
// To allow for slight inconsistencies in system clocks, adjust by 5 minutes
var notBefore = new Date((payload.nbf - 300) * 1000);
var expires = new Date((payload.exp + 300) * 1000);
if (now < notBefore || now > expires) {
callback(false);
}
// Now that we've passed our checks, save the bits of data
// we need from the token.
sessionStorage.userDisplayName = payload.name;
sessionStorage.userSigninName =
payload.preferred_username;
// Per the docs at:
// https://azure.microsoft.com/en-us/documentation/articles/active-directory-v2-protocols-implicit/#send-the-sign-in-request
// Check if this is a consumer account so we can set domain_hint properly
sessionStorage.userDomainType =
payload.tid === "9188040d-6c67-4c5b-b112-36a304b66dad"
? "consumers"
: "organizations";
callback(true);
}
function makeSilentTokenRequest(callback) {
// Build up a hidden iframe
var iframe = $("<iframe/>");
iframe.attr("id", "auth-iframe");
iframe.attr("name", "auth-iframe");
iframe.appendTo("body");
iframe.hide();
iframe.load(function() {
callback(sessionStorage.accessToken);
});
iframe.attr(
"src",
buildAuthUrl() +
"&prompt=none&domain_hint=" +
sessionStorage.userDomainType +
"&login_hint=" +
sessionStorage.userSigninName
);
}
// Helper method to validate token and refresh
// if needed
function getAccessToken(callback) {
var now = new Date().getTime();
var isExpired =
now > parseInt(sessionStorage.tokenExpires);
// Do we have a token already?
if (sessionStorage.accessToken && !isExpired) {
// Just return what we have
if (callback) {
callback(sessionStorage.accessToken);
}
} else {
// Attempt to do a hidden iframe request
makeSilentTokenRequest(callback);
}
}
Access token
eyJ0eXAiOiJKV1QiLCJub25jZSI6IkFRQUJBQUFBQUFDNXVuYTBFVUZnVElGOEVsYXh0V2pUUEF3aGltT2hjOXAxUkdiSnVjTDcyd0pjSFdTd1lpUXNFdDdqXzgxRVd6UXhvaWRaWnVXU2d5VS1HWXRqNFFNa3JjMUNpeFFTWElNRVpYQWhSUlJXZENBQSIsImFsZyI6IlJTMjU2IiwieDV0Ijoid1VMbVlmc3FkUXVXdFZfLWh4VnRESkpaTTRRIiwia2lkIjoid1VMbVlmc3FkUXVXdFZfLWh4VnRESkpaTTRRIn0.eyJhdWQiOiJodHRwczovL2dyYXBoLm1pY3Jvc29mdC5jb20iLCJpc3MiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC9iNmFjNDlmNy1iMTNlLTQyOWMtYmI4NS0wODQ4OTY5NTA2OTkvIiwiaWF0IjoxNTQ0MTkyODUxLCJuYmYiOjE1NDQxOTI4NTEsImV4cCI6MTU0NDE5Njc1MSwiYWNjdCI6MCwiYWNyIjoiMSIsImFpbyI6IjQyUmdZT0JiMFhGQjBNSHFySW5FbVU5aEc4T1p6MTllOHpIRld1S0sxbldqQzVrOFAvd0IiLCJhbXIiOlsicHdkIl0sImFwcF9kaXNwbGF5bmFtZSI6IkZvcm1wcmVzcyBjbGVhciBhdHRhY2htZW50cyIsImFwcGlkIjoiNTY0NzBlMjctZTAxNC00Zjg4LWEzY2QtZjQxODNlZjBhMjkxIiwiYXBwaWRhY3IiOiIwIiwiZmFtaWx5X25hbWUiOiJLYXJsc3NvbiIsImdpdmVuX25hbWUiOiJDaHJpc3RvcGhlciIsImlwYWRkciI6IjE1NS40LjE5NC4xMzQiLCJuYW1lIjoiQ2hyaXN0b3BoZXIgS2FybHNzb24iLCJvaWQiOiI5Y2JlMjBlYy1iMTk0LTRjZTYtOTU0Zi1hMTgzNTlmNGYzYTAiLCJwbGF0ZiI6IjMiLCJwdWlkIjoiMTAwMzAwMDA5QzI0MTI0NyIsInNjcCI6Ik1haWwuUmVhZCBvcGVuaWQgcHJvZmlsZSBVc2VyLlJlYWQgZW1haWwiLCJzaWduaW5fc3RhdGUiOlsia21zaSJdLCJzdWIiOiI5NVhONDhua08zbDJIeGYyTmdVTFo2YjA3WWpVV0lHOTRUQjA2bVNRUk9vIiwidGlkIjoiYjZhYzQ5ZjctYjEzZS00MjljLWJiODUtMDg0ODk2OTUwNjk5IiwidW5pcXVlX25hbWUiOiJjaHJpc3RvcGhlckBpdHBhcnRuZXJhYi5zZSIsInVwbiI6ImNocmlzdG9waGVyQGl0cGFydG5lcmFiLnNlIiwidXRpIjoiVmJGRW9EQ0RqMEMxVkFfSTNITWRBQSIsInZlciI6IjEuMCIsInhtc19zdCI6eyJzdWIiOiJka1FicDc3c2Y5ZmJlSnVYM1NqUml3aVhPa1FYOV9MZTF6MHp4X3Q4SU5BIn0sInhtc190Y2R0IjoxNDc4ODg0NzA1fQ.YQlKVdhXQcVwkcmbpY9Wx6ENro2DL7yH1rWwwkDZLD1inUrfbLRVb67lWKzgK9GnYP81d58Fp_2CZBw8C2E4X1eo02vog6_Qga9kVb8GF2-Ue0VP0KUv8EtRpEty_DBK7Re3iOkJR9yFSPQgf11Gf15l5O2mcEifrwny5nkRvab4_ssRt6hNf53V99uTFJ3_yKycGHPTobVbyQT5ZyDKxXRwoZVprFU70qrHGcBgo5emO8HbziYCUiQ9vGMpmtz61tE0U-c0E20FPC82i3zgLfMgmhNqmljZOpkOe85PFrxoep7fYkpZpWowCozugDW0E2A3SxBLZ_JHpci2R4irxg
files is a global variable that contains an array of objects, the data seems to be correct.
Here is a sample response I get when running the code:
{
statusCode: 403,
code: "ErrorAccessDenied",
message: "Access is denied. Check credentials and try again.",
requestId: "0739c0d9-38f2-45f7-a57d-c25dfbf92f75",
date: Fri Dec 07 2018 14:54:32 GMT+0100 (centraleuropeisk normaltid), …
}
API URL
me/messages/{id}/attachments/{id}
At https://apps.dev.microsoft.com/#/application/ I have given the application Mail.ReadWrite and Mail.ReadWrite.Shared access, though it only mentions read when signing up for the application which I find odd.
It looks like my app doesn't have the correct access. Is there something else I have to do that I have missed? Is my client query incorrect?
You need to check your scopes again. The Access Token you provided only includes the following scopes: Mail.Read openid profile User.Read email.
The code sample you provided references a scopes variable but it isn't clear what you've set this to:
var authParams = {
response_type: "id_token token",
client_id: appId,
redirect_uri: redirectUri,
scope: scopes,
state: sessionStorage.authState,
nonce: sessionStorage.authNonce,
response_mode: "fragment"
};
The value of this property should be something along the lines of openid profile email Mail.ReadWrite Mail.ReadWrite.Shared.

AngularJS & Socket.IO - Return value from Service (emit here) to Controller is undefined || promises, asynchronicity

I don't get my code to work properly.
I'm developing an app with AngularJS including a connection to a backend server via socket.io. I'm working on a login which is intended to send an user's data to the server. The server is intended to respond with "valid" and the user's data (name, dateOfBirth, ...) if the sent data is correct (email and password). Elements are:
BackendService (Factory which executes emit to server)
AppController (Controller which calls the login function of BackendService)
Node.js Server (computes if the sent data is valid so that the user can be logged in)
The intention is that the login function in the Factory returns a "login code" which tells the controller if the login is correct. Unfortunately the function returns "undefined". During my research, I found out that it might be because of asynchronicity and promises. However, I couldn't apply the given information to my problem as the majority was about $http. In addition - if the structure of my code is in need of improvement, let me know!
Here's my code:
Node.js Server
socket.on('logincust', function (p1, fn) {
connection.query("SELECT Salt FROM Customer WHERE Email = ?", [p1.Email], function (err, data, fields)
{
if (err) {
throw err;
}
if (data.length > 0) {
var hash = crypto.createHash('sha256').update(p1.Password + data[0].Salt).digest('base64');
connection.query("SELECT LName,FName,Email,Telephone,Address,DateOfBirth FROM Customer WHERE Password = ?", [hash], function (err, data2, fields) {
if (err) {
throw err;
}
if (data2.length > 0) {
fn('valid', data2[0]);
}
else {
fn('invalidpassword', 'nodata')
}
})
}
else {
fn('invalidemail','nodata')
}
})
})
BackendService (Factory)
"use strict";
mobileClientApp.factory('BackendService', function() {
var mySocket = io.connect('http://thisLinkIsPrivate:8888/ns');
return {
login: function (pUserData) {
if (mySocket.connected) {
mySocket.emit('logincust', pUserData, function (resp, data) {
if (resp == "valid") {
var parsedData = JSON.stringify(data);
console.log(parsedData);
user.lName = parsedData.LName; // Fill userData
user.fName = parsedData.FName;
user.email = parsedData.Email;
user.phoneCallcenter = parsedData.Telephone;
console.info("Login successful.");
return 0;
}
else {
if (resp == "invalidpassword") {
return 1;
}
else if (resp == "invalidemail") {
return 2;
}
}
});
}
else { // Socket is not connected
console.warn("Socket not connected.);
user.fName = "Peter";
user.lName = "Offline";
return -1;
}
};
Angular Controller
$scope.doLogin = function() {
var user = {'Email': this.loginData.username, 'Password': this.loginData.password};
var isLoggedIn = BackendService.login(user); // 0 - logged in, 1 - invalid password, 2 - invalid email, -1 - socket not connected
console.log("BackendService.login(user): " + BackendService.login(user)); // is undefined!
console.log("isLoggedIn: " + isLoggedIn); // undefined!
if (isLoggedIn == 0 || isLoggedIn == -1) {
$location.path('/app/landing');
}
else {
$scope.errorMessage = "Invalid login data!";
}
};
Yes, the problem seems to be asynchrony. If you want to have access to results from login method you should pass a callback to it. Since after you called it, the next execution will be your console log and that will happen before your SQL query returns results.

Azure mobile service - reading from two tables and return custom response object

I am writing Azure Mobile Service (AMS) API with JavaScript backend named customertickets and in the .get function I am attempting to read values from two tables in my AMS.
The two tables are 'User' and 'Ticket' and I am attempting to retrieve all tickets based on the passed parameter (phonenumber) and return some statistical data. Additionally, I am injecting a custom return value item.spname when looping through the tickets if the ticket satisfy a certain condition (item.parsedjsondata.SPId is not null or empty).
Here is the complete code (ctrl+F 'THIS DOES NOT WORK!'):
//Get ticket info by customer phonenumber
exports.get = function(request, response) {
var phonenumber = request.query.phonenumber;
var activeTicketsCounter = 0;
var completedTicketsCounter = 0;
var canceledTicketCounter = 0;
var needCustomerRatingTicketCounter = 0;
var includeCounterData = request.query.includeCounterData;
console.log("customertickets/get phonenumber: " + phonenumber);
request.service.tables.getTable('Ticket').read({
//SEE: http://stackoverflow.com/questions/24406110/how-to-get-system-properties-createdat-version-in-javascript-backend-of-azu
systemProperties: ['__createdAt', '__updatedAt'],
success: function(result) {
//No ticket is found
if (result.length === 0) {
console.log("customertickets/get: no ticket found");
response.send(statusCodes.NOT_FOUND, { message: "customertickets/get: no ticket is found" });
}
//return tickets after filtration based on the customer phonenumber
else {
console.log("customertickets/get: ticket found");
var filteredResults = [];
result.forEach(function(item) {
//only tickets with matched phonen number
if (JSON.parse(item.jsondata).customerPhonenumber == phonenumber) {
console.log(item);
//Adding parsed jsondata to item
item.parsedjsondata = JSON.parse(item.jsondata);
//Adding the name of the assigned spid; only if spid is not null ie. ticket is already assinged
if (item.parsedjsondata.SPId) {
console.log("SPID", item.parsedjsondata.SPId);
console.log("customerId", item.parsedjsondata.customerId);
//This works as expected
item.spid = item.parsedjsondata.SPId;
request.service.tables.getTable('User').where({ id: item.parsedjsondata.SPId.toString(), usertype: "200" }).read({
success: function(userResults) {
console.log('result', userResults[0]);
//Does not exist; return NOT FOUND as sp name!
if (userResults.length === 0) {
//This wroks fine and logs the expected result
console.log("customertickets/get: not SPname found", item.parsedjsondata.SPId);
//THIS DOES NOT WORK!
item.spname = "Service Provider Name Not Found";
}
//Record exists; return it
else {
//This wroks fine and logs the expected result
console.log("retrieved spname", JSON.parse(userResults[0].userjsondata).businessData.businessname.ar);
//THIS DOES NOT WORK!
item.spname = JSON.parse(userResults[0].userjsondata).businessData.businessname.ar;
}
}
});
}
//ActiveTicketsCounter
if (item.parsedjsondata.ticketStatus == "TicketStatus_ToBeAssigned"
|| item.parsedjsondata.ticketStatus == "TicketStatus_IsAssigned"
|| item.parsedjsondata.ticketStatus == "TicketStatus_InProgress"
|| item.parsedjsondata.ticketStatus == "TicketStatus_SPDisputed_CustomerNotReachable"
|| item.parsedjsondata.ticketStatus == "TicketStatus_SPDisputed_OutOfScope"
|| item.parsedjsondata.ticketStatus == "TicketStatus_SPDisputed_CustomerCanceled"
|| item.parsedjsondata.ticketStatus == "TicketStatus_SPDisputed_PriceDisagreement") {
activeTicketsCounter++;
}
//needCustomerRatingTicketCounter
if (item.parsedjsondata.ticketStatus == "TicketStatus_Completed_No_Customer_Rating") {
needCustomerRatingTicketCounter++;
}
//CompletedTicketsCounter
if (item.parsedjsondata.ticketStatus == "TicketStatus_Completed_And_Customer_Rated") {
completedTicketsCounter++;
}
//canceledTicketCounter
if (item.parsedjsondata.ticketStatus == "TicketStatus_CanceledByB8akAgent") {
canceledTicketCounter++;
}
//Testing: it works!
item.testing = "Testing Something!";
console.log("item.spname before push:", item.spname);
//pushing the found item to the list of results
filteredResults.push(item);
}
});
console.log("includeCounterData: " + includeCounterData);
if (includeCounterData == true) {
//After the loop, add the counters to the filteredresults to be returned
filteredResults.push({
activeTickets: activeTicketsCounter,
pendingCustomerRatingTickets: needCustomerRatingTicketCounter,
completedTickets: completedTicketsCounter,
canceledTickets: canceledTicketCounter
});
}
response.send(statusCodes.OK, filteredResults);
}
}
});
};
My main issue is that item.spname is never assigned a value and does not get returned in the response. I can see that the assigned value of item.spname which is JSON.parse(userResults[0].userjsondata).businessData.businessname.ar logs successfully in the AMS log but it does not get returned in the response.
The problem you have is that you're not waiting for the result of the many calls to
request.service.tables.getTable('User').where(...).read(...
to arrive before sending the response back to the client. Remember that almost everything in the node.js / JavaScript backend is asynchronous. When you call read on the User table, it won't wait for the result to arrive before executing the next statement - instead, it will queue the operation so that when the response is available, it will be executed.
To fix this you'll need to make all read calls to the users table, and only after you've got all the callbacks is when you can call response.send.

Categories