Skip to content

Instantly share code, notes, and snippets.

@gregfroese
gregfroese / routes.js
Created August 12, 2014 14:23
Protecting routes with authentication in Express
var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');
var jwtauth = require('../../../jwtauth.js');
router.get('/', [bodyParser(), jwtauth], function(req, res) {
//jwtauth is run automatically and will populate req.user if there is a valid token supplied
if(!req.user) {
res.status(401).send({error: "Unauthorized access"});
return
var jwt = require('jwt-simple');
var config = require('yaml-config');
var settings = config.readConfig('./config/app.yaml');
var username = settings.rabbitmq.username;
var password = settings.rabbitmq.password;
var url = settings.rabbitmq.url;
var port = settings.rabbitmq.port;
var rpc = require('amqp-rpc').factory({
@gregfroese
gregfroese / jwtauth.js
Last active August 29, 2015 14:05
jwtauth.js
var rpc = require('amqp-rpc').factory({
url: "amqp://guest:guest@localhost:5672"
});
module.exports = function(req, res, next) {
var jwt = require('jwt-simple');
token = (req.body && req.body.access_token) || (req.query && req.query.access_token) || req.headers['x-access-token'];
rpc.call('profile.confirmToken', {token: token}, function(result, user, msg) {
@gregfroese
gregfroese / gist:4124753
Created November 21, 2012 13:04
Posting an image in RubyMotion using BubbleWrap and saving it in Rails
## RubyMotion
imageData = UIImage.UIImageJPEGRepresentation(@image_view.image, 1)
encodedData = [imageData].pack("m0")
data["image"] = encodedData
BW::HTTP.post("http://localhost:3000/upload}", {payload: data}) do |response|
if response.ok?
end
end
public function editAboutMe_ajax($params) {
$this->show_layout = false;
$resp = new SilkAjax();
$this->set("params", $params);
$this->set("formType", "edit");
$resp->replace_html("#AboutMe", $this->render_partial("edit_about_me_ajax.tpl"));
return $resp->get_result();
}
$friendsActivity = orm("UserActivity")->find_all(array("join" => array("INNER JOIN silk_friends on friends.user_id = silk_user_activity.user_id"), "conditions" => array("friends.user_id = ?", $params["user_id"])));
<?php
class mpUser extends SilkUser {
public function __construct(){
parent::__construct();
}
public function setup() {
$this->create_has_many_association("friends", "Friend", "user_id");
$this->create_has_many_association("activity", "UserActivity", "user_id");
// $this->create_belongs_to_association("activity", "user_activity", "user_id");
object(__PHP_Incomplete_Class)#3 (16) {
["__PHP_Incomplete_Class_Name"]=>
string(6) "mpUser"
["params"]=>
array(11) {
["id"]=>
string(2) "26"
["username"]=>
string(9) "braindead"
["password"]=>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>{$title}</title>
{javascript file="lib/silk/jquery/jquery.js"}
{javascript file="lib/silk/jquery/jquery.color.js"}
{javascript file="lib/silk/jquery/jquery.silk.js"}
{stylesheet}
/**
* Fills an object with posted form variables
*
* @param array Contains hash of values to load into the object (used in conjunction with SilkForm::auto_form)
*/
function fill_object_form($params) {
foreach($params as $key=>$value) {
$this->$key = $value;
}
}