Skip to content

Commit 78ecb66

Browse files
committed
Merge pull request #38 from valor-software/master
Update to angular2 v0.44
2 parents f2d440b + 8a9d63b commit 78ecb66

38 files changed

+394
-10069
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ build/Release
2727
ehthumbs.db
2828
Icon?
2929
Thumbs.db
30+
.idea
3031

3132
# Node Files #
3233
node_modules

Diff for: Readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ Clone this repository as well as [the server](https://github.com/auth0/nodejs-jw
1414

1515
First, run the server app in the port `3001`.
1616

17-
Then, run `npm install` on this project and run `npm startwatch` to start the app. Then just navigate to [http://localhost:3000](http://localhost:3000) :boom:
18-
17+
Then, run `npm install` on this project and run `npm start` to start the app. Then just navigate to [http://localhost:3000](http://localhost:3000) :boom:
18+
Use `npm run server` to run API server.

Diff for: backend/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
.env

Diff for: backend/README.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# NODE TODO API
2+
3+
This is a NodeJS full API that you can use to test with your SPAs or Mobile apps.
4+
5+
## How to use it
6+
7+
This service is deployed in Heroku and saves the TODOs in memory, so once the dyno dies, all the todos are removed.
8+
9+
It's deployed on [http://auth0-todo-test-api.herokuapp.com/](http://auth0-todo-test-api.herokuapp.com/)
10+
11+
## Available APIs
12+
13+
### Open API
14+
15+
The Open API lets anyone do CRUD operation on a set of TODOs. This means that you can add a TODO and then John Doe can delete it.
16+
17+
Available methods:
18+
19+
* **POST /api/open/todos**: Adds a new TODO
20+
* **PUT /api/open/todos/:id**: Updates the TODO with id `id`
21+
* **GET /api/open/todos/:id**: Returns the TODO with id `id`
22+
* **DELETE /api/open/todos/:id**: Deletes the TODO with id `id`
23+
* **GET /api/open/todos**: Gets all fo the TODOs
24+
25+
### User based API
26+
27+
You can use this API to save TODO items for a particular user. For that, you need to use Auth0 to get the `id_token` (JWT) and send it in every request as part of the `Authorization` header.
28+
29+
This server validates JWT from the following account:
30+
31+
* **Domain**: `samples.auth0.com`
32+
* **ClientID**: `BUIJSW9x60sIHBw8Kd9EmCbj8eDIFxDC`
33+
34+
Available methods:
35+
36+
* **POST /api/todos**: Adds a new TODO
37+
* **PUT /api/todos/:id**: Updates the TODO with id `id`
38+
* **GET /api/todos/:id**: Returns the TODO with id `id`
39+
* **DELETE /api/todos/:id**: Deletes the TODO with id `id`
40+
* **GET /api/todos**: Gets all fo the TODOs
41+
42+
## Running this for your Auth0 account
43+
44+
If you want, you can run this server for YOUR Auth0 account. For that, you just need to create a `.env` file and set the `AUTH0_CLIENT_ID` and `AUTH0_CLIENT_SECRET` variables with the information from your account:
45+
46+
````bash
47+
AUTH0_CLIENT_ID=YourClientId
48+
AUTH0_CLIENT_SECRET=YourClientSecret
49+
````
50+

Diff for: backend/anonymous-routes.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var express = require('express'),
2+
quoter = require('./quoter');
3+
4+
var app = module.exports = express.Router();
5+
6+
app.get('/api/random-quote', function(req, res) {
7+
res.status(200).send(quoter.getRandomOne());
8+
});

Diff for: backend/config.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"secret": "ngEurope rocks!"
3+
}

Diff for: backend/package.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "in-memory-todo",
3+
"version": "0.1.0",
4+
"description": "An In memory todo for Logged in and not Logged in users",
5+
"main": "server.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/auth0/in-memory-todo.git"
12+
},
13+
"keywords": [
14+
"todo",
15+
"in",
16+
"memory",
17+
"jwt",
18+
"auth0"
19+
],
20+
"author": "Martin Gontovnikas",
21+
"license": "MIT",
22+
"bugs": {
23+
"url": "https://github.com/auth0/in-memory-todo/issues"
24+
},
25+
"homepage": "https://github.com/auth0/in-memory-todo",
26+
"dependencies": {
27+
"body-parser": "^1.6.5",
28+
"compression": "^1.0.11",
29+
"cors": "^2.4.1",
30+
"dotenv": "^0.4.0",
31+
"errorhandler": "^1.1.1",
32+
"express": "^4.8.5",
33+
"express-jwt": "^0.3.1",
34+
"jsonwebtoken": "^1.1.2",
35+
"lodash": "^2.4.1",
36+
"morgan": "^1.2.3"
37+
}
38+
}

Diff for: backend/protected-routes.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var express = require('express'),
2+
jwt = require('express-jwt'),
3+
config = require('./config'),
4+
quoter = require('./quoter');
5+
6+
var app = module.exports = express.Router();
7+
8+
var jwtCheck = jwt({
9+
secret: config.secret
10+
});
11+
12+
app.use('/api/protected', jwtCheck);
13+
14+
app.get('/api/protected/random-quote', function(req, res) {
15+
res.status(200).send(quoter.getRandomOne());
16+
});

Diff for: backend/quoter.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var quotes = require('./quotes.json');
2+
3+
exports.getRandomOne = function() {
4+
var totalAmount = quotes.length;
5+
var rand = Math.ceil(Math.random() * totalAmount);
6+
return quotes[rand];
7+
}

Diff for: backend/quotes.json

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
["Chuck Norris doesn't call the wrong number. You answer the wrong phone.",
2+
"Chuck Norris has already been to Mars; that's why there are no signs of life.",
3+
"Chuck Norris and Superman once fought each other on a bet. The loser had to start wearing his underwear on the outside of his pants.",
4+
"Some magicans can walk on water, Chuck Norris can swim through land.",
5+
"Chuck Norris once urinated in a semi truck's gas tank as a joke....that truck is now known as Optimus Prime.",
6+
"Chuck Norris doesn't flush the toilet, he scares the sh*t out of it",
7+
"Chuck Norris counted to infinity - twice.",
8+
"Chuck Norris can cut through a hot knife with butter",
9+
"Chuck Norris is the reason why Waldo is hiding.",
10+
"Death once had a near-Chuck Norris experience",
11+
"When the Boogeyman goes to sleep every night, he checks his closet for Chuck Norris.",
12+
"Chuck Norris can slam a revolving door.",
13+
"Chuck Norris once kicked a horse in the chin. Its decendants are known today as Giraffes.",
14+
"Chuck Norris will never have a heart attack. His heart isn't nearly foolish enough to attack him.",
15+
"Chuck Norris once got bit by a rattle snake........ After three days of pain and agony ..................the rattle snake died",
16+
"Chuck Norris can win a game of Connect Four in only three moves.",
17+
"When Chuck Norris does a pushup, he isn't lifting himself up, he's pushing the Earth down.",
18+
"There is no theory of evolution. Just a list of animals Chuck Norris allows to live.",
19+
"Chuck Norris can light a fire by rubbing two ice-cubes together.",
20+
"Chuck Norris doesn’t wear a watch. HE decides what time it is.",
21+
"The original title for Alien vs. Predator was Alien and Predator vs Chuck Norris.",
22+
"The film was cancelled shortly after going into preproduction. No one would pay nine dollars to see a movie fourteen seconds long.",
23+
"Chuck Norris doesn't read books. He stares them down until he gets the information he wants.",
24+
"Chuck Norris made a Happy Meal cry.",
25+
"Outer space exists because it's afraid to be on the same planet with Chuck Norris.",
26+
"If you spell Chuck Norris in Scrabble, you win. Forever.",
27+
"Chuck Norris can make snow angels on a concrete slab.",
28+
"Chuck Norris destroyed the periodic table, because Chuck Norris only recognizes the element of surprise.",
29+
"Chuck Norris has to use a stunt double when he does crying scenes.",
30+
"Chuck Norris' hand is the only hand that can beat a Royal Flush.",
31+
"There is no theory of evolution. Just a list of creatures Chuck Norris has allowed to live.",
32+
"Chuck Norris does not sleep. He waits.",
33+
"Chuck Norris tells a GPS which way to go.",
34+
"Some people wear Superman pajamas. Superman wears Chuck Norris pajamas.",
35+
"Chuck Norris's tears cure cancer ..... to bad he has never cried",
36+
"Chuck Norris doesn't breathe, he holds air hostage.",
37+
"Chuck Norris had a staring contest with Medusa, and won.",
38+
"When life hands Chuck Norris lemons, he makes orange juice.",
39+
"When Chuck Norris goes on a picnic, the ants bring him food.",
40+
"Chuck Norris gives Freddy Krueger nightmares.",
41+
"They once made a Chuck Norris toilet paper, but there was a problem: It wouldn't take shit from anybody.",
42+
"Chuck Norris can punch a cyclops between the eyes.",
43+
"Chuck Norris doesn't mow his lawn, he stands on the porch and dares it to grow",
44+
"Chuck Norris put out a forest fire. using only gasoline",
45+
"Chuck Norris CAN believe it's not butter.",
46+
"Custom t-shirts provided by Spreadshirt"]

Diff for: backend/server.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var logger = require('morgan'),
2+
cors = require('cors'),
3+
http = require('http'),
4+
express = require('express'),
5+
errorhandler = require('errorhandler'),
6+
dotenv = require('dotenv'),
7+
bodyParser = require('body-parser');
8+
9+
var app = express();
10+
11+
dotenv.load();
12+
13+
// Parsers
14+
// old version of line
15+
// app.use(bodyParser.urlencoded());
16+
// new version of line
17+
app.use(bodyParser.urlencoded({ extended: true }));
18+
app.use(bodyParser.json());
19+
app.use(cors());
20+
21+
app.use(function(err, req, res, next) {
22+
if (err.name === 'StatusError') {
23+
res.send(err.status, err.message);
24+
} else {
25+
next(err);
26+
}
27+
});
28+
29+
if (process.env.NODE_ENV === 'development') {
30+
app.use(express.logger('dev'));
31+
app.use(errorhandler())
32+
}
33+
34+
app.use(require('./anonymous-routes'));
35+
app.use(require('./protected-routes'));
36+
app.use(require('./user-routes'));
37+
38+
var port = process.env.PORT || 3001;
39+
40+
http.createServer(app).listen(port, function (err) {
41+
console.log('listening in http://localhost:' + port);
42+
});
43+

Diff for: backend/statusError.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function StatusError(msg, status) {
2+
var err = Error.call(this, msg);
3+
err.status = status;
4+
err.name = 'StatusError';
5+
return err;
6+
}
7+
8+
9+
StatusError.prototype = Object.create(Error.prototype, {
10+
constructor: { value: StatusError }
11+
});
12+
13+
module.exports = StatusError;
14+

Diff for: backend/user-routes.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var express = require('express'),
2+
_ = require('lodash'),
3+
config = require('./config'),
4+
jwt = require('jsonwebtoken');
5+
6+
var app = module.exports = express.Router();
7+
8+
// XXX: This should be a database of users :).
9+
var users = [{
10+
id: 1,
11+
username: 'gonto',
12+
password: 'gonto'
13+
}];
14+
15+
function createToken(user) {
16+
return jwt.sign(_.omit(user, 'password'), config.secret, { expiresInMinutes: 60*5 });
17+
}
18+
19+
app.post('/users', function(req, res) {
20+
if (!req.body.username || !req.body.password) {
21+
return res.status(400).send("You must send the username and the password");
22+
}
23+
if (_.find(users, {username: req.body.username})) {
24+
return res.status(400).send("A user with that username already exists");
25+
}
26+
27+
var profile = _.pick(req.body, 'username', 'password', 'extra');
28+
profile.id = _.max(users, 'id').id + 1;
29+
30+
users.push(profile);
31+
32+
res.status(201).send({
33+
id_token: createToken(profile)
34+
});
35+
});
36+
37+
app.post('/sessions/create', function(req, res) {
38+
if (!req.body.username || !req.body.password) {
39+
return res.status(400).send("You must send the username and the password");
40+
}
41+
42+
var user = _.find(users, {username: req.body.username});
43+
if (!user) {
44+
return res.status(401).send("The username or password don't match");
45+
}
46+
47+
if (!user.password === req.body.password) {
48+
return res.status(401).send("The username or password don't match");
49+
}
50+
51+
res.status(201).send({
52+
id_token: createToken(user)
53+
});
54+
});

Diff for: index.html

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
<!-- styles -->
1313
<link rel="stylesheet" type="text/css" href="/node_modules/bootstrap/dist/css/bootstrap.css">
1414
<!-- Angular 2 -->
15-
<script src="/lib/traceur-runtime.min.js"></script>
1615
<script type="text/javascript" src="/node_modules/whatwg-fetch/fetch.js"></script>
1716
<script type="text/javascript" src="/node_modules/jwt-decode/build/jwt-decode.js"></script>
1817
</head>

0 commit comments

Comments
 (0)