├── Procfile
├── .idea
├── .name
├── misc.xml
├── scopes
│ └── scope_settings.xml
├── encodings.xml
├── vcs.xml
├── modules.xml
├── angular-restful-auth.iml
└── workspace.xml
├── README.md
├── app
├── partials
│ ├── me.html
│ ├── home.html
│ ├── signin.html
│ └── signup.html
├── lib
│ └── ngStorage.js
├── scripts
│ ├── app.js
│ ├── controllers.js
│ └── services.js
└── index.html
├── models
└── User.js
├── .gitignore
├── package.json
├── client.js
└── server.js
/Procfile:
--------------------------------------------------------------------------------
1 | web: node server.js
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | angular-restful-auth
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | token-based-auth-backend
2 | ========================
3 |
4 | Token Based Authentication Backend Project Written in NodeJS
5 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/scopes/scope_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/app/partials/me.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
{{myDetails.data.email}}
4 |
{{myDetails.data.password}}
5 |
6 |
--------------------------------------------------------------------------------
/models/User.js:
--------------------------------------------------------------------------------
1 | var mongoose = require('mongoose');
2 | var Schema = mongoose.Schema;
3 |
4 | var UserSchema = new Schema({
5 | email: String,
6 | password: String,
7 | token: String
8 | });
9 |
10 | module.exports = mongoose.model('User', UserSchema);
--------------------------------------------------------------------------------
/app/partials/home.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Restful Authentication System with AngularJS & NodeJS Example
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/angular-restful-auth.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | dist
3 | .c9
4 | .tmp
5 | .sass-cache
6 | bower_components
7 | lib-cov
8 | *.seed
9 | *.log
10 | *.csv
11 | *.dat
12 | *.out
13 | *.pid
14 | *.gz
15 | *.idea
16 | *.idea/
17 | .idea/
18 | .bower-cache/*
19 | .bower-registry/*
20 | public/bower/*
21 | config/runtime.json
22 | pids
23 | logs
24 | results
25 | npm-debug.log
26 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "angular-restful-auth",
3 | "version": "0.0.1",
4 | "dependencies": {
5 | "express": "4.x",
6 | "body-parser": "~1.0.0",
7 | "morgan": "latest",
8 | "mongoose": "3.8.8",
9 | "express-jwt": "0.2.1",
10 | "jsonwebtoken": "0.4.0"
11 | },
12 | "engines": {
13 | "node": ">=0.10.0"
14 | }
15 | }
--------------------------------------------------------------------------------
/client.js:
--------------------------------------------------------------------------------
1 | // Required Modules
2 | var express = require("express");
3 | var morgan = require("morgan");
4 | var app = express();
5 |
6 | var port = process.env.PORT || 3000;
7 |
8 | app.use(morgan("dev"));
9 | app.use(express.static("./app"));
10 |
11 | app.get("/", function(req, res) {
12 | res.sendFile("./app/index.html");
13 | });
14 |
15 | // Start Server
16 | app.listen(port, function () {
17 | console.log( "Express server listening on port " + port);
18 | });
--------------------------------------------------------------------------------
/app/partials/signin.html:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/app/partials/signup.html:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/app/lib/ngStorage.js:
--------------------------------------------------------------------------------
1 | /*! ngStorage 0.3.0 | Copyright (c) 2013 Gias Kay Lee | MIT License */"use strict";!function(){function a(a){return["$rootScope","$window",function(b,c){for(var d,e,f,g=c[a]||(console.warn("This browser does not support Web Storage!"),{}),h={$default:function(a){for(var b in a)angular.isDefined(h[b])||(h[b]=a[b]);return h},$reset:function(a){for(var b in h)"$"===b[0]||delete h[b];return h.$default(a)}},i=0;i
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | Restful Authentication with NodeJS & AngularJS
11 |
12 |
13 |
14 |
15 |
22 |
23 |
24 |
28 |
29 |
30 |
31 |
32 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
--------------------------------------------------------------------------------
/server.js:
--------------------------------------------------------------------------------
1 | // Required Modules
2 | var express = require("express");
3 | var morgan = require("morgan");
4 | var bodyParser = require("body-parser");
5 | var jwt = require("jsonwebtoken");
6 | var mongoose = require("mongoose");
7 | var app = express();
8 |
9 | var port = process.env.PORT || 3001;
10 | var User = require('./models/User');
11 |
12 | // Connect to DB
13 | mongoose.connect(process.env.MONGO_URL);
14 |
15 | app.use(bodyParser.urlencoded({ extended: true }));
16 | app.use(bodyParser.json());
17 | app.use(morgan("dev"));
18 | app.use(function(req, res, next) {
19 | res.setHeader('Access-Control-Allow-Origin', '*');
20 | res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
21 | res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type, Authorization');
22 | next();
23 | });
24 |
25 |
26 |
27 | app.post('/authenticate', function(req, res) {
28 | User.findOne({email: req.body.email, password: req.body.password}, function(err, user) {
29 | if (err) {
30 | res.json({
31 | type: false,
32 | data: "Error occured: " + err
33 | });
34 | } else {
35 | if (user) {
36 | res.json({
37 | type: true,
38 | data: user,
39 | token: user.token
40 | });
41 | } else {
42 | res.json({
43 | type: false,
44 | data: "Incorrect email/password"
45 | });
46 | }
47 | }
48 | });
49 | });
50 |
51 |
52 | app.post('/signin', function(req, res) {
53 | User.findOne({email: req.body.email, password: req.body.password}, function(err, user) {
54 | if (err) {
55 | res.json({
56 | type: false,
57 | data: "Error occured: " + err
58 | });
59 | } else {
60 | if (user) {
61 | res.json({
62 | type: false,
63 | data: "User already exists!"
64 | });
65 | } else {
66 | var userModel = new User();
67 | userModel.email = req.body.email;
68 | userModel.password = req.body.password;
69 | userModel.save(function(err, user) {
70 | user.token = jwt.sign(user, process.env.JWT_SECRET);
71 | user.save(function(err, user1) {
72 | res.json({
73 | type: true,
74 | data: user1,
75 | token: user1.token
76 | });
77 | });
78 | })
79 | }
80 | }
81 | });
82 | });
83 |
84 | app.get('/me', ensureAuthorized, function(req, res) {
85 | User.findOne({token: req.token}, function(err, user) {
86 | if (err) {
87 | res.json({
88 | type: false,
89 | data: "Error occured: " + err
90 | });
91 | } else {
92 | res.json({
93 | type: true,
94 | data: user
95 | });
96 | }
97 | });
98 | });
99 |
100 | function ensureAuthorized(req, res, next) {
101 | var bearerToken;
102 | var bearerHeader = req.headers["authorization"];
103 | if (typeof bearerHeader !== 'undefined') {
104 | var bearer = bearerHeader.split(" ");
105 | bearerToken = bearer[1];
106 | req.token = bearerToken;
107 | next();
108 | } else {
109 | res.send(403);
110 | }
111 | }
112 |
113 | process.on('uncaughtException', function(err) {
114 | console.log(err);
115 | });
116 |
117 | // Start Server
118 | app.listen(port, function () {
119 | console.log( "Express server listening on port " + port);
120 | });
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 |
282 |
283 |
284 |
285 |
286 |
287 |
288 |
289 |
290 |
291 |
292 |
293 |
294 |
295 |
296 |
297 |
298 |
299 |
300 |
301 |
302 |
303 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
311 |
312 |
313 |
314 |
315 |
316 |
317 |
318 |
319 |
320 |
321 |
322 |
323 |
324 |
325 |
326 |
327 |
328 |
329 |
330 |
331 |
332 |
333 |
334 |
335 |
336 |
337 |
338 | 1411145592770
339 | 1411145592770
340 |
341 |
342 |
343 |
344 |
345 |
346 |
347 |
348 |
349 |
350 |
351 |
352 |
353 |
354 |
355 |
356 |
357 |
358 |
359 |
360 |
361 |
362 |
363 |
364 |
365 |
366 |
367 |
368 |
369 |
370 |
371 |
372 |
373 |
374 |
375 |
376 |
377 |
378 |
379 |
380 |
381 |
382 |
383 |
384 |
385 |
386 |
387 |
388 |
389 |
390 |
391 |
392 |
393 |
394 |
395 |
396 |
397 |
398 |
399 |
400 |
401 |
402 |
403 |
404 |
405 |
406 |
407 |
408 |
409 |
410 |
411 |
412 |
413 |
414 |
415 |
416 |
417 |
418 |
419 |
420 |
421 |
422 |
423 |
424 |
425 |
426 |
427 |
428 |
429 |
430 |
431 |
432 |
433 |
434 |
435 |
436 |
437 |
438 |
439 |
440 |
441 |
442 |
443 |
444 |
445 |
446 |
447 |
448 |
449 |
450 |
451 |
452 |
453 |
454 |
455 |
456 |
457 |
458 |
459 |
460 |
461 |
462 |
463 |
464 |
465 |
466 |
467 |
468 |
469 |
470 |
471 |
472 |
473 |
474 |
475 |
476 |
477 |
478 |
479 |
480 |
481 |
482 |
483 |
484 |
485 |
486 |
487 |
488 |
489 |
490 |
491 |
492 |
493 |
494 |
495 |
496 |
497 |
498 |
499 |
500 |
501 |
502 |
503 |
--------------------------------------------------------------------------------