Skip to content

Commit

Permalink
sign in script moved to the actual script file
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryanrob327 committed Nov 3, 2023
1 parent 0420646 commit b0bff50
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 60 deletions.
55 changes: 27 additions & 28 deletions assets/sign-in/script.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
function signIn(event) {
event.preventDefault();
document.getElementById('signInForm').addEventListener('submit', function(event) {
event.preventDefault();

// Get email and password from the form
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
var email = document.getElementById('email').value;
var password = document.getElementById('password').value;

// authentication logic (validate email and password)
/*
if (email == (get email data) && password == (get password data)) {
const isAuthenticated = true;
}
else {
const isAuthenticated = false;
}
*/
// Make a POST request to your authentication endpoint
fetch('https://cj-backend.stu.nighthawkcodingsociety.com/human/authenticate', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
email: email,
password: password
})
})
.then(response => response.json())
.then(data => {
// Assuming the response contains a 'token' field
var token = data.token;
// Store the token in localStorage or a cookie for future use
// For simplicity, we're using localStorage here
console.log(token);
localStorage.setItem('token', token);

// For demo purposes, let's assume successful authentication
const isAuthenticated = true;

if (isAuthenticated) {
// Set a cookie with the user's email
document.cookie = `userEmail=${email}; expires=Sun, 1 Jan 2023 00:00:00 UTC; path=/`;

// Redirect to the home page
window.location.href = '/';
} else {
// Display an error message (you can customize this based on your authentication logic)
alert('Authentication failed. Please check your email and password.');
}
}
alert('Sign in successful!');
})
.catch(error => console.error('Error:', error));
});
33 changes: 1 addition & 32 deletions sign_in.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,7 @@ <h2>Sign In</h2>
</form>
</div>

<script>
document.getElementById('signInForm').addEventListener('submit', function(event) {
event.preventDefault();

var email = document.getElementById('email').value;
var password = document.getElementById('password').value;

// Make a POST request to your authentication endpoint
fetch('https://cj-backend.stu.nighthawkcodingsociety.com/human/authenticate', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
email: email,
password: password
})
})
.then(response => response.json())
.then(data => {
// Assuming the response contains a 'token' field
var token = data.token;
// Store the token in localStorage or a cookie for future use
// For simplicity, we're using localStorage here
console.log(token);
localStorage.setItem('token', token);

alert('Sign in successful!');
})
.catch(error => console.error('Error:', error));
});
</script>
<script src="{{site.baseurl}}/assets/sign-in/script.js"> </script>

</body>
</html>

0 comments on commit b0bff50

Please sign in to comment.