Checking authentication on the backend
This commit is contained in:
		@ -62,3 +62,32 @@ export function stripeInit(pubKey) {
 | 
			
		||||
        });
 | 
			
		||||
    })();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function checkAuth(api) {
 | 
			
		||||
    if (localStorage.getItem("token") === null) {
 | 
			
		||||
        location.href = "/login";
 | 
			
		||||
        return
 | 
			
		||||
    } else {
 | 
			
		||||
        let token = localStorage.getItem("token")
 | 
			
		||||
        const myHeaders = new Headers();
 | 
			
		||||
        myHeaders.append("Content-Type", "application/json");
 | 
			
		||||
        myHeaders.append("Authorization", "Bearer " + token);
 | 
			
		||||
 | 
			
		||||
        const requestOptions = {
 | 
			
		||||
            method: "POST",
 | 
			
		||||
            Headers: myHeaders,
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        fetch(api + "/api/is-authenticated", requestOptions)
 | 
			
		||||
        .then(response => response.json())
 | 
			
		||||
            .then(function(data) {
 | 
			
		||||
                if (data.error === true) {
 | 
			
		||||
                    console.log("not logged in");
 | 
			
		||||
                    location.href = "/login"
 | 
			
		||||
                } else {
 | 
			
		||||
                    console.log("Logged in");
 | 
			
		||||
                }
 | 
			
		||||
            })
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user