[Tutorial] Register/Login System Using DJson *Includes DUDB Conversion!
#2

Okay, now it's time to make /login.
Add the login command in OnPlayerCommandText:
pawn Код:
dcmd(login,5,cmdtext);
Now we start how we did with /register
pawn Код:
dcmd_login(playerid, params[])
{
  new PlayerName[24];
  GetPlayerName(playerid, PlayerName, 24);
  if(AccountExists(PlayerName))
  {
    if(AccountGetInt(PlayerName, "LoggedIn") == 1) return SendClientMessage(playerid, COLOR_RED, "You are already logged in!");
    if(!strlen(params)) return SendClientMessage(playerid, COLOR_RED, "/login password [Password is CaSE SeNSiTivE]");
    if(strlen(params) > 20) return SendClientMessage(playerid, COLOR_RED, "Password cannot be greater than 20 characters.");
    if(strlen(params) < 3) return SendClientMessage(playerid, COLOR_RED, "Password cannot be less than 3 characters.");
  } else return SendClientMessage(playerid, COLOR_RED, "Account does not exist. Please use /register password.");
  return 1;
}
Alright, now what we have here is getting the PlayerName once again and checking if the account exists. This time we have an if saying if the account does exist, continue. Otherwise, skip down to the bottom else and tell them that their account doesn't exist and that they need to use /register first. So if their account does exist we run a few checks. The 2nd, 3rd, and 4th we are already familiar with from /register. But the first one, even though we used it at one point you may not recognize. We are simply checking if they are logged in or not and if they are we tell them that they are already logged in.

Now lets add the password check:
pawn Код:
dcmd_login(playerid, params[])
{
  new PlayerName[24];
  GetPlayerName(playerid, PlayerName, 24);
  if(AccountExists(PlayerName))
  {
    if(AccountGetInt(PlayerName, "LoggedIn") == 1) return SendClientMessage(playerid, COLOR_RED, "You are already logged in!");
    if(!strlen(params)) return SendClientMessage(playerid, COLOR_RED, "/login password [Password is CaSE SeNSiTivE]");
    if(strlen(params) > 20) return SendClientMessage(playerid, COLOR_RED, "Password cannot be greater than 20 characters.");
    if(strlen(params) < 3) return SendClientMessage(playerid, COLOR_RED, "Password cannot be less than 3 characters.");
    new buf[145];
    WP_Hash(buf, sizeof(buf), params);
    new PwCheck[145];
    new str[35];
    format(str, sizeof(str), "%s/password", PlayerName);
    format(PwCheck, sizeof(PwCheck), "%s", dj("accounts.json", str));
    if(strcmp(PwCheck, buf, false) == 0)
    {
     
    }
  } else return SendClientMessage(playerid, COLOR_RED, "Account does not exist. Please use /register password.");
  return 1;
}
Okay, since our 'secret code' cannot be decrypted, meaning we cannot turn it back into normal text, we take the params they enter in /login and convert that into the secret code format. Than we compare the password they entered to the one stored on file. If they match, let them login, otherwise, they have the invalid password.

So, we encrypt the params they enter just like in /register, than we create another format which has that %s/password again. That is our file-password location. We use this in our next new format. We called it PwCheck. Notice the size is 145, the same as what we used for our buf in /register and right in login here. Remember, I said you should keep it consistent? Alrighty, now as you can see all that's in the format is %s. Remember what that is? It means we are replacing it for a string. In this case we are getting the password stored on file and replacing it there. So now we have both secret password codes stored. The one submitted in /login is stored in buf, and the one on file is stored as PwCheck. Now we have a new if. I starts with strcmp? What the heck is that? strcmp stands for, "String Compare". Right, so now the pieces of the puzzle all fit in. We are comparing the two strings(the two encrypted passwords) to see if they match. If they match it will = 0. So if it equals 0 (matches) we continue.

pawn Код:
dcmd_login(playerid, params[])
{
  new PlayerName[24];
  GetPlayerName(playerid, PlayerName, 24);
  if(AccountExists(PlayerName))
  {
    if(AccountGetInt(PlayerName, "LoggedIn") == 1) return SendClientMessage(playerid, COLOR_RED, "You are already logged in!");
    if(!strlen(params)) return SendClientMessage(playerid, COLOR_RED, "/login password [Password is CaSE SeNSiTivE]");
    if(strlen(params) > 20) return SendClientMessage(playerid, COLOR_RED, "Password cannot be greater than 20 characters.");
    if(strlen(params) < 3) return SendClientMessage(playerid, COLOR_RED, "Password cannot be less than 3 characters.");
    new buf[145];
    WP_Hash(buf, sizeof(buf), params);
    new PwCheck[145];
    new str[35];
    format(str, sizeof(str), "%s/password", PlayerName);
    format(PwCheck, sizeof(PwCheck), "%s", dj("accounts.json", str));
    if(strcmp(PwCheck, buf, false) == 0)
    {
      AccountSetInt(PlayerName, "LoggedIn", 1);
      SendClientMessage(playerid, COLOR_SUCCESS, "You have successfully logged in!");
    }
  } else return SendClientMessage(playerid, COLOR_RED, "Account does not exist. Please use /register password.");
  return 1;
}
So now we simply set their LoggedIn value to 1, meaning they have logged in successfully and tell them they have logged in successfully.


- Credits -
-Lavamike [Making the tutorial]
-Dracoblue [Making DJson & DUDB and all your other useful things]
-Cez/Cezar [Making Cez-ACC]
-****** [Making the Whirlpool plugin]
If I missed someone please let me know


I hope some people find it useful!
-Mike
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)