Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using GTANetworkServer;
- using Insight.Database;
- using Insight.Database.Providers.MySql;
- using MySql.Data.MySqlClient;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using BCr = BCrypt.Net;
- namespace database
- {
- public class Main : Script
- {
- private static MySqlConnectionStringBuilder _database;
- private static IUserRepository _userRepository;
- public Main()
- {
- API.onResourceStart += API_onResourceStart;
- }
- private void API_onResourceStart()
- {
- MySqlInsightDbProvider.RegisterProvider();
- _database = new MySqlConnectionStringBuilder("server=localhost;user=root;database=generationfive;port=3306;password=MPjCbqdwLXDaijN8;");
- _userRepository = _database.Connection().As<IUserRepository>();
- }
- [Command("ins", GreedyArg = true)]
- public void CMD_UserRegistration(Client player, string password)
- {
- var hash = BCr.BCrypt.HashPassword(password, BCr.BCrypt.GenerateSalt(12));
- UserAccount account = new UserAccount
- {
- Username = player.name,
- Hash = hash
- };
- _userRepository.RegisterAccount(account);
- API.sendChatMessageToPlayer(player, "Vous êtes maintenant enregistré");
- }
- [Command("con", GreedyArg = true)]
- public void CMD_UserLogin(Client player, string password)
- {
- UserAccount account = _userRepository.GetAccount(player.name);
- bool isPasswordCorrect = BCr.BCrypt.Verify(password, account.Hash);
- if (isPasswordCorrect)
- {
- API.sendChatMessageToPlayer(player, "Connexion réussie!");
- }
- else
- {
- API.sendChatMessageToPlayer(player, "Mauvais mot de passe!");
- }
- }
- public interface IUserRepository
- {
- UserAccount RegisterAccount(UserAccount userAccount);
- UserAccount GetAccount(string name);
- }
- public class UserAccount
- {
- public string Username { get; set; }
- public string Hash { get; set; }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement