DEV Community

Cover image for How to protect admin dashboard using Html,Css and Js
Prince_Np
Prince_Np

Posted on

4 2

How to protect admin dashboard using Html,Css and Js

In this brief tutorial, I will present how you can protect the Admin Panel and User Panel to your Html,Css and Javascript website or application consuming NodeJs Backend. You can use this knowledge to build an entire application with access roles for managing different sort of users.

first you must have the token for authentication and additional you need to have the user data in the Local storage as show in the demo

Image description

then the role in the user payload is the one we are going to be basing on while redirecting and protecting the admin and user dashboard by implying these 2 function that will be applied or called to each page of the admin or user accordingly .

Function to check if you are authenticated

this function need to be applied to the login and register page so that the authenticated user can not be able to access them.


function checkAuthentication() {
    let user = localStorage.getItem("user");
    if (user) {
        user = JSON.parse(user);
        const role = user.role;
        const admin = "admin";
        if (role === admin) {
            window.location.href = "../admin/dashboard.html";
        } else if (role != admin) {
            window.location.href = "../user/dashboard.html";
        } else {
        }
    } else {
        // do nothing
    }
}
checkAuthentication();

Enter fullscreen mode Exit fullscreen mode

this function will protect your login page and register page .

Function to protect Admin dashboard

this function need to be applied to all pages that you need to be accessed only by the admin but the practice is used was to group together all admin files in the admin directory and user files in the user directory so that the Un-authenticated user can not be able to access them.

function checkAdmin() {
    let user = localStorage.getItem("user");
    if (user) {
        user = JSON.parse(user);
        const role = user.role;
        const admin = "admin";
        if (role === admin) {
            console.log("Admin");
        } else if (role != admin) {
            window.location.href = "../user/dashboard.html";
        } else {
            window.location.href = "../login.html";
        }
    } else {
        console.log("User");
    }
}
checkAdmin();
Enter fullscreen mode Exit fullscreen mode

as you can see I'm basing on the user role that have been stored in the local storage so that I can redirect the user if he/she tries to tamper with the admin files.

Function to protect User dashboard

this function need to be applied to all pages that you need to be accessed only by the user so that the Un-authenticated user can not be able to access them.

function checkAuthUser() {
    let user = localStorage.getItem("user");
    if (user) {
        user = JSON.parse(user);
        const role = user.role;
        const standardUser = "standard-user";
        if (role === standardUser) {
            // do nothing 
        } else if (role != standardUser) {
            window.location.href = "../admin/dashboard.html";
        } else {
            window.location.href = "../login.html";
        }
    } else {
     // do nothing
    }
}
Enter fullscreen mode Exit fullscreen mode

for this all your routes will be protected and safe form outside users .

if you are interested this the link to the Github repository , don't forget to like and follow to support for future posts,
Thanks .

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more