Skip to content

Commit 4092e71

Browse files
Add Angular app and enable CORS
1 parent e3a4068 commit 4092e71

File tree

10 files changed

+55
-56
lines changed

10 files changed

+55
-56
lines changed

Diff for: angular-auth

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit cf31abde3bf88827b9ce72e0850ab55cb285e6e4

Diff for: api/controllers/QuoteController.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ module.exports = {
1212

1313
getProtectedQuote: function(req, res) {
1414
return res.json({ quote: quoter.getRandomOne() });
15-
},
15+
}
1616
};
1717

Diff for: api/controllers/TaskController.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* TaskController
3+
*
4+
* @description :: Server-side logic for managing tasks
5+
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
6+
*/
7+
8+
module.exports = {
9+
10+
};
11+

Diff for: api/controllers/UserController.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module.exports = {
5555
if (!valid) {
5656
return res.json(401, {err: 'invalid username or password'});
5757
} else {
58-
res.json({success: true, token: token});
58+
res.json({success: true, token: token, user: user.username});
5959
}
6060
});
6161
})

Diff for: api/models/Task.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Task.js
3+
*
4+
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
5+
* @docs :: http://sailsjs.org/documentation/concepts/models-and-orm/models
6+
*/
7+
8+
module.exports = {
9+
10+
attributes: {
11+
12+
}
13+
};
14+

Diff for: api/policies/isAuthenticated.js

+7-33
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,12 @@
11
/**
2-
* tokenAuth
3-
*
2+
* isAuthenticated
43
*
54
*/
6-
var jwt = require('jsonwebtoken');
7-
8-
// Get token from header
9-
function getToken(req) {
10-
if (req.headers.authorization &&
11-
req.headers.authorization.split(' ')[0] === 'Bearer') {
12-
return req.headers.authorization.split(' ')[1];
13-
}
14-
return null;
15-
}
16-
17-
module.exports = function(req, res, next) {
5+
var jwt = require('express-jwt');
186

19-
var token = getToken(req);
7+
var authCheck = jwt({
8+
secret: new Buffer('6ciafzROZr9eWWEKiMCsviPx1_fhp2QfEQqSHa1Cm9wGDthSfkXU7EZHUib6Vw3y', 'base64'),
9+
audience: 'mOh81XhUQvqeQ1uVY2yvgERpKKQjSKQr'
10+
});
2011

21-
if(token) {
22-
// verifies secret and checks exp
23-
jwt.verify(token, sails.config.sessionSecret, function(err, decoded) {
24-
if(err) {
25-
return res.json({ message: 'Unauthorized Access. Mismatched token.' });
26-
} else {
27-
// if everything is good, save to request for use in other routes
28-
req.decoded = decoded;
29-
next();
30-
}
31-
});
32-
} else {
33-
// if there is no token return an error
34-
return res.status(403).json({
35-
message: 'Unauthorized Access'
36-
});
37-
}
38-
};
12+
module.exports = authCheck;

Diff for: config/cors.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module.exports.cors = {
3737
* *
3838
***************************************************************************/
3939

40-
// allRoutes: false,
40+
allRoutes: true,
4141

4242
/***************************************************************************
4343
* *
@@ -47,15 +47,15 @@ module.exports.cors = {
4747
* *
4848
***************************************************************************/
4949

50-
// origin: '*',
50+
origin: '*',
5151

5252
/***************************************************************************
5353
* *
5454
* Allow cookies to be shared for CORS requests? *
5555
* *
5656
***************************************************************************/
5757

58-
// credentials: true,
58+
credentials: true,
5959

6060
/***************************************************************************
6161
* *
@@ -64,7 +64,7 @@ module.exports.cors = {
6464
* *
6565
***************************************************************************/
6666

67-
// methods: 'GET, POST, PUT, DELETE, OPTIONS, HEAD',
67+
methods: 'GET, POST, PUT, DELETE, OPTIONS, HEAD',
6868

6969
/***************************************************************************
7070
* *
@@ -73,6 +73,6 @@ module.exports.cors = {
7373
* *
7474
***************************************************************************/
7575

76-
// headers: 'content-type'
76+
headers: 'content-type, Authorization'
7777

7878
};

Diff for: config/policies.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@
1717
*/
1818

1919

20-
module.exports.policies = {
20+
module.exports.policies = {
2121

22-
/***************************************************************************
23-
* *
24-
* Default policy for all controllers and actions (`true` allows public *
25-
* access) *
26-
* *
27-
***************************************************************************/
22+
/***************************************************************************
23+
* *
24+
* Default policy for all controllers and actions (`true` allows public *
25+
* access) *
26+
* *
27+
***************************************************************************/
2828

29-
'*': true,
29+
'*': true,
3030

31-
QuoteController: {
32-
getProtectedQuote: 'isAuthenticated'
33-
}
34-
};
31+
QuoteController: {
32+
getProtectedQuote: 'isAuthenticated'
33+
}
34+
};

Diff for: config/routes.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ module.exports.routes = {
3434

3535
'/': { view: 'homepage' },
3636
'/welcome': 'WelcomeController.welcome',
37-
'post /login': 'UserController.login',
38-
'post /signup': 'UserController.signup',
39-
'get /random-quote': 'QuoteController.getQuote',
40-
'get /protected/random-quote': 'QuoteController.getProtectedQuote'
37+
'get /api/random-quote': 'QuoteController.getQuote',
38+
'get /api/protected/random-quote': 'QuoteController.getProtectedQuote'
4139

4240
/***************************************************************************
4341
* *

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"dependencies": {
88
"bcrypt": "^0.8.7",
99
"ejs": "2.3.4",
10+
"express-jwt": "^5.1.0",
1011
"grunt": "1.0.1",
1112
"grunt-contrib-clean": "1.0.0",
1213
"grunt-contrib-coffee": "1.0.0",

0 commit comments

Comments
 (0)