1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. GTA Network forum is now in archive mode.

MySQL With User System - In-depth Tutorial From Start to Finish

Discussion in 'Tutorials/Documentations' started by andreasb, Jan 8, 2017.

  1. andreasb

    andreasb Well-Known Member

    Messages:
    128
    Likes Received:
    61
    Joined:
    Aug 4, 2016
    So I've seen quite a few questions regarding MySQL, user registration/login systems, ORMs, password security etc., so I decided to write a very detailed step-by-step guide for those who might not be very familiar or experienced with this. It is filled up with images illustrating each point, which is also the reason why it's quite lengthy.

    Throughout this tutorial you’ll learn to
    • set up a new GTA Network Server project in Visual Studio,
    • add references and NuGet packages to your project,
    • use the Insight.Database ORM (Object-Relational Mapper) for MySQL communication (so you can avoid certain hurdles you normally encounter),
    • set up a new database and tables in MySQL using phpMyAdmin,
    • code the foundation for a secure user account system with login and registration using Bcrypt for password security,
    • learn how to deal with database stored procedures/routines

    Read the tutorial here: https://github.com/andreasbmp/gtan-mysql-tutorial/blob/master/DetailedMySQLLoginandRegistrationTutorial.pdf
    (did not want to re-format the entire thing into multiple forum posts)

    NOTE: I’m using WAMPserver to get MySQL and phpMyAdmin easily installed. It’s a prerequisite that you have phpMyAdmin and a MySQL server running or understanding of how to apply this tutorial to another database and management tool. This tutorial will not go through setup of such.

    I am also not active here anymore, thus I won't answer questions or PMs unless I happen to come by.
     

    Attached Files:

    Last edited: May 26, 2017
  2. Jack

    Jack Member Donator

    Messages:
    31
    Likes Received:
    15
    Joined:
    Jan 3, 2017
    Great tutorial! although i wouldn't do mine this way, I guess everyone has their own way of programming.

    Kind Regards,
    JC
     
  3. andreasb

    andreasb Well-Known Member

    Messages:
    128
    Likes Received:
    61
    Joined:
    Aug 4, 2016
    Thanks!
    And yes, I agree! nod~~
     
  4. brux0

    brux0 Member

    Messages:
    12
    Likes Received:
    1
    Joined:
    Dec 19, 2016
    Hey, it's really nice tutorial! Thank you.

    I'm now curious how check if user already exist in database. Hope you'll do some more tutorials!
     
    andreasb likes this.
  5. andreasb

    andreasb Well-Known Member

    Messages:
    128
    Likes Received:
    61
    Joined:
    Aug 4, 2016
    Hey,

    I'll try to expand this into a series of tutorials, but they take a few hours to make due to their level of detail.
    However, checking if a user(name) already exists upon registration, you could of course expand the CMD_UserRegistration command handler to include the _userRepository.GetAccount(player.name); method used in the Login handler. If that method does not return a user it means that no user with that username exists. Then you could do an if check etc., basic C# :=)
     
    Last edited: Jan 8, 2017
  6. Goncho28

    Goncho28 Member Donator

    Messages:
    44
    Likes Received:
    12
    Joined:
    Dec 27, 2016
    Excellent tutorial, take your like ;-)
     
  7. HidroDF

    HidroDF Member

    Messages:
    19
    Likes Received:
    2
    Joined:
    Jan 13, 2017
    Excellent.

    I'm trying to do check if username exists in DataBase but I don't understand how I can make it.

    The _userRepository.GetAccount(player.name); cannot be a boolean like isPasswordCorrect.

    Error CS0029 Cannot implicitly convert type 'users.UserAccount' to 'bool'

    How I can do to include the method?

    Thanks!
     
  8. OscarTabu

    OscarTabu New Member

    Messages:
    3
    Likes Received:
    1
    Joined:
    Dec 23, 2016
    Hi, I've followed your tutorial and it worked for me, but I have a problem. When I want to log in, it gives me an error in the console over an object and it will not be solved by myself.
     
  9. andreasb

    andreasb Well-Known Member

    Messages:
    128
    Likes Received:
    61
    Joined:
    Aug 4, 2016
    Need more info than this. Screenshots of console and database (table, table content), perhaps?

    No, GetAccount() will return an account if it exists. If it doesn't exist, it'll probably return null or similar, which you could use in a conditional statement.
     
  10. isee

    isee Member

    Messages:
    12
    Likes Received:
    5
    Joined:
    Dec 12, 2016
    Hi, thanks a lot for this tutorial!!
    Sorry to bother you with this but when i try to login the chat message isn´t sent, i think that the login part isn´t working for me althogh the register part it´s working!
    I´ve noticed my console with some information, but im starting to code and im still a noob.
    Hope you can help me and help others with the same problem, here´s what was written on the console:
    Hope that you can identify my noobish error.
    Sorry, some parts are in Portuguese.
    Thanks!! DBerror.jpg
     
    Last edited: Jan 14, 2017
  11. andreasb

    andreasb Well-Known Member

    Messages:
    128
    Likes Received:
    61
    Joined:
    Aug 4, 2016
    Hey! Glad you liked it. Regarding your errors. I guess there´s a variable/property called name somewhere (according to one of the last lines in your console message) which doesn't exist. I need to see some code here to better suggest fixes. Can you paste some code into the pastebin and post the link here?
    Pastebin: https://pastebin.gtanet.work/
     
  12. isee

    isee Member

    Messages:
    12
    Likes Received:
    5
    Joined:
    Dec 12, 2016

    Thanksss a lot for your fast repply!!
    heres the code for the Main.cs
    https://pastebin.gtanet.work/?f2465e1ac9792164#PKvVunt1n3jMncTwokocSmMrC29G0jA1wQq9/QEumao=

    and here´s for the meta.xml
    https://pastebin.gtanet.work/?715372f0d4a38465#hUgT/5d8Qq2FUDLLRzONTS8QdPHz6MFb3y5V8yURSoU=


    Thanks a lot Andreas!
     
  13. andreasb

    andreasb Well-Known Member

    Messages:
    128
    Likes Received:
    61
    Joined:
    Aug 4, 2016
    Thanks!
    Well, on line 71 in your script you have the following line of code:
    Code:
    UserAccount GetAccount(string password);
    You're passing in string password as an argument while you should pass in string name. So you get this error because the stored procedure called GetAccount() in MySQL tries to find a variable called name, but it doesn't find it because you called it password instead :=)
     
  14. isee

    isee Member

    Messages:
    12
    Likes Received:
    5
    Joined:
    Dec 12, 2016
    HAHAHAHHAHAHAHAHA thanksssssss a lot Andreas!!!
    Fixed, gonna try it!

    ----> edit: Already test it and It´s working! :=D :=D :=D :=D :=D

    Thanks a lot for this tutorial and your help!!!
     
    Last edited: Jan 14, 2017
    andreasb likes this.
  15. HidroDF

    HidroDF Member

    Messages:
    19
    Likes Received:
    2
    Joined:
    Jan 13, 2017
    Yes, I've deduced but what type of conditional statement I should use?

    For example, an IF statement cannot be used like:

    if(GetAccount(player.name))
    {
    // Registred
    }
    else
    {
    // Not Registred
    }

    Because GetAccount() doesn't return true/false/1/0. Do you have any idea on how that works? Thanks!
     
  16. OscarTabu

    OscarTabu New Member

    Messages:
    3
    Likes Received:
    1
    Joined:
    Dec 23, 2016
  17. Goncho28

    Goncho28 Member Donator

    Messages:
    44
    Likes Received:
    12
    Joined:
    Dec 27, 2016
    I didn't try this login system and i don't know what data provides "GetAccount" but you can test what is the result if the account doesn't exist and then in the if you can chef if(value = x) // The account is unregistered, else, the account is registered. Hope you can do it, regards!
     
  18. Nicoo

    Nicoo Member

    Messages:
    16
    Likes Received:
    1
    Joined:
    Nov 27, 2016
    Hello @andreasb i try on windows and it's perfectly work,but on Linux, i have this error when i try to type a cmd: [​IMG]
     
  19. Jorge Santamaria

    Jorge Santamaria Member

    Messages:
    38
    Likes Received:
    7
    Joined:
    Dec 8, 2016
    Install the new client / server update and now when I use the / ul or / ur command I get this error:

    [02:07:19] EXCEPTION IN RESOURCE newserver INSIDE SCRIPTENGINE Main
    [02:07:19] System.Reflection.TargetInvocationException: Se produjo una excepción en el destino de la invocación. ---> Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: No se puede convertir NULL en 'int' porque es un tipo de valor que no acepta valores NULL.
    en CallSite.Target(Closure , CallSite , Object )
    en System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
    en NewServer.Main.CMD_UserLogin(Client player, String password)
    --- Fin del seguimiento de la pila de la excepción interna ---
    en System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
    en System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
    en System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
    en GTANetworkServer.ScriptingEngine.<>c__DisplayClass26_0.<InvokeVoidMethod>b__0() en C:\Users\Adam\Documents\Git\gtanet-src\mtav\Server\ResourceInfo.cs:línea 176
    en GTANetworkServer.ScriptingEngine.MainThreadLoop() en C:\Users\Adam\Documents\Git\gtanet-src\mtav\Server\ResourceInfo.cs:línea 109
    [02:07:58] EXCEPTION IN RESOURCE newserver INSIDE SCRIPTENGINE SpawnManager
    [02:07:58] Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: No se puede convertir NULL en 'float' porque es un tipo de valor que no acepta valores NULL.
    en CallSite.Target(Closure , CallSite , Object )
    en System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
    en RPGResource.SpawnManager.SpawnCitizen(Client target)
    en RPGResource.SpawnManager.ClientEvent(Client sender, String eventName, Object[] args)
    en GTANetworkServer.ScriptingEngine.<>c__DisplayClass58_0.<InvokeClientEvent>b__0() en C:\Users\Adam\Documents\Git\gtanet-src\mtav\Server\ResourceInfo.cs:línea 590
    en GTANetworkServer.ScriptingEngine.MainThreadLoop() en C:\Users\Adam\Documents\Git\gtanet-src\mtav\Server\ResourceInfo.cs:línea 109
     
  20. -Andreas

    -Andreas Well-Known Member

    Messages:
    172
    Likes Received:
    25
    Joined:
    Sep 29, 2016
    Thanks for the guide, but I got a question. We're using the ORM to avoid having to write a lot of MySql commands all the time or have I not understood this correctly? At the moment I've archived something like this without using an ORM like Insight. But is it better to use an ORM, and what's the benefit of Insight above others.
     

Share This Page