Add user registration UIs for IoT Server

charithag 9 years ago
parent bca86e63e0
commit b00637fa1f

@ -0,0 +1,5 @@
{{unit "cdmf.unit.ui.title" pageTitle="Register"}}
{{#zone "content"}}
{{unit "iot.unit.user.register"}}
{{/zone}}

@ -0,0 +1,145 @@
var emailIsValid = function (email) {
var atPosition = email.indexOf("@");
var dotPosition = email.lastIndexOf(".");
return !(atPosition < 1 || ( dotPosition - atPosition < 2 ));
};
var validatePassword = function (psswd, conPass) {
var error = "";
var illegalChars = /[\W_]/; // allow only letters and numbers
if ((psswd.length < 5) || (psswd.length > 15)) {
error = "The password is of wrong length. Should be between 5 and 15 characters. \n";
$('.wr-validation-summary strong').text(error);
$('.wr-validation-summary').removeClass("hidden");
return false;
} else if (illegalChars.test(psswd)) {
error = "The password contains illegal characters.\n";
$('.wr-validation-summary strong').text(error);
$('.wr-validation-summary').removeClass("hidden");
return false;
} else if ((psswd.search(/[a-zA-Z]+/) == -1) || (psswd.search(/[0-9]+/) == -1)) {
error = "The password must contain at least one numeral and one character.\n";
$('.wr-validation-summary strong').text(error);
$('.wr-validation-summary').removeClass("hidden");
return false;
} else if (psswd != conPass) {
error = "The password and confirm-password should match.\n";
$('.wr-validation-summary strong').text(error);
$('.wr-validation-summary').removeClass("hidden");
// return false;
} else {
return true;
}
};
$(function () {
$("button#add-user-btn").click(function () {
//e.preventDefault();
var username = $("input#user_name").val();
var firstname = $("input#first_name").val();
var lastname = $("input#last_name").val();
var emailAddress = $("input#email").val();
var password = $("input#password").val();
var passwordConfirm = $("input#password_confirmation").val();
// var userRoles = $("select#roles").val();
if (!firstname) {
$('.wr-validation-summary strong').text("Firstname is a required field. It cannot be empty.");
$('.wr-validation-summary').removeClass("hidden");
return false;
} else if (!lastname) {
$('.wr-validation-summary strong').text("Lastname is a required field. It cannot be empty.");
$('.wr-validation-summary').removeClass("hidden");
return false;
} else if (!username) {
$('span.wr-validation-summary strong').text("Username is a required field. It cannot be empty.");
$('.wr-validation-summary').removeClass("hidden");
return false;
} else if (!emailAddress) {
$('.wr-validation-summary strong').text("Email is a required field. It cannot be empty.");
$('.wr-validation-summary').removeClass("hidden");
return false;
} else if (!emailIsValid(emailAddress)) {
$('.wr-validation-summary strong').text("Email is not valid. Please enter a correct email address.");
$('.wr-validation-summary').removeClass("hidden");
return false;
} else if (!password) {
$('.wr-validation-summary strong').text("Please enter a user login password.");
$('.wr-validation-summary').removeClass("hidden");
return false;
} else if (!passwordConfirm) {
$('.wr-validation-summary strong').text("Please re-type password");
$('.wr-validation-summary').removeClass("hidden");
return false;
} else if (!validatePassword(password, passwordConfirm)) {
return false;
} else if (!$('#t_and_c').is(':checked')) {
$('.wr-validation-summary strong').text("Please accept our terms and conditions");
$('.wr-validation-summary').removeClass("hidden");
} else {
var addUserFormData = {};
addUserFormData.username = username;
addUserFormData.firstname = firstname;
addUserFormData.lastname = lastname;
addUserFormData.emailAddress = emailAddress;
addUserFormData.password = password;
addUserFormData.userRoles = null;
var context = $(".form-login-box").attr("action");
var addUserAPI = context + "/api/user/register";
$.ajax({
type: 'POST',
url: addUserAPI,
contentType: 'application/json',
data: JSON.stringify(addUserFormData),
success: function (data) {
if (data == 200) {
$('.wr-validation-summary strong').text("Successfully Submitted.");
$('.wr-validation-summary strong').removeClass("label-danger");
$('.wr-validation-summary strong').addClass("label-success");
} else if (data == 201) {
$('.wr-validation-summary strong').text("User created. You will be redirected to login page");
$('.wr-validation-summary strong').removeClass("label-danger");
$('.wr-validation-summary strong').addClass("label-success");
setTimeout(function () {
window.location = context + "/login";
}, 2000);
} else if (data == 400) {
$('.wr-validation-summary strong').text("Exception at backend.");
$('.wr-validation-summary strong').removeClass("label-danger");
$('.wr-validation-summary strong').addClass("label-warning");
} else if (data == 403) {
$('.wr-validation-summary strong').text("Action not permitted.");
} else if (data == 409) {
$('.wr-validation-summary strong').text("User exists.");
$('.wr-validation-summary strong').removeClass("label-default");
$('.wr-validation-summary strong').addClass("label-success");
}
$('.wr-validation-summary').removeClass("hidden");
$('#password').val('');
$('#password_confirmation').val('');
// return true;
},
error: function (err) {
$('.wr-validation-summary strong').text("An unexpected error occurred.");
$('.wr-validation-summary').removeClass("hidden");
return false;
}
});
}
return false;
});
});

@ -0,0 +1,65 @@
<div class="container col-md-12 col-lg-6 col-centered wr-content wr-login col-centered sign-panel">
<p class="page-sub-title">Register</p>
<p>Create new account on IoT Server</p>
<!-- validation -->
<span class="wr-validation-summary hidden center-block col-centered">
<strong class="label label-danger col-centered"></strong>
</span>
<hr/>
<form method="POST" class="form-login-box" action="{{@app.context}}">
<label class="wr-input-label">First Name</label>
<div class="wr-input-control">
<input type="text right" id="first_name" placeholder="First Name">
</div>
<label class="wr-input-label">Last Name</label>
<div class="wr-input-control">
<input type="text right" id="last_name" placeholder="Last Name">
</div>
<label class="wr-input-label">Username</label>
<div class="wr-input-control">
<input type="text right" id="user_name" placeholder="Username ">
</div>
<label class="wr-input-label">Email</label>
<div class="wr-input-control">
<input type="text right" id="email" placeholder="Email">
</div>
<label class="wr-input-label">Password</label>
<div class="wr-input-control">
<input type="password" id="password" placeholder="Password">
</div>
<label class="wr-input-label">Confirm Password</label>
<div class="wr-input-control">
<input type="password" id="password_confirmation" placeholder="Confirm Password">
</div>
<div class="wr-input-label">
<input type="checkbox" name="Agree" id="t_and_c" value="Bike"> <span class="italic">
I Agree by Clicking register, you agree to the Terms and Conditions set out by this site</span>
</div>
<div class="wr-input-control">
<button class="wr-btn" id="add-user-btn">&nbsp;&nbsp;&nbsp;&nbsp;Register&nbsp;&nbsp;&nbsp;&nbsp;</button>
<button class="wr-btn" onclick="document.location.href='{{@app.context}}/login';">&nbsp;&nbsp;&nbsp;&nbsp;Cancel&nbsp;&nbsp;&nbsp;&nbsp;</button>
</div>
</form>
</div>
{{#zone "bottomLoginJs"}}
{{js "js/validate-register.js"}}
{{/zone}}

@ -0,0 +1,50 @@
function post(path, method) {
method = method || "post"; // Set method to post by default if not specified.
form.submit();
}
$( document ).ready(function() {
var currentHash = window.location.hash,
submitPath = $("#signInForm").attr("action");
if(currentHash=="#auth-failed") {
$('.wr-validation-summary p').text("Sorry!, Please make sure to enter correct username and password");
$('.wr-validation-summary').removeClass("hidden");
}else if(currentHash=="#error"){
$('.wr-validation-summary p').text("Sorry!, Error occured");
$('.wr-validation-summary').removeClass("hidden");
}
$('#signIn').click(function(){
var username = $("input#username").val(),
password = $("input#password").val();
if (!username) {
$('.wr-validation-summary p').text("Sorry!, Username cannot be empty.");
$('.wr-validation-summary').removeClass("hidden");
} else if (!password){
$('.wr-validation-summary p').text("Sorry!, Password cannot be empty.");
$('.wr-validation-summary').removeClass("hidden");
} else {
post(submitPath,"POST");
}
});
});
function submitLoginForm() {
var submitPath = $("#signInForm").attr("action");
$('form input').keypress(function() {
if(e.which == 10 || e.which == 13) {
if (!username) {
alert()
$('.wr-validation-summary p').text("Sorry!, Username cannot be empty.");
$('.wr-validation-summary').removeClass("hidden");
} else if (!password){
$('.wr-validation-summary p').text("Sorry!, Password cannot be empty.");
$('.wr-validation-summary').removeClass("hidden");
} else {
post(submitPath,"POST");
}
}
});
}

@ -0,0 +1,44 @@
<div class="container col-md-12 col-lg-3 col-centered wr-content wr-login col-centered">
<p class="page-sub-title">Login</p>
<hr/>
<!-- validation -->
<span class="wr-validation-summary hidden">
<p>Login failed. Check your credentials and try again.</p>
</span>
<div class="panel-body">
<form id="signInForm" method="POST"
class="{{defineZone "signInForm-class" scope="protected"}}"
action="{{@app.context}}/api/user/login">
<div class="wr-input-control">
<label class="wr-input-label">Username *</label>
<input type="text" name="username" class="form-control" placeholder="User Name"
id="username" autofocus="autofocus" />
</div>
<div class="wr-input-control">
<label class="wr-input-label">Password *</label>
<input type="password" name="password" class="form-control"
id="password" placeholder="Password" />
</div>
<div class="wr-input-control wr-btn-grp">
<button class="wr-btn btn-download-agent" id="signIn">&nbsp;&nbsp;&nbsp;&nbsp;Login&nbsp;&nbsp;&nbsp;&nbsp;</button>
</div>
<div class="form-group">
{{#if showRememberMe}}
<span class="pull-left">
<input type="checkbox" name="rememberMe" value="remember"> Remember me
</span>
{{/if}}
{{#if showCreateAccount}}
<a id="createAccount"
href="{{@app.context}}/register"
class="pull-right create-account">Create an account</a>
{{/if}}
</div>
</form>
</div>
</div>
</div>
{{#zone "bottomLoginJs"}}
{{js "js/login.js" }}
{{/zone}}
Loading…
Cancel
Save