Mar 31, 2019 at 11:55am UTC
Hi i did a program with a mysql auth system and it returns errors but i dont find them,would be thankful if someone help me!
login.cpp code:
#include "login.h"
#include <iostream>
struct auth {
char* address;
char* username;
char* password;
char* database;
int port;
};
int login_check(char* username, char* password, std::string hwid, MYSQL* & sql) {
auth login;
login.address = "address";
login.username = "user";
login.password = "password";
login.database = "database";
login.port = 3306;
sql = mysql_init(sql);
if (mysql_real_connect(sql, login.address, login.username, login.password, login.database, login.port, nullptr, NULL)) {
printf("Failed to Connect: %s\n", mysql_error(sql));
mysql_close(sql);
sql = NULL;
return FAILED_CONNECTION;
}
else
{
printf("Server is OK!\n");
}
std::string query = std::string( "SELECT * FROM users WHERE ") + " hwid='" + hwid + "' AND toggled=true AND username='" + username +"' AND password = '" + password + "';" ;
if (!mysql_query(sql, query.c_str())) {
printf("Failed to query for login: %s", mysql_error(sql));
mysql_close(sql);
sql = NULL;
return FAILED_QUERY;
}
MYSQL_RES* result = mysql_store_result(sql);
if (result)
{
printf("Login is OK");
mysql_close(sql);
sql = NULL;
return SUCCESS;
}
else
{
printf("Bad Login.");
mysql_close(sql);
sql = NULL;
return BADLOGIN;
}
}
int register_user(char* username, char* password, std::string hwid, MYSQL*& sql) {
auth login;
login.address = "address";
login.username = "username";
login.password = "password";
login.database = "database";
login.port = 3306;
sql = mysql_init(sql);
if (mysql_real_connect(sql, login.address, login.username, login.password, login.database, login.port, nullptr, NULL)) {
printf("Failed to Connect: %s\n", mysql_error(sql));
mysql_close(sql);
sql = NULL;
return FAILED_CONNECTION;
}
else
{
printf("Server is OK!\n");
}
std::string query = std::string("SELECT * FROM users WHERE username = '") + username + "';";
if (!mysql_query(sql, query.c_str())) {
printf("Failed to query for login: %s", mysql_error(sql));
mysql_close(sql);
sql = NULL;
return FAILED_QUERY;
}
MYSQL_RES* result = mysql_store_result(sql);
if (result)
{
printf("User already exists");
mysql_close(sql);
sql = NULL;
return SUCCESS;
}
else
{
query = std::string("INSERT IGNORE INTO `autoclicker`.`users` (`hwid`, `toggled`, `username`, `password`) VALUES ('" + hwid + "', 'false', '" + username + "', '" + password + "');");
std::cout << query;
if (!mysql_query(sql, query.c_str())) {
printf("Failed to register: %s", mysql_error(sql));
mysql_close(sql);
sql = NULL;
return FAILED_QUERY;
}
else {
std::string query = std::string("SELECT * FROM users WHERE username = '") + username + "';";
if (!mysql_query(sql, query.c_str())) {
printf("Failed to query for login: %s", mysql_error(sql));
mysql_close(sql);
sql = NULL;
return FAILED_QUERY;
}
MYSQL_RES* result2 = mysql_store_result(sql);
if (result2) {
mysql_close(sql);
sql = NULL;
printf("\nRegistered Sucessfully!");
return BADLOGIN;
}
else
{
mysql_close(sql);
sql = NULL;
const char *teste = mysql_error(sql);
return 1;
}
}
}
}
login.h code:
#pragma once
#include <mysql.h>
#include <string>
#include "picosha2.h"
#pragma comment(lib,"libmysql.lib")
#define FAILED_CONNECTION -1
#define FAILED_QUERY -2
#define BADLOGIN 0
#define SUCCESS 1
int login_check(char* username, char* password, std::string hwid, MYSQL*& sql);
int register_user(char* username, char* password, std::string hwid, MYSQL*& sql);
Last edited on Mar 31, 2019 at 10:44pm UTC