This repository has been archived on 2025-12-28. You can view files and clone it, but cannot push or open issues or pull requests.
inkletblot-forum-com-v1/scripts/auth.controllers.js

159 lines
4.7 KiB
JavaScript

daddy.controller("loginCtrlr", function($scope, $cookies, $location, $http, stateData){
let titles = {
login : "Login!",
signup : "Not a Member?"
}
$scope.titles = titles
let message = {
start : "Feel free to",
link : "Sign Up",
end : "at any time!"
}
$scope.message = message
$scope.setSignup = () => {
stateData.setPage("forum")
$location.path("/signup")
}
$scope.login = () => {
$scope.status = true
$scope.loginStatus = "Logging you in..."
console.log("logging in")
console.log("requesting authentication from server")
$http({
url : server + "/auth/login?userName="+$scope.user.username+"&userPass="+$scope.user.password,
method : "GET"
}).then((res) => {
console.log(res)
if (res.status == 200) {
console.log("request granted")
let today = new Date()
let expired = new Date(today)
expired.setDate(today.getDate()+1)
$cookies.putObject('user', res.data, {expires : expired})
console.log("set cookie")
stateData.setUser(res.data)
console.log("set data")
stateData.setPage("forum")
$location.path("/")
}
}).catch((res) => {
if (res.status == 400) {
$scope.loginStatus = "Incorrect information!"
console.log("request denied")
} else if (res.status == 500) {
$scope.loginStatus = "Login Failed."
console.log("something went wrong: " + res)
}
})
}
})
daddy.controller("signupCtrlr", function($scope, $http, $location, stateData) {
let titles = {
form : "Sign up here!",
success : "Signup Success!"
}
$scope.titles = titles
let message = {
start : "You account has been created!",
line2 : "Feel free to",
link : "Login",
end : "whenever you like."
}
$scope.message = message
$scope.success = false
$scope.exists = false
$scope.status = false
$scope.setLogin = () => {
stateData.setPage("forum")
$location.path("/login")
}
$scope.signup = () => {
$scope.userExists()
$scope.passwordsMatch()
$scope.emailGood()
if ($scope.match && !$scope.exists && $scope.goodemail) {
$scope.status = false
$scope.submit()
} else {
$scope.status = true
$scope.signupStatus = "Please correct errors above."
}
}
$scope.emailGood = () => {
if (validateEmail($scope.user.email)) {
$scope.goodemail = true
$scope.emailError = ""
} else {
$scope.goodemail = false
$scope.emailError = "< invalid email."
}
}
$scope.passwordsMatch = () => {
if ($scope.user.password != $scope.user.confirmPassword) {
$scope.match = false
$scope.passwordError = "< password does not match."
} else {
$scope.match = true
$scope.passwordError = ""
}
}
$scope.submit = () => {
$http({
url : server + "/auth/signup?userName=" + $scope.user.username + "&userPass=" + $scope.user.password + "&userEmail=" + $scope.user.email,
method : "POST"
}).then((res) => {
if (res.status == 200) {
console.log("user created successfully")
$scope.success = true
$scope.signupForm.$setPristine()
$scope.user.username = ""
$scope.user.password = ""
$scope.user.confirmPassword = ""
$scope.user.email = ""
}
}).catch((res) => {
if (res.status == 500) {
console.log("whoops...")
}
})
}
$scope.userExists = () => {
console.log("checking username is used?")
$http({
url : server + "/auth/exists?userName=" + $scope.user.username,
method : "POST"
}).then((res) => {
if (res.status == 200) {
console.log(res.data)
if (res.data == true) {
$scope.exists = true
$scope.usernameError= "< username already exists."
} else {
$scope.exists = false
$scope.usernameError = ""
}
}
}).catch((res) => {
if (res.status == 500) {
console.log ("something went wrong")
}
})
}
})