-2

I'm pretty new to HTML, like 1 week new. I am making a web store and I want to be able to login into an "admin panel" to make it easier for me to manage my products. Add new, remove, rename etc. My problem is, I have my login information stored in the html code and I use if-statements to check the validity.

When I was testing the code, I was curious and wanted to inspect element. Unsurprisingly, there was my entire login information and anybody can have access to it.

I need to somehow hide it, or hide the login fields from users except me. But I do not know how to approach that. I thought of a few solutions like have a hidden part on the store page and if I click it a certain amount of times then it will show the fields. But I think I'm complicating it.

Any ideas are greatly appreciated. Thanks. Below is my function for logging in.

function login()
    {
        var username = "test username";
        var password = "testpassword";

        if(document.getElementById("username field").value == username && document.getElementById("password field").value == password)
        {
            var btn = document.createElement("BUTTON");
            document.body.appendChild(btn);

            <!-- hide the user name field after login -->
            document.getElementById("username field").hidden = true;

            <!-- hide the password field after login -->
            document.getElementById("password field").hidden = true;

            <!-- hide the login button after login -->
            document.getElementById("login btn").hidden = true;

            <!-- show a message indicating login was successfull -->
            window.alert("Login successfull! Welcome back admin!")
        }
        else
        {
            window.alert("Sorry, you are not authorized to view this page.");
        }
    }

And this is a screenshot of the inspect element. I don't want anything too crazy like a database because I'm the only user, just a way to be able to access the admin panel without exposing myself. Thanks again.

Inspect Element Screenshot

EDIT:

I am not using my own server, I am using Wix.com to make the initial website and then using the HTML widget to create a webstore. I don't think they allow people to have any communication with their servers whatsoever.

NoobxCamper
  • 33
  • 1
  • 2
  • 14
  • Possible duplicate of [How do I protect javascript files?](http://stackoverflow.com/questions/4766834/how-do-i-protect-javascript-files) – Siguza Apr 26 '16 at 16:23

3 Answers3

3

Username and password validation should never be done on the client side. It should always be done on the server. Do not use javascript for this task. Allow your user to enter their username and password in a form, and then submit the form to a server side script to validate their credentials. Doing it on the client side will never be secure.

Michael Arrison
  • 1,464
  • 2
  • 13
  • 22
  • I forgot to mention that I am using Wix as my host. I am using their HTML code widget to make my own custom store. So I am guessing that makes them my server too? If so, then I wouldn't be able to have a server side script right? Because it's not my server .-. – NoobxCamper Apr 26 '16 at 16:29
  • If you don't have access to server side code, you should look up third party authenticators like some sort of social login. Anything that your client does will be visible. – Akshay Shenoy Apr 26 '16 at 16:35
0

If you serve the pages via a server, you can enforce basic HTTP auth. Should be really simple to set up and you would have the benefit of a standard of security.

Here are the Apache docs for this, for example.

casraf
  • 21,085
  • 9
  • 56
  • 91
  • I don't use my own server. I use Wix.com as a host and they serve the website itself. I am using their HTML widget to create the webstore. So I basically have no access to their server code nor would they even let me. – NoobxCamper Apr 26 '16 at 16:44
0

There's no easy solution to your particular request, but before I oblige you with the details I'd like to stress three very important points.

1: Javascript is not Safe

Javascript is a client side language, which means every piece of data you'll ever be dealing with that comes from your user can be directly modified. These include, but are not limited too, any values or attributes of HTML tags, inline Javascript, loaded image files, etc. Essentially, anything that is cached on the user's computer can be modified and might not be what you're expecting to receive.

As such, a Javascript authentication system is absolutely not safe by any definition of the word. For a local page that only you can access, it would do the job, but that begs the question of why you need authentication in the first place. Even then, as a new developer you'd be widely encouraged to never try do it anyway. There's no point practising and learning how to do something in a completely insecure way and nobody is likely to suggest it.

2: Authentication is a tricky topic

Authenticating logins is not an easy thing to do. Yes, it's easy to make a login script but it's not easy to do it properly. I would never try to discourage anyone from making something themselves nor discourage a new developer from pursuing any goal, but authentication is not something you should be learning only a week into HTML. I'm sorry if that comes across as harsh, but there are people who have been masterminding applications for years who still don't do it securely.

3: Third Party are Best

It's possible to make your own authentication system that likely only the most determined of attackers could access, but it wouldn't involve Javascript authentication. Consider Javascript to be more of a convenience to the user than a solution for the developer. Javascript can do some remarkable things, but being a trusted source of data is something it will never do. Please understand this important point, because the source code you have provided is riddled with security flaws.

--

Now, on to what you want to do. Identifying that you're the "admin" user is something you're putting a password in to do. If you could figure out you're the owner of this site before putting in your password, you wouldn't need the password, right? In short, you can't do what you want to do; not reliably, anyway. It's possible to only show those forms if you're using a particular IP, but IPs can be masked, imitated and changed, which makes it insecure.

There are several third party authentication methods that you can use to do all the heavy lifting for you. All you do is put the fields on your page and they'll handle the rest. You can use any Social Media login (Facebook, Twitter, Google Plus, etc) or you can use O Auth, which deals with all the heavy lifting of authentication for you.

I don't mean to discourage you, nor anyone else, from pursuing their own authentication methods but if I'm honest with you I think this is something way beyond your skill level that you shouldn't be considering right now.

Woody Payne
  • 595
  • 4
  • 15
  • And from your edit, I'd suggest that this is something you shouldn't be attempting to do on your side. Without the sufficient server side authentication it'd be unwise to be showing anything that people shouldn't be accessing. If you absolutely must do so, use OAuth, because you *shouldn't* need access to your server to use it, unless something has changed since I lasted used them. – Woody Payne Apr 26 '16 at 16:50
  • Thank you for the explanations and for taking the time to type it out. I will be checking out O Auth first and then consider your suggestion to use a social login form. Thanks again, by far the most helpful answer. – NoobxCamper Apr 26 '16 at 16:53