Skip to content

Commit 57c4269

Browse files
committed
Added login module
1 parent 0c71cb3 commit 57c4269

File tree

6 files changed

+106
-4
lines changed

6 files changed

+106
-4
lines changed

app/models/model.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@ Schema = mongoose.Schema;
55
// and should be defined before the schema for readability
66

77

8+
// Model Schema
9+
//var ModelSchema = new Schema ({
10+
// name : {
11+
// type: String
12+
// },
13+
//});
14+
815
// Model Schema
916
var ModelSchema = new Schema ({
10-
name : {
17+
email : {
1118
type: String
1219
},
20+
password : {
21+
type: String
22+
}
1323
});
14-
15-
module.exports = mongoose.model('Model', ModelSchema);
24+
module.exports = mongoose.model('Model', ModelSchema);
25+
//module.exports = mongoose.model('Model', ModelSchema);

app/routes/api.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,28 @@ module.exports = function(app) {
4343
});
4444
});
4545
});
46+
// Example POST route
47+
app.post('/users', function (req, res) {
48+
Model.create({
49+
email : req.body.email, // Bound using Angular
50+
password : req.body.password
51+
}, function(err, model) {
52+
if(err) {
53+
res.send(err);
54+
}
55+
56+
Model.find(function(err, users) {
57+
res.send(users);
58+
});
59+
});
60+
});
61+
app.get('/users', function(req, res) {
62+
63+
// Checks the model collection and returns all of them`
64+
Model.find(function(err, users) {
65+
66+
// returns all people in JSON format
67+
res.send(users);
68+
});
69+
});
4670
}

app/routes/routes.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ module.exports = function(app) {
1515
// Must always end the Writeable stream
1616
res.end();
1717
});
18+
19+
app.get('/login', function(req, res) {
20+
// Displaying an already made view
21+
res.sendfile('public/views/login.html');
22+
});
1823

1924
// Wildcard route serving static html page
2025
app.get('*', function(req, res) {

config/db.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module.exports = {
2-
url : 'mongodb://localhost:27017/test'
2+
url : 'mongodb://localhost:27017/users'
33
}

public/controllers/login.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Export the controller
2+
var login = angular.module('login', []);
3+
4+
// Defining wrapper Routes for our API
5+
login.controller('loginCtrl', function loginCtrl($scope, $http) {
6+
$scope.formData = {};
7+
$scope.createUser = function() {
8+
$http.post('/users', $scope.formData)
9+
.success(function(data) {
10+
$scope.formData = {};
11+
$scope.users = data;
12+
console.log(data);
13+
})
14+
.error(function(data) {
15+
console.log("Error: " + data);
16+
});
17+
};
18+
19+
})

public/views/login.html

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!DOCTYPE html>
2+
<html ng-app="login">
3+
<head>
4+
<title>Login Page</title>
5+
6+
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
7+
<link rel="stylesheet" type="text/css" href="../assets/css/styles.css">
8+
9+
<!-- Angular from Google CDN -->
10+
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js"></script>
11+
<!-- Load AppController -->
12+
<script type="text/javascript" src="../controllers/login.js"></script>
13+
</head>
14+
<body ng-controller="loginCtrl">
15+
<div class="container">
16+
17+
<!-- FORM TO CREATE USERS -->
18+
<div id="model-form" class="form-horizontal">
19+
<div class="col-sm-8 col-sm-offset-2 text-center" id="name-field">
20+
21+
<!-- Only posts to Angular's createModel if form is valid and was submitted -->
22+
<form name="modelCreate" ng-submit="modelCreate.$valid && submitted && createUser()" novalidate>
23+
<div class="form-group">
24+
25+
<!-- BIND THIS VALUE TO formData.name -->
26+
<div class="col-sm-12">
27+
<input type="email" name="email" class="form-control input-lg text-center" placeholder="Email" ng-model="formData.email" required />
28+
29+
<input type="password" name="password" class="form-control input-lg text-center" placeholder="Password" ng-model="formData.password" required />
30+
31+
</div>
32+
</div>
33+
34+
<!-- Sets the value submitted to true to help with form validation -->
35+
<button type="submit" class="btn btn-primary btn-lg" ng-click="submitted=true" ng-disabled="modelCreate.password.$dirty && modelCreate.password.$invalid ||
36+
modelCreate.email.$dirty && modelCreate.email.$invalid">
37+
Register
38+
</button>
39+
</form>
40+
</div>
41+
</div>
42+
</div>
43+
</body>
44+
</html>

0 commit comments

Comments
 (0)