Skip to content

Instantly share code, notes, and snippets.

@RafhaanShah
Last active December 20, 2022 10:27
Show Gist options
  • Save RafhaanShah/a53534453cf244e7d397ebfb6d8a628c to your computer and use it in GitHub Desktop.
Save RafhaanShah/a53534453cf244e7d397ebfb6d8a628c to your computer and use it in GitHub Desktop.
Wix Stuff
// https://www.wix.com/velo/forum/community-discussion/how-to-embed-a-pdf-on-a-dynamic-page-tutorial
// https://www.wix.com/velo/forum/community-discussion/creating-a-viewable-pdf-in-wix
import wixData from 'wix-data';
import wixLocation from 'wix-location';
let src;
$w.onReady(function() {
$w("#dataset1").onReady(() => {
const item = $w('#dataset1').getCurrentItem();
src = item.file // FIELD KEY from the dataset for the document file
let url = src.split("/")[3]; // split full path to get pdf file name
if (src.includes('ugd')) {
url = src.split("/")[4]; // if ugd is in the path then it's index 4
}
$w('#html1').src = `https://docs.wixstatic.com/ugd/${url}`; // set the src for the html box
})
});
// Wix site code to log out a user if they login on another browser
// The code in this file will load on every page of your site
import wixUsers from 'wix-users';
import {
session
} from 'wix-storage';
import wixData from 'wix-data';
// gets run when your site is ready and loaded
$w.onReady(function() {
let sessionLogin = session.getItem("loginTime"); // get session saved login time
let user = wixUsers.currentUser; // get current user
// if the user has no session, log them out to force a login
if (!sessionLogin && user.loggedIn) {
wixUsers.logout();
}
// check if session matches the back end every 60 seconds
setInterval(checkForOtherLogins, 60000);
});
function checkForOtherLogins() {
let user = wixUsers.currentUser;
let sessionLogin = session.getItem("loginTime");
if (user.loggedIn) {
wixData.get("UserLogins", user.id)
.then((results) => {
if (sessionLogin != results.lastLoginTime) { // uses '!=' for type coercion
wixUsers.logout(); // log out the user if they have logged in somewhere else more recently
}
})
.catch((err) => {
console.error(err);
});
}
}
// gets run when a user logs in
wixUsers.onLogin((user) => {
let userId = user.id; // get current user ID
let curTime = Date.now(); // get current timestamp
session.setItem("loginTime", curTime); // saves it in session storage
let toInsert = {
"_id": userId,
"userId": userId,
"lastLoginTime": curTime
};
// update old value saved in the back-end
wixData.update("UserLogins", toInsert)
.then((results) => {
let item = results;
})
.catch((updateErr) => {
// if update fails (because it does not exist), do an insert instead
wixData.insert("UserLogins", toInsert)
.then((results) => {
let item = results;
})
.catch((insertErr) => {
console.error(insertErr);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment