Update index.js
This commit is contained in:
parent
7d395b5016
commit
ecf485d305
1 changed files with 23 additions and 2 deletions
23
index.js
23
index.js
|
@ -1,4 +1,4 @@
|
||||||
let gun = Gun(['http://nodemixaholic.com:8069/gun', 'https://gun-manhattan.herokuapp.com/gun']);
|
let gun = Gun(['http://nodemixaholic.com:8765/gun', 'https://gun-manhattan.herokuapp.com/gun']);
|
||||||
let usrname;
|
let usrname;
|
||||||
|
|
||||||
function showMainPage() {
|
function showMainPage() {
|
||||||
|
@ -23,6 +23,25 @@ function showMainPage() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function validateUsername(username) {
|
||||||
|
if (!username) {
|
||||||
|
// Username is blank, null, or undefined
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// Perform additional validation if necessary, such as checking for invalid characters
|
||||||
|
|
||||||
|
// Check if the username exists in the database
|
||||||
|
const coredb = gun.get(`mayaspace`);
|
||||||
|
let userRef = coredb.get('users').get(username);
|
||||||
|
return userRef.once().then(function (userData) {
|
||||||
|
// If userData is null, the username doesn't exist in the database
|
||||||
|
return !!userData;
|
||||||
|
}).catch(function (error) {
|
||||||
|
console.error('Error validating username:', error);
|
||||||
|
return false; // Handle the error appropriately
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async function sha256HexPromise(data) {
|
async function sha256HexPromise(data) {
|
||||||
const msgUint8 = new TextEncoder().encode(data); // encode as (utf-8) Uint8Array
|
const msgUint8 = new TextEncoder().encode(data); // encode as (utf-8) Uint8Array
|
||||||
const hashBuffer = await crypto.subtle.digest("SHA-256", msgUint8); // hash the message
|
const hashBuffer = await crypto.subtle.digest("SHA-256", msgUint8); // hash the message
|
||||||
|
@ -101,7 +120,9 @@ function post() {
|
||||||
const coredb = gun.get(`mayaspace`);
|
const coredb = gun.get(`mayaspace`);
|
||||||
const postsDB = coredb.get('posts');
|
const postsDB = coredb.get('posts');
|
||||||
if (post.length < 1001) {
|
if (post.length < 1001) {
|
||||||
|
if (validateUsername(usrname)) {
|
||||||
postsDB.put(postData);
|
postsDB.put(postData);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
alert("max post length is 1000 chars!")
|
alert("max post length is 1000 chars!")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue