Hi there …
As @russian-dima suggested, you need a DB to store your members data, I suggest these fields:
-
name : ( Text ) Store the member’s name in this field.
-
owner : ( Reference Field ) A reference field to the built-in members database.
-
bans : ( Array ) an array of objects of the bans.
-
warnings : ( Array ) an array of objects of the warnings.
On the page or lightbox you use to let your members login, shift your login code to the backend, and when users attempts to login, check against the warnings DB, and redirect them to the appropriate page/lightbox if necessary, or login them in if everything is fine.
Backend: login.jsw
import { login } from 'wix-users-backend';
import wixData from 'wix-data';
export async function login(email, password) => {
try {
const login_res = await login(email, password).then((x) => {
return { type: 'success', token: x }
}).catch(() => {
return { type: 'error', message: 'Your email or password are incorrect' }
})
if (login_res.type === 'success') {
return checkMember(email).then((status) => {
if (status.clean) {
return { ok: true, token: login_res.token }
} else {
return { ok: false, type: status.type, time: status.remaining }
}
})
} else {
throw new Error(login_res.message)
}
} catch(message) {
return Promise.reject({ type: 'error', message });
}
}
// Get the member by its email address
async function getMemberByEmail(email) {
try {
// get the member
const member = await wixData.query('Members/PrivateMembersData')
.eq('loginEmail', email)
.find({suppressAuth: true})
.then((x) => { return x.items[0] });
if (member) {
return Promise.resolve(member);
} else {
throw new Error('Unable to find the member');
}
} catch(message) {
return Promise.reject({ type: 'error', message });
}
}
// Check the membership status of a member via its email
function checkStatus(member_id) {
return wixData.query('Details').eq('owner', member_id).find({suppressAuth: true}).then(x => {
if (x.length > 0) {
const member = x.items[0];
if (member.bans && member.bans.length > 0) {
// Sort the items in this array by their creation date
// .... Sort here.
/* Filter the array to only return the items where the
ban is less than 3 days old, check how long remains
and resolve this promise with a "clean: false" and
and remaining is the time difference between the current
time and the creation time */
const bans = []; // New array for the active bans
if (bans.length > 0) {
return Promise.resolve({
clean: false,
type: 'ban',
remaining: <time left>
})
}
}
if (member.warnings && member.warnings.length > 0) {
// Do the same as the above, make the necessary changes.
}
}
// return a default ok response;
return { clean: true }
})
}
// A function to do what the above two functions do
async function checkMember(email) {
try {
const member = await getMemberByEmail(email);
return checkStatus(member._id);
} catch (err) {
return Promise.reject(err);
}
}
On your login page:
import { login } from 'backend/login.jsw';
import { applySessionToken } from 'wix-users';
// Inside the event of the login event's trigger;
$w('#loginBtn').onClick(() => {
cosnt email = $w('#email').value.toLowerCase();
const pass = $w('#password').value;
return login(email, pass).then((result) => {
if (result.ok) {
// if everything is okay, login the user in
applySessionToken(result.token);
} else {
/* If the code reaches this stage, that means a ban or warning
has been set on this account, now you decide what you want
to do */
const type = result.type; // Either "ban" or "warning".
const remaining = result.remaining; // the number of milliseconds left;
}
})
});
Hope this long answer helps.
Ahmad