Lots of fixes and misc changes.

main
Luna 2021-07-23 12:41:55 -07:00
parent 4e57c11eaa
commit d05db5c2de
9 changed files with 74 additions and 51 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
streamchat.code-workspace streamchat.code-workspace
assets/ assets/
todo.txt todo.txt
isonline.js

View File

@ -25,6 +25,10 @@ label {
} }
button {
padding: 1%;
}
#loggeduser { #loggeduser {
font-size: 30px; font-size: 30px;
} }
@ -44,6 +48,7 @@ label {
width: 100%; width: 100%;
height: 90%; height: 90%;
overflow-y: scroll; overflow-y: scroll;
overflow-x: hidden;
text-align: left; text-align: left;
color: white; color: white;
} }

View File

@ -30,23 +30,14 @@
<div id="loggeduser"></div> <div id="loggeduser"></div>
<script type="text/javascript">
window.onload = function() {
let a = document.getElementById("logoutlink");
a.onclick = function() {
logout()
return false;
}
}
</script>
<a id="logoutlink" href="stream.html">(logout)</a>
<div id="errormessage"></div>
<script src="chat.js"></script> <script src="chat.js"></script>
<script src="logout.js"></script> <script src="logout.js"></script>
<button type="button" onclick="logout()">Logout</button>
<div id="errormessage"></div>
</body> </body>
</html> </html>

View File

@ -1,5 +1,4 @@
// VARIABLES // VARIABLES
let date = '2021-07-22'
let messageCount = 0; let messageCount = 0;
let username = localStorage.getItem('username'); let username = localStorage.getItem('username');
const form = document.querySelector('form'); const form = document.querySelector('form');
@ -20,7 +19,7 @@ form.addEventListener("submit", async function (event) {
//SEND MESSAGE FETCH FUNCTION //SEND MESSAGE FETCH FUNCTION
async function sendMessage() { async function sendMessage() {
sendMessageInfo = { "name": username, "body": formMessage, "date": date } sendMessageInfo = { "name": username, "body": formMessage }
fetch('/api/message/send', { fetch('/api/message/send', {
method: 'POST', method: 'POST',
headers: { headers: {

View File

@ -33,7 +33,7 @@ a {
#chatbox { #chatbox {
flex: 1; flex: 1;
height: 50rem; height: 53rem;
margin-left: 0.3em; margin-left: 0.3em;
margin-right: -50%; margin-right: -50%;
border: none; border: none;

View File

@ -4,6 +4,7 @@
let uname = document.querySelector('#uname').value; let uname = document.querySelector('#uname').value;
let pin = document.querySelector('#pin').value; let pin = document.querySelector('#pin').value;
const form = document.querySelector('form'); const form = document.querySelector('form');
let username = localStorage.getItem('username');
// SUBMIT FORM FUNCTION. AND FETCH USERNAME AND PIN FROM API. // SUBMIT FORM FUNCTION. AND FETCH USERNAME AND PIN FROM API.
@ -14,24 +15,47 @@ form.addEventListener("submit", async function (event) {
uname = formData.get('uname'); uname = formData.get('uname');
pin = formData.get('pin'); pin = formData.get('pin');
const response = await fetch(`/api/users/${uname}/${pin}`); try {
const loginInfo = await response.json(); const loginInfo = await loginFetch();
if (loginInfo.status === "ok") { console.log(loginInfo)
login()
} else { if (loginInfo.status === 200) {
incorrectLogin() login()
} else {
incorrectLogin()
}
} catch(e) {
console.log(e);
document.querySelector("#errormessage").innerHTML = 'An Error has Occurred. Try again later. ' + e.toString();
} }
}) })
// LOGIN FETCH
async function loginFetch() {
let sendLoginInfo = { "name": uname, "pin": pin }
return await fetch('/api/login/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(sendLoginInfo),
});
}
// FUNCTIONS FOR WHETHER THE LOGIN WAS A SUCCESS OR FAILURE // FUNCTIONS FOR WHETHER THE LOGIN WAS A SUCCESS OR FAILURE
function login() { function login() {
console.log('You have logged in!') window.location.replace("/index.html")
document.querySelector("#username").innerHTML = `Logged in as ${uname}`
document.querySelector("#errormessage").innerHTML = '' document.querySelector("#errormessage").innerHTML = ''
localStorage.setItem("username", `${uname}`); localStorage.setItem('username', `${uname}`);
if (username != '') {
document.querySelector("#username").innerHTML = `Logged in as ${username}`
}
} }
function incorrectLogin() { function incorrectLogin() {

View File

@ -1,6 +1,9 @@
//VARIBLES //VARIBLES
let username = localStorage.getItem('username'); // //IF NOT LOGGED IN DON'T SHOW LOG IN BUTTON
// if (username === '') {
// document.getElementById("logoutlink").style.display = "none";
// }
//LOGOUT FETCH FUNCTION //LOGOUT FETCH FUNCTION
@ -11,10 +14,9 @@ async function logout() {
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
body: JSON.stringify(sendRegisterInfo), body: JSON.stringify(sendLogoutInfo),
}); });
document.querySelector("#errormessage").innerHTML = 'Logged out.' document.querySelector("#errormessage").innerHTML = 'Logged out.'
localStorage.removeItem('username') localStorage.removeItem('username')
}
}

View File

@ -6,7 +6,7 @@ let pin = document.querySelector('#pin').value;
let selected = document.querySelector('#selected').value; let selected = document.querySelector('#selected').value;
let custom = document.querySelector('#custom').value; let custom = document.querySelector('#custom').value;
let pronouns = '' let pronouns = ''
let responseText; let responseJson;
const form = document.querySelector('form'); const form = document.querySelector('form');
//SUBMIT FUNCTION &CHECKING IF USERNAME IS TAKEN //SUBMIT FUNCTION &CHECKING IF USERNAME IS TAKEN
@ -20,43 +20,40 @@ form.addEventListener("submit", async function (event) {
selected = formData.get('selected'); selected = formData.get('selected');
custom = formData.get('custom') custom = formData.get('custom')
if (custom !== '') { if (custom === '' && selected === 'none') {
pronouns = custom newPronouns = ''
} else if (custom !== '') {
newPronouns = custom
} else { } else {
pronouns = selected newPronouns = selected
} }
try { const response = await fetch(`api/users/${uname}/`);
const isNotTaken = await getUname(); const isTaken = await response.json();
if (isNotTaken.status === "fail") { //YES THIS IS CONFUSING I KNOW.
register() if (isTaken.status === "fail") {
} else { register()
document.querySelector("#errormessage").innerHTML = `${uname} is already taken.` } else {
} document.querySelector('#errormessage').innerHTML = `${uname} is already taken.`
} catch {
document.querySelector("#errormessage").innerHTML = 'An Error has Occurred. Try again later.'
} }
}) })
//FETCH FUNCTIONS. GETTING USERNAME FROM API & REGISTERING USER ASSIGNED NAME AND PIN. //FETCH FUNCTIONS. GETTING USERNAME FROM API & REGISTERING USER ASSIGNED NAME AND PIN.
async function getUname() {
let response = await fetch(`/api/users/${uname}`);
responseJson = await response.json();
return responseJson;
}
async function register() { async function register() {
let sendRegisterInfo = { "name": uname, "pin": pin, "pronouns": pronouns } let sendRegisterInfo = { "name": uname, "pin": pin, "pronouns": pronouns }
fetch('/api/register/', { fetch('/api/register/', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
body: JSON.stringify(sendRegisterInfo), body: JSON.stringify(sendRegisterInfo),
}); });
document.querySelector("#errormessage").innerHTML = 'Registered!' document.querySelector("#errormessage").innerHTML = 'Registered!'
window.location.replace("/login.html") window.location.replace("/login.html")
} }
// function errorMessage() {
// document.querySelector("#errormessage").innerHTML = 'An error has occurrred. Please try again later.'
// }

View File

@ -25,6 +25,10 @@ form {
padding-top: 1%; padding-top: 1%;
} }
#logoutlink {
display: block;
}
label { label {
font-family: "Lucida Console", "Courier New", monospace; font-family: "Lucida Console", "Courier New", monospace;
} }