Pdnell
Pdnell

Reputation: 321

AppleScript password checking

I'm trying to get simple user authentication going via AppleScript. The goal is to have the passwords check against each other and then continue on with the rest of the script. Right now the script can recognize mis-matched password if password_initial is correct and password_final is incorrect, but if password_initial is incorrect and password_final is correct there is no logic to check against itself.

set user_name to ""
display dialog "Please enter your user name" default answer "firstname.lastname"
set the user_name to the text returned of result

set password_initial to ""
display dialog "Please enter your password:" default answer "" with hidden answer
set password_initial to the text returned of result

display dialog "Please verify your password below." buttons {"OK"} default answer "" with hidden answer
set password_final to text returned of result
considering case
    repeat while password_final is not equal to password_initial
        display dialog "Passwords do not match, please re-enter your password below." buttons {"OK"} default answer "" with hidden answer
        set password_final to text returned of result
    end repeat
end considering

--do something

Can anyone point me in the right direction on this? Thanks!

Upvotes: 2

Views: 1660

Answers (2)

scohe001
scohe001

Reputation: 15446

Whoa whoa whoa, handlers and global variables? No need for all that, why not just throw the whole thing into a loop and then break it when we get what we want?

set user_name to text returned of (display dialog "Please enter your user name" default answer "firstname.lastname")

set display_text to "Please enter your password:"
repeat
    considering case
        set init_pass to text returned of (display dialog display_text default answer "" with hidden answer)
        set final_pass to text returned of (display dialog "Please verify your password below." buttons {"OK"} default button 1 default answer "" with hidden answer)
        if (final_pass = init_pass) then
            exit repeat
        else
            set display_text to "Mismatching passwords, please try again"
        end if
    end considering
end repeat

#Rest of script here...

Upvotes: 1

markhunte
markhunte

Reputation: 6932

The trick with something like this is to use Handlers.

These are bits of code that can be called to run within your script as many times as you want and save on rewriting the same code over again.

They can also help you not use repeat loops like you are doing. Also you should always add the 'cancel' button to your display dialogs. If there is bad logic in a repeat loop or a handler call the user needs a way to exit it.

I have also made some of the display dialog text dynamic. and used some global variables

I have tested this code which has Handles and the calls to them and it works well in my testing. But it is an example and should give you enough to move forward.

 set displays to {"Please enter your password:", "Please verify your password below.", "Passwords do not match, please re-enter your password below."}

    set user_name to add_user_name() #get user name

    set thedisplay to item 1 of displays #set the fisrt dialog for the password display to the first item in displays
    global thedisplay, displays, password_initial #global variables

   set password_final to setDetails() #Call to start the password dialogs

--your code here ..

  #HANDLERS  
    on setDetails()

        set password_initial to add_password() #call to get user password

        set password_final to verify_password() #call to get verify password
    end setDetails


    on add_user_name()
        display dialog "Please enter your user name" buttons {"Cancel", "OK"} default answer "firstname.lastname"
        set the user_name to the text returned of result
        return user_name
    end add_user_name

    on add_password()

        display dialog thedisplay buttons {"Cancel", "OK"} default answer "" with hidden answer
        set password_initial to the text returned of result
        return password_initial
    end add_password

    on verify_password()
        set thedisplay to item 2 of displays #set the dialog for the password  verify display to the second item in displays
        display dialog thedisplay buttons {"Cancel", "OK"} default answer "" with hidden answer
        set password_final to text returned of result

        considering case

            if password_final is not equal to password_initial then
                set thedisplay to item 3 of displays #set the dialog for the password  verify display to the third item in displays
                my setDetails() # start over again asking for password as it did not does not match dialog displays will also change accordingly 
            else
                set thedisplay to item 2 of displays #set the dialog for the password  verify display to the second item in displays
            end if
        end considering
        return password_final
    end verify_password

Upvotes: 1

Related Questions