Skip to content

Commit

Permalink
enable cors with credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
abarnhard committed Jan 5, 2015
1 parent 5f01276 commit 75a875c
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 18 deletions.
12 changes: 12 additions & 0 deletions db/functions/get_day.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE OR REPLACE FUNCTION get_day (u_id integer, sday date)
RETURNS TABLE (id integer, "start" date, "title" text, "allDay" boolean) AS $$
DECLARE
BEGIN
RETURN QUERY
SELECT d.id, d.start, p.name || ' - ' || w.name AS "title", true AS "allDay"
FROM days d
INNER JOIN workouts w ON d.workout_id = w.id
INNER JOIN phases p ON d.phase_id = p.id
WHERE d.user_id = u_id AND d.start = sday;
END;
$$ language plpgsql;
4 changes: 2 additions & 2 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';

var Hapi = require('hapi'),
server = new Hapi.Server({connections: {routes: {cors: true}}}),
server = new Hapi.Server(),
routes = require('./routes/routes'),
plugins = require('./lib/plugins'),
authentication = require('./lib/authentication');

server.connection({port:process.env.PORT});
server.connection({port:process.env.PORT, routes: {cors: {origin: ['http://localhost:8100'],credentials: true}}});

server.register(plugins, function(){
server.auth.strategy('session', 'cookie', true, authentication);
Expand Down
7 changes: 7 additions & 0 deletions server/models/schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ Schedule.findAll = function(obj, cb){
});
};

Schedule.findOne = function(obj, cb){
pg.query('SELECT * FROM get_day($1,$2)', [obj.userId, obj.date], function(err, results){
var schedule = convertDatesToStrings(results.rows);
cb(err, schedule[0]);
});
};

Schedule.deleteDay = function(obj, cb){
pg.query('SELECT delete_day($1,$2)', [obj.userId,obj.dayId], function(err, results){
var id = (results || {rows:[{}]}).rows[0].delete_day;
Expand Down
25 changes: 25 additions & 0 deletions server/routes/config/schedule/get_day.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

var Schedule = require('../../../models/schedule'),
Joi = require('joi');

module.exports = {
description: 'Get 1 scheduled workouts for user',
tags:['workouts', 'schedule'],
validate: {
params: {
date: Joi.date().required()
}
},
cors: {
origin: ['http://localhost:8100'],
credentials: true
},
handler: function(request, reply){
var params = {date: request.params.date, userId:request.auth.credentials.id};
Schedule.findOne(params, function(err, schedule){
if(err){console.log('ERROR: Workout.getPhases', err);}
reply({schedule:schedule}).code(err ? 418 : 200);
});
}
};
33 changes: 17 additions & 16 deletions server/routes/routes.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
'use strict';

module.exports = [
{method: 'get', path: '/{param*}', config: require('./config/static/static')},
{method: 'post', path: '/register', config: require('./config/users/register')},
{method: 'post', path: '/login', config: require('./config/users/login')},
{method: 'delete', path: '/logout', config: require('./config/users/logout')},
{method: 'get', path: '/status', config: require('./config/users/status')},
{method: 'get', path: '/regimes', config: require('./config/workouts/query_regimes')},
{method: 'post', path: '/regimes', config: require('./config/workouts/add_regime')},
{method: 'get', path: '/regimes/{rid}/phases', config: require('./config/workouts/query_phases')},
{method: 'post', path: '/phases', config: require('./config/workouts/add_phase')},
{method: 'get', path: '/phases/{id}/workouts', config: require('./config/workouts/query_workouts')},
{method: 'get', path: '/workouts/day/{id}', config: require('./config/workouts/find_by_day')},
{method: 'post', path: '/workouts', config: require('./config/workouts/add_workout')},
{method: 'delete', path: '/workouts/{id}', config: require('./config/workouts/delete_workout')},
{method: 'post', path: '/workouts/schedule', config: require('./config/schedule/schedule_workout')},
{method: 'get', path: '/workouts/schedule', config: require('./config/schedule/query_schedule')},
{method: 'delete', path: '/workouts/day/{id}', config: require('./config/schedule/delete_day')}
{method: 'get', path: '/{param*}', config: require('./config/static/static')},
{method: 'post', path: '/register', config: require('./config/users/register')},
{method: 'post', path: '/login', config: require('./config/users/login')},
{method: 'delete', path: '/logout', config: require('./config/users/logout')},
{method: 'get', path: '/status', config: require('./config/users/status')},
{method: 'get', path: '/regimes', config: require('./config/workouts/query_regimes')},
{method: 'post', path: '/regimes', config: require('./config/workouts/add_regime')},
{method: 'get', path: '/regimes/{rid}/phases', config: require('./config/workouts/query_phases')},
{method: 'post', path: '/phases', config: require('./config/workouts/add_phase')},
{method: 'get', path: '/phases/{id}/workouts', config: require('./config/workouts/query_workouts')},
{method: 'get', path: '/workouts/day/{id}', config: require('./config/workouts/find_by_day')},
{method: 'post', path: '/workouts', config: require('./config/workouts/add_workout')},
{method: 'delete', path: '/workouts/{id}', config: require('./config/workouts/delete_workout')},
{method: 'post', path: '/workouts/schedule', config: require('./config/schedule/schedule_workout')},
{method: 'get', path: '/workouts/schedule', config: require('./config/schedule/query_schedule')},
{method: 'get', path: '/workouts/schedule/{date}', config: require('./config/schedule/get_day')},
{method: 'delete', path: '/workouts/day/{id}', config: require('./config/schedule/delete_day')}
];
1 change: 1 addition & 0 deletions test/scripts/create-db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ psql $1 -f ../../db/functions/add_set.sql
psql $1 -f ../../db/functions/add_workout.sql
psql $1 -f ../../db/functions/delete_day.sql
psql $1 -f ../../db/functions/delete_workout.sql
psql $1 -f ../../db/functions/get_day.sql
psql $1 -f ../../db/functions/query_phases.sql
psql $1 -f ../../db/functions/query_workouts.sql
psql $1 -f ../../db/functions/query_workout_by_day.sql
Expand Down

0 comments on commit 75a875c

Please sign in to comment.