[Tutorial] Creating password changing system using dialogs
#1

Introduction
I have not seen any kind of tutorial related to password changing system with dialogs and nicely written code (with no loose indentation and bugs or bad coding skills). This code is fully written and tested by myself. And i will recommend everyone to read every single code and information instead of copy and pasting (which I'm personally sure that most people do). I basically made this for my gamemode, using the AndySedeyn's Y_INI tutorial and I will prefer, you guys to use the AndySedeyn's tutorial (link is given below).

Necessities
- A script which uses YSI library and whirlpool or a new script which uses AndySedeyn's tutorial : https://sampforum.blast.hk/showthread.php?tid=597639.
- The YSI library: https://sampforum.blast.hk/showthread.php?tid=570883
- Whirlpool: https://sampforum.blast.hk/showthread.php?tid=570945
- ZCMD command processor : https://sampforum.blast.hk/showthread.php?tid=91354

Script

PHP Code:
enum Dialog_Data // all of the data of the 'enum' initialiser, is saved in the memory.
{
    
DIALOG_CHANGEPS,
    
DIALOG_CHANGEPS2
}; 
As we are going to use dialogs for changing the password, I prefer using the dialog ids with 'enum' initialiser, for ease and no tension related to mixing/collision of the dialog ids.

PHP Code:
CMD:changepass(playeridparams[])
{
    
ShowPlayerDialog(playeridDIALOG_CHANGEPSDIALOG_STYLE_INPUT"Changing Password - Step 1""Input your current password, in order to change your password.""Next""Cancel"); // showing the player a dialog
    
return 1;

We have created a command using the ZCMD command processor, and we are showing the player a dialog, to enter his current password, so he/she can move on to the next and final step of changing password.

PHP Code:
public OnDialogResponse(playeriddialogidresponselistiteminputtext[])
{
    switch(
dialogid)
    {
        case 
DIALOG_CHANGEPS:
        {
            if(
response)
            {
                new 
hashpass[129]; // the whirlpool uses a 129 length string for hashing the password.
                
WP_Hash(hashpasssizeof(hashpass), inputtext); // to hash the input data
                
if(!strcmp(hashpassPlayerInfo[playerid][Password])) // if the password matches, continue to step 2
                
{
                    
ShowPlayerDialog(playeridDIALOG_CHANGEPS2DIALOG_STYLE_INPUT"Changing Password - Step 2""Input your desired new password, in order to comple the process.""Okay""Cancel"); // showing the player a second and final dialog to enter his new password
                
}
                else
                {    
// if the password is incorrect, re-show them to enter the current and correct password
                    
SendClientMessage(playeriddarkred"ERROR: You have entered an incorrect password."); // no need of explanation
                    
ShowPlayerDialog(playeridDIALOG_CHANGEPSDIALOG_STYLE_INPUT"Changing Password - Step 1""Input your current password, in order to change your password.""Next""Cancel"); // showing player the dialog to enter his correct and current password
                
}
            }
            return 
1;
        } 
// end of the dialog part one 
read the commented lines in the code above.

PHP Code:
        case DIALOG_CHANGEPS2:
        {
            if(
response)
            {
                if(!
strlen(inputtext)) // if nothing is entered in the dialog
                
{
                    return 
SendClientMessage(playeriddarkred"ERROR: You must enter a password.");
                }
                new 
string[128];
                
                
format(stringsizeof(string), "Your password is: %s"inputtext); // we have formatted a string to include variables inside it
                
SendClientMessage(playerid, -1string); // sending the above string message 
                
                
new INI:file INI_Open(UserPath(playerid)); // opened the player's save file
                
WP_Hash(PlayerInfo[playerid][Password], 129inputtext); // hashed the password in the player enum
                
INI_WriteString(file"Password"PlayerInfo[playerid][Password]); // replaced the old hashed password with the new one
                
INI_Close(file);
                return 
1;
            }
        }
    }
    return 
0;
// On Dialog Response code end 
Read the commented lines in the code above.

And we are done. If you find any issue or bugs or typos, feel free to notify me. If you liked the tutorial and/or going to use it.
Reply


Messages In This Thread
Creating password changing system using dialogs - by Logic_ - 13.03.2016, 11:49
Re: Creating password changing system using dialogs - by saffierr - 13.03.2016, 12:24
Re: Creating password changing system using dialogs - by AndySedeyn - 13.03.2016, 12:43

Forum Jump:


Users browsing this thread: 1 Guest(s)