Javascript Password - Switch or if/else?


#1

Hey;

Been working on a project where I’ve been using simple passwords for my project. I’ve been using this line of code which I found on an older forum thread and it’s been working great. I don’t really need it for security since it’s really just a game between friends.

var actualPassword = "luggage12345";
var attemptedPassword = prompt("Please enter the password");

if(attemptedPassword == actualPassword) {
    // success case
    hypeDocument.showSceneNamed("Protected Scene", hypeDocument.kSceneTransitionPushRightToLeft);
} else {
    // failure case
    alert("The password you entered is incorrect.");
}

Now it works but I’ve been trying to modify it so that instead of it working for just one password, it works for multiple different passwords at once. As in; if you entered in Bob, you’d go to Bob’s scene, or if you typed in Sarah you’d go to Sarah’s scene. I don’t know if I should be changing it to a switch statement or an if/else.

You’re help is always appreciated.
Thank you.


(Warren Adams-Ockrassa) #2

I’d go to a switch, partly because the statements stack more readily and are more human parseable, and partly because it would make debugging, maintenance, and scalability considerably easier.


#3

Yeah I was thinking that too but the switch won’t connect when I use it. The code I’m trying is:

switch attemptedPassword = prompt("Please enter Primary Password");
case(attemptedPassword == "sasha") {
    // success case
    hypeDocument.showSceneNamed("SashaS_Desk", hypeDocument.kSceneTransitionCrossfade);
} 
break;
case(attemptedPassword == "mark") {
        //second success case
    hypeDocument.showSceneNamed("MarkG_Desk", hypeDocument.kSceneTransitionCrossfade);
}
break;
default
    // failure case
    alert("Password Incorrect.");
}
break;
}

(Warren Adams-Ockrassa) #4

The syntax might be off here - I don’t see any colons following the case statements, and there appears to be a profusion of curlies. See if this works better:

switch attemptedPassword = prompt("Please enter Primary Password") {

case(attemptedPassword == "sasha"):
    // success case
    hypeDocument.showSceneNamed("SashaS_Desk", hypeDocument.kSceneTransitionCrossfade);
break;

case(attemptedPassword == "mark"):
        //second success case
    hypeDocument.showSceneNamed("MarkG_Desk", hypeDocument.kSceneTransitionCrossfade);
break;

default:
    // failure case
    alert("Password Incorrect.");

}

There’s an example here of analogous structure that illustrates what I mean:


(Warren Adams-Ockrassa) #5

Actually this is probably incorrect as well:

case(attemptedPassword == "sasha"):

…it more likely should be:

case "sasha":

…since attemptedPassword is already invoked in the switch declaration.


#6

I actually am using the else if and it’s working beautifully. I’ll write it out here when I can.


(mmatera) #7

I would love for you to post your code. I am a teacher and I have no idea how to code but would love to drop in your code and change the page names and passwords.

Thanks so much,

Michael


#8

Here’s the code like I promised. It’s a long else/if but it did the trick.
Very insecure but it’s for a game so there is no personal information.

var attemptedPassword;
var attemptedPassword = prompt("Alpha 527 please enter password");
if(attemptedPassword == "SASHA") {
	hypeDocument.showSceneNamed("Green Flash", hypeDocument.kSceneTransitionInstant);
} 
else if(attemptedPassword == "ALVIN") {
	hypeDocument.showSceneNamed("Cook", hypeDocument.kSceneTransitionInstant);
}
else if(attemptedPassword == "PAT") {
	hypeDocument.showSceneNamed("Gamble", hypeDocument.kSceneTransitionInstant);
}
else if(attemptedPassword == "JAMIE") {
	hypeDocument.showSceneNamed("Seen", hypeDocument.kSceneTransitionInstant);
}
else if(attemptedPassword == "MARK") {
	hypeDocument.showSceneNamed("Friends", hypeDocument.kSceneTransitionInstant);
}
else if(attemptedPassword == "TRACY") {
	hypeDocument.showSceneNamed("Prejudice", hypeDocument.kSceneTransitionInstant);	}
else if(attemptedPassword == "JORDAN") {
	hypeDocument.showSceneNamed("MindReader", hypeDocument.kSceneTransitionInstant);
}
else if(attemptedPassword == "ELIZABETH") {
	hypeDocument.showSceneNamed("TheBetrayer", hypeDocument.kSceneTransitionInstant);
}
else if(attemptedPassword == "AARON") {
	hypeDocument.showSceneNamed("Honey", hypeDocument.kSceneTransitionInstant);
}
else if(attemptedPassword == "T-27") {
	hypeDocument.showSceneNamed("Speechless", hypeDocument.kSceneTransitionInstant);
}
else {
    alert("You are not Alpha 527 Access Denied.");
}

(Warren Adams-Ockrassa) #9

This should work, in ‘switch/case’ format, if you’re hot to give it a try…

var attemptedPassword = prompt("Alpha 527 please enter password");
switch(attemptedPassword){
  case "SASHA":
    hypeDocument.showSceneNamed("Green Flash", hypeDocument.kSceneTransitionInstant);
    break;
  case "ALVIN":
    hypeDocument.showSceneNamed("Cook", hypeDocument.kSceneTransitionInstant);
    break;
  case "PAT":
    hypeDocument.showSceneNamed("Gamble", hypeDocument.kSceneTransitionInstant);
    break;
  case "JAMIE":
    hypeDocument.showSceneNamed("Seen", hypeDocument.kSceneTransitionInstant);
    break;
  case "MARK":
    hypeDocument.showSceneNamed("Friends", hypeDocument.kSceneTransitionInstant);
    break;
  case "TRACY":
    hypeDocument.showSceneNamed("Prejudice", hypeDocument.kSceneTransitionInstant);
    break;
  case "JORDAN":
    hypeDocument.showSceneNamed("MindReader", hypeDocument.kSceneTransitionInstant);
    break;
  case "ELIZABETH":
    hypeDocument.showSceneNamed("TheBetrayer", hypeDocument.kSceneTransitionInstant);
    break;
  case "AARON":
    hypeDocument.showSceneNamed("Honey", hypeDocument.kSceneTransitionInstant);
    break;
  case "T-27":
    hypeDocument.showSceneNamed("Speechless", hypeDocument.kSceneTransitionInstant);
    break;
  default:
    alert("You are not Alpha 527. Access Denied.");
}

It’s partly a question of how you prefer your code to look, I suppose. To my eye the multiple stackings on switch/case are easier to read, parse, and maintain than a series of if/then/else statements.

The biggest thing to remember is to add the break keyword after each statement that you want to be an escape from the cascade; otherwise, both that statement and the default will run.


(Ugo Mena) #10

There is another, albeit similar method to use. Certainly following what has been said earlier about how you want your code to look… Following that, I did not want my code to outright give away the passwords (like the above methods) and I did not want to resort to some OAUTH login either.

So here is my solution, it uses a similar JS login, but only looks for hype Scenes, if it does not find them, it goes to a Denied page, which lets you log in again. Many of you will recognize the hype linking to scene functions Linking to specific scenes

Looking at the code, a crafty person will also see that the “passwords” are in fact in plain site, just not where one would expect them to be… here is the hint, look at the div names.

Ok, here is the site, I coded this almost entirely in hype for a work project.

I would love to hear what folks here think.


(Ugo Mena) #11

this is what the password Javascript ends up looking like:

{pwd = prompt(‘Enter your frendbox serial number’,’’);
if (pwd !== “”) {
hypeDocument.showSceneNamed(pwd, hypeDocument.kSceneTransitionCrossfade);
hypeDocument.showSceneNamed(“Denied”, hypeDocument.kSceneTransitionInstant);
} else {
hypeDocument.showSceneNamed(“Denied”, hypeDocument.kSceneTransitionInstant);
}
}


Javascript on Submit button - thisName.value statement help
(Ugo Mena) #12

I will post the actual hype document for all to see later.


(Warren Adams-Ockrassa) #13

Definitely a cleaner option, yeah, provided the scene names match given passwords. :smiley:


(Ugo Mena) #14

in the password Javascript above, there is no password variable and there is no checking against what was entered. It checks to make sure it is not blank and then just passes the entered text directly into the hypeDocument.showSceneNamed variable. Either the scene is there and loads, or it isn’t, in which case the “Denied” scene is called.

I liked that it removes the need to have a separate password list to check (and reveal within the script) and just uses the scene names as “passwords”.

I found my hype document, let me know if you are interested and I will post.

best regards.


(Warren Adams-Ockrassa) #15

Exactly; it’s really quite an elegant solution.


#16

This is great! I’d love to see that hype project if you are able to share it!


(Mark Hunte) #17

This is nice.

Here is an example. Also note that scene names are case sensitive which is demonstrated in this example.
passwordBySceneName.hype.zip (44.1 KB)


(Ugo Mena) #18

Thank you for the nice comments regarding this example.
Here is the hype file. I have removed all of the branded elements to allow for better reuse.

JS_to_Scene_Login.hype.zip (51.5 KB)


(Ugo Mena) #19

I like how you have boiled this down to just one script. Nicely done Mark.

Looking back at my own version, I am not sure why I didn’t use the hypeDocument.sceneNames() API.


(Mark Hunte) #20

I actually thought I was just doing what you most likely had done in regards to your code snippet.
The only thing I added was the form fill instead of a prompt ( which I dislike) :smile: