Skip to content

Commit

Permalink
half working register. need to fix GET
Browse files Browse the repository at this point in the history
  • Loading branch information
jwoo12 committed Mar 10, 2016
1 parent d176dd5 commit 3c2c80f
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 35 deletions.
1 change: 1 addition & 0 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
angular.module("myApp", [
"ngRoute",
"myApp.login",
"myApp.register",
"myApp.home"
]).
config(["$routeProvider", function($routeProvider) {
Expand Down
2 changes: 2 additions & 0 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@
<script src="login/login.js"></script>
<script src="login/loginHandler.js"></script>
<script src="homepage/home.js"></script>
<script src="register/register.js"></script>
<script src="register/registerHandler.js"></script>
</body>
</html>
1 change: 1 addition & 0 deletions app/login/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
</div>
</div>
<button type="button" class="btn btn-default" ng-click="submit()">LOGIN</button>
<button type="button" class="btn btn-default" ng-click="register()" style="display: block; margin: 0 auto; margin-top: 20px;">REGISTER</button>
</div>
8 changes: 4 additions & 4 deletions app/login/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ angular.module("myApp.login", ["ngRoute", "myApp.login.loginHandler"])
.controller("LoginController", function($scope, $location, loginHandler) {
$scope.submit = function() {
loginHandler.login($scope.usernameInput, $scope.passwordInput, function(result) {

console.log("RESULT " + result);
if(result.success){
console.log("success");
$location.url("/home");
}else{
alert("No account data");
}

});
};

$scope.register = function() {
$location.url("/register");
};
});
52 changes: 52 additions & 0 deletions app/register/register.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<div class="RegisterWrapper" style="text-align:center;" ng-controller="RegisterController">
<div> <h1> REGISTER </h1> </div>
<div class="username" style="">
<div class="form-group">
<label for="usernameInput">Username</label>
<input class="form-control input-sm" ng-model="regUser" type="text" style="margin: 0 auto; width: 50%;">
</div>
</div>

<div class="password">
<div class="form-group">
<label for="passwordInput">Password</label>
<input class="form-control input-sm" ng-model="regPass" type="text" style="margin: 0 auto; width: 50%;">
</div>
</div>

<div class="fname">
<div class="form-group">
<label for="fnameInput">First Name</label>
<input class="form-control input-sm" ng-model="regName" type="text" style="margin: 0 auto; width: 50%;">
</div>
</div>

<div class="lname">
<div class="form-group">
<label for="lnameInput">Last Name</label>
<input class="form-control input-sm" ng-model="regLast" type="text" style="margin: 0 auto; width: 50%;">
</div>
</div>

<div class="addr">
<div class="form-group">
<label for="addrInput">Address</label>
<input class="form-control input-sm" ng-model="regAddr" type="text" style="margin: 0 auto; width: 50%;">
</div>
</div>

<div class="email">
<div class="form-group">
<label for="emailInput">Email</label>
<input class="form-control input-sm" ng-model="regMail" type="text" style="margin: 0 auto; width: 50%;">
</div>
</div>

<div class="phone">
<div class="form-group">
<label for="phoneInput">Phone</label>
<input class="form-control input-sm" ng-model="regPhone" type="text" style="margin: 0 auto; width: 50%;">
</div>
</div>
<button type="button" class="btn btn-default" ng-click="registerUsr()" style="display: block; margin: 0 auto; margin-top: 20px;">REGISTER</button>
</div>
51 changes: 51 additions & 0 deletions app/register/register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
angular.module("myApp.register", ["ngRoute", "myApp.register.registerHandler"])

.config(["$routeProvider", function($routeProvider) {
$routeProvider.when("/register", {
templateUrl: "register/register.html",
controller: "RegisterController"
});
}])

.controller("RegisterController", function($scope, $location, registerHandler) {
$scope.registerUsr = function() {

if($scope.regUser || $scope.regMail) {
registerHandler.check($scope.regUser, $scope.regMail, function (result) {

if(result.success){
registerHandler.register($scope.regUser, $scope.regPass, $scope.regName, $scope.regLast, $scope.regAddr, $scope.regMail, $scope.regPhone,
function(result) {
if(result.success){
$location.url("/login");
}else{
alert("Error2");
}
});
}else{
alert("Error1");
}
});

} else {
alert("Username and Email fields cannot be empty");
}
}

// registerHandler.check($scope.regUser, $scope.regMail, function (result) {
// if(result.success){
// registerHandler.register($scope.regUser, $scope.regPass, $scope.regName $scope.regLast, $scope.regAddr, $scope.regMail, $scope.regPhone,
// function(result) {
// if(result.success){
// $location.url("/login");
// }else{
// alert("Error2");
// }
// });
// };
// };
// }else{
// alert("Error1");
// }
// });
})
45 changes: 45 additions & 0 deletions app/register/registerHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
var SERVICE_BASE_URL = 'http://localhost:8080/';

angular.module("myApp.register.registerHandler", [])
.service("registerHandler", function($http, $q) {
this.register = function (username, password, fname, lname, addr, email, phone, callback) {
var $regpost = $http({
method: 'POST',
url: SERVICE_BASE_URL + "register",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data: {userName: username, password: password, fname: fname,
lname: lname, addr: addr, email: email, phone: phone}
}).success(function (result) {
return callback(result);
}).error(function (result) {
return callback(result);
});
};

this.check = function(username, email, callback) {
var $checkpost = $http({
method: 'GET',
url: SERVICE_BASE_URL + "register",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data: {userName: username, email: email}
}).success(function (result) {
return callback(result);
}).error(function (result) {
return callback(result);
});

};

});
2 changes: 1 addition & 1 deletion dbConfig.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
user: "jwoo",
password: "", //Put password here (I'll message you my password :P)
connectString: ""
connectString: "localhost:1525/CRS"
//if WFH then localhost:1525/CRS
//if lab machine then CRS
};
133 changes: 103 additions & 30 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ app.set('ip', "127.0.0.1");
//Add public folder for static files
app.use(express.static(__dirname));

// //Set first page
// app.get('/', function(request, response) {
// response.render(__dirname + '/app/index');
// });

app.get("*", function (req, res) {
var resource = url.parse(req.url).pathname;
if(~resource.indexOf("node_modules") || ~resource.indexOf("bower_components")) {
Expand All @@ -71,31 +66,9 @@ process.on('SIGINT', function() {
process.exit();
});


//Get Oracle connection (test function)
// oracledb.getConnection(dbConfig, function(err, connection) {
// if (err) {
// console.error(err.message);
// return;
// }
// connection.execute(
// "SELECT * " +
// "FROM COURSE ",
// function(err, result)
// {
// if (err) {
// console.error(err.message);
// doRelease(connection);
// return;
// }
// console.log(result.metaData);
// console.log(result.rows);
// doRelease(connection);
// });
// });


app.post("/login", function (req, res) {
console.log(util.inspect(req.body, {showHidden: false, depth: null}));

oracledb.getConnection(dbConfig, function (err, connection) {
if (err) {
connectionError(err, res);
Expand All @@ -110,7 +83,7 @@ app.post("/login", function (req, res) {
if (err) {
executeError(err, res);
} else {
console.log(util.inspect(result, {showHidden: false, depth: null}));
// console.log(util.inspect(result, {showHidden: false, depth: null}));
if(result.rows.length > 0) {
// // Set cookies
// res.cookie("user_name", req.body.userName);
Expand All @@ -135,6 +108,106 @@ app.post("/login", function (req, res) {
);
});

app.route("/register")
.get(function (req, res) {
/** Use to check if a user name or email is already taken.
* All parameters should be part of the query string.
*
* Possible parameters:
* userName
* email
*/

console.log(util.inspect(req.body, {showHidden: false, depth: null}));

var DBQueryString,
DBQueryParam,
username = req.body.userName,
email = req.body.email;
console.log(username);
console.log(email);

DBQueryString =
"SELECT * " +
"FROM persons " +
"WHERE persons.user_name = :user_name OR persons.email = :email";

DBQueryParam = {user_name: username, email: email};


oracledb.getConnection(dbConfig, function (err, connection) {
if (err) {
connectionError(err, res);
return;
}
connection.execute(DBQueryString,
DBQueryParam,
function (err, result) {
if (err) {
executeError(err, res);
} else if (result.rows.length) {
res.status(409);
res.send(
{
error: true,
errorCode: 4,
message: "Element already exists",
success: false
}
);
} else {
res.send({success: true});
}
doRelease(connection);
}
);
});
})

.post (function (req, res){

});

// app.post("/register", function (req, res) {
// oracledb.getConnection(dbConfig, function (err, connection) {
// if (err) {
// connectionError(err, res);
// return;
// }
// connection.execute(
// "SELECT * " +
// "FROM users " +
// "WHERE user_name = :user_name AND password = :password",
// {user_name: req.body.userName, password: req.body.password},
// function (err, result) {
// if (err) {
// executeError(err, res);
// } else {
// console.log(util.inspect(result, {showHidden: false, depth: null}));
// if(result.rows.length > 0) {
// // // Set cookies
// // res.cookie("user_name", req.body.userName);
// // res.cookie("login", true);
// res.send({success: true});
// } else {
// res.status(422);
// res.send(
// {
// error: true,
// errorCode: 2,
// message: "Username or password is incorrect. Please try again.",
// success: false
// }
// );
// }
// }
// doRelease(connection);
// }
// );
// }
// );
// });

//Disconnect from Oracle
function doRelease(connection)
{
Expand Down

0 comments on commit 3c2c80f

Please sign in to comment.