use websockets to logout remote users
This commit is contained in:
		@ -1,3 +1,32 @@
 | 
			
		||||
export let socket;
 | 
			
		||||
 | 
			
		||||
export function wsConn(is_authenticated, user_id) {
 | 
			
		||||
    if (is_authenticated !== 1) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    socket = new WebSocket("ws://localhost:4000/ws")
 | 
			
		||||
    socket.onopen = () => {
 | 
			
		||||
        console.log("Successfully connected to websockets")
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    socket.onclose = event => {};
 | 
			
		||||
    socket.onerror = error => {};
 | 
			
		||||
 | 
			
		||||
    socket.onmessage = msg => {
 | 
			
		||||
        let data = JSON.parse(msg.data);
 | 
			
		||||
 | 
			
		||||
        switch (data.action) {
 | 
			
		||||
            case "logout":
 | 
			
		||||
                if (data.user_id === user_id) {
 | 
			
		||||
                    logout()
 | 
			
		||||
                }
 | 
			
		||||
                break;
 | 
			
		||||
            default:
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// let loginLink = document.getElementById("login-link");
 | 
			
		||||
// let vtLink = document.getElementById("vt-link");
 | 
			
		||||
//
 | 
			
		||||
@ -11,9 +40,9 @@
 | 
			
		||||
//     loginLink.classList.remove('d-none')
 | 
			
		||||
// });
 | 
			
		||||
 | 
			
		||||
// function logout() {
 | 
			
		||||
//     localStorage.removeItem("token");
 | 
			
		||||
//     localStorage.removeItem("token_expiry");
 | 
			
		||||
//     location.href = "/logout";
 | 
			
		||||
// }
 | 
			
		||||
function logout() {
 | 
			
		||||
    localStorage.removeItem("token");
 | 
			
		||||
    localStorage.removeItem("token_expiry");
 | 
			
		||||
    location.href = "/logout";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,3 +1,5 @@
 | 
			
		||||
import {socket} from "./base.js"
 | 
			
		||||
 | 
			
		||||
export function showUsers(api) {
 | 
			
		||||
    const tbody = document.getElementById("user-table").getElementsByTagName("tbody")[0];
 | 
			
		||||
    const token = localStorage.getItem("token");
 | 
			
		||||
@ -14,6 +16,7 @@ export function showUsers(api) {
 | 
			
		||||
    fetch(api + "/api/admin/all-users", requestOptions)
 | 
			
		||||
        .then(response => response.json())
 | 
			
		||||
        .then(function (data) {
 | 
			
		||||
            console.log(data)
 | 
			
		||||
            if (data) {
 | 
			
		||||
                data.forEach(i => {
 | 
			
		||||
                    let newRow = tbody.insertRow();
 | 
			
		||||
@ -63,21 +66,6 @@ export function showUser(api, userID) {
 | 
			
		||||
            document.getElementById("last_name").value = data.last_name;
 | 
			
		||||
            document.getElementById("email").value = data.email;
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
    delBtn.addEventListener("click", function () {
 | 
			
		||||
        Swal.fire({
 | 
			
		||||
            title: "Are you sure?",
 | 
			
		||||
            text: "You won't be able to undo this!",
 | 
			
		||||
            icon: "warning",
 | 
			
		||||
            showCancelButton: true,
 | 
			
		||||
            confirmButtonColor: "#3085d6",
 | 
			
		||||
            cancelButtonColor: "#d33",
 | 
			
		||||
            confirmButtonText: 'Delete User',
 | 
			
		||||
        }).then((result) => {
 | 
			
		||||
            console.log("would delete user id", id);
 | 
			
		||||
        });
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function saveUser(api, event) {
 | 
			
		||||
@ -160,6 +148,11 @@ export function deleteUser(api) {
 | 
			
		||||
                    if (!data.ok) {
 | 
			
		||||
                        Swal.fire("Error" + data.message)
 | 
			
		||||
                    } else {
 | 
			
		||||
                        let jsonData = {
 | 
			
		||||
                            action: "deleteUser",
 | 
			
		||||
                            user_id: parseInt(id),
 | 
			
		||||
                        };
 | 
			
		||||
                        socket.send(JSON.stringify(jsonData));
 | 
			
		||||
                        location.href = "/admin/all-users"
 | 
			
		||||
                    }
 | 
			
		||||
                });
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user