Skip to content

Instantly share code, notes, and snippets.

View joshtwist's full-sized avatar

Josh Twist joshtwist

View GitHub Profile
@joshtwist
joshtwist / codez-slash-foo.js
Created November 26, 2012 05:30
Magic magic buckets
// note, this file should actually be /codez/foo.js but gist won't allow folders
var req = require('request');
module.exports.foo = function() {
console.log('wrapped? making HTTP request');
req.get('http://www.thejoyofcode.com', function(error, response, body) {
console.log('http status code: ' + response.statusCode);
});
}
@joshtwist
joshtwist / profile.insert.js
Created December 1, 2012 21:35
Azure Mobile Services script to add name and imageUrl properties on insert
var req = require('request');
function insert(item, user, request) {
item.userId = user.userId;
if (user.userId.indexOf('Twitter:') === 0) {
var twitterId = user.userId.replace('Twitter:', '');
req.get('https://api.twitter.com/1/users/show.json?user_id=' + twitterId,
function(error, result, body) {
item.imageUrl = '';
if (error || result.statusCode != 200) { console.error(error || result); }
@joshtwist
joshtwist / app.js
Last active December 10, 2015 02:58
Simple node JS application to pull down all your Mobile Services table scripts and then watch the folder for changes, uploading back to the server any changes you make to your script. Potential improvements: There's very little error handling and it watches ALL files in the specified directory but could be improved to only watch the files that w…
var watch = require('watch');
var util = require('util');
var sys = require('sys');
var exec = require('child_process').exec;
var service = process.argv[2];
var directory = process.argv[3] || __dirname;
console.log('Mobile Service name: ', service);
console.log('Directory: ', directory);
@joshtwist
joshtwist / items.insert.js
Last active December 10, 2015 06:08
Unit testing two Mobile Services scripts that encrypt on insert and decrypt on read.
// the insert script that encrypts the text column
var crypto = require('crypto');
function insert(item, user, request) {
// your encryption key - choose something better and randomly generated in real-life
var password = 'zumoisawesome'
var cipher = crypto.createCipher('aes256', password);
var output = cipher.update(item.text, 'utf-8', 'base64');
item.text = output + cipher.final('base64');
// go ahead, insert the modified data
@joshtwist
joshtwist / zumojwt.js
Last active February 3, 2016 20:37
Function that creates a ZUMO JWT token. Specify the date, the aud, the userId and your Mobile Services master key, e.g. var expiry = new Date().setUTCDate(new Date().getUTCDate() + 1); // expires in one day from nowvar jwt = zumoJwt(expiry, "your-aud", "unique string for this user", "yourmasterkey");
function zumoJwt(expiryDate, aud, userId, masterKey) {
var crypto = require('crypto');
function base64(input) {
return new Buffer(input, 'utf8').toString('base64');
}
function urlFriendly(b64)
{
return b64.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=/g, '');
@joshtwist
joshtwist / fetchBasicProfile.js
Created December 29, 2012 23:29
Simple function to generate a basic profile for users that log into your Mobile Service via Twitter and Facebook. This is great for pre-completing a default profile and/or getting profile images for your users. Includes simple mocha tests.
function fetchBasicProfile(user, callback) {
var req = require('request');
var util = require('util');
var sep = user.userId.indexOf(':');
var profile = {};
profile.source = user.userId.substring(0, sep).toLowerCase();
profile.id = user.userId.substring(sep + 1);
var createError = function(e, r) {
// .NET C#
var mobileServiceClient = new MobileServiceClient("<your-app-url>", "<your-app-key>");
mobileServiceClient.CurrentUser = new MobileServiceUser("Foo:123456789");
mobileServiceClient.CurrentUser.MobileServiceAuthenticationToken = "<your-users-JWT>";
@joshtwist
joshtwist / App.xaml.cs
Last active December 10, 2015 10:38
Using a filter in a C# Windows 8 application to automatically log a user back into the Mobile Service if any request returns a 401 (for example, due to a timeout of the token).
// This is a modified App.xaml.cs file from the Mobile Services quickstart, showing how to add the AuthFilter above
private static MobileServiceClient GetClient()
{
var client = new MobileServiceClient(
"<your-app-url>",
"<your-app-key>"
);
var filter = new AuthFilter();
@joshtwist
joshtwist / TodoService.m
Last active August 23, 2017 09:57
Using a filter in an iOS / Objective-C application to automatically log a user back into the Mobile Service if any request returns a 401 (for example, due to a timeout of the token).
// This is the MSFilter protocol implementation taken from the iOS Mobile Services'\
// quickstart and modified to log the user in if a 401 Unauthorized response is received
#pragma mark * MSFilter methods
- (void) handleRequest:(NSURLRequest *)request
onNext:(MSFilterNextBlock)onNext
onResponse:(MSFilterResponseBlock)onResponse
{
// Increment the busy counter before sending the request
@joshtwist
joshtwist / MSClient+CustomId.h
Last active December 10, 2015 12:08
Objective C Category to add custom identity to Mobile Services.
#import <WindowsAzureMobileServices/WindowsAzureMobileServices.h>
@interface MSClient (CustomId) <MSFilter>
- (void) registerUsername:(NSString *) username withPassword: (NSString *) password withCompletion: (MSItemBlock) completion;
- (void) loginUsername:(NSString *) username withPassword: (NSString *) password completion: (MSClientLoginBlock) completion;
@end