@@ -18,7 +18,10 @@ import (
1818
1919//Gets the env variable from the .env file, if none exist it will create one.
2020func getEnvVariables (envname string ) string {
21+
22+ //Checks if the .env file is present in the exe directory.
2123 if _ , err := os .Stat (".env" ); err != nil {
24+ //Creates a standard .env file in exe directory.
2225 fmt .Println ("No .env file found, creating a new one..." )
2326 envfile := "DB_USERNAME=\n DB_PASSWORD=\n DB_HOSTNAME=\n DB_TABLENAME="
2427 os .Create (".env" )
@@ -27,6 +30,7 @@ func getEnvVariables(envname string) string {
2730 os .Exit (0 )
2831 return ""
2932 } else {
33+ //Loads the env with the specified key unless its empty and returns the value.
3034 err = godotenv .Load (".env" )
3135 if err != nil {
3236 panic (err )
@@ -50,30 +54,39 @@ func hash(username string, password string) (hash string) {
5054
5155//Opens a database connection using the configured env's, returns database connection.
5256func SqlConnect () (db * sql.DB ) {
57+ //Gets the enviroment variables from the .env file.
5358 usernameSQL := getEnvVariables ("DB_USERNAME" )
5459 passwordSQL := getEnvVariables ("DB_PASSWORD" )
5560 hostnameSQL := getEnvVariables ("DB_HOSTNAME" )
5661 tablenameSQL := getEnvVariables ("DB_TABLENAME" )
62+
63+ //Starts a database connection using the enviroment variables from the .env.
5764 db , err := sql .Open ("mysql" , usernameSQL + ":" + passwordSQL + "@tcp(" + hostnameSQL + ")/" + tablenameSQL )
5865 if err != nil {
5966 panic (err )
6067 }
6168 return db
6269}
6370
71+ //Checks if the username or email exists in database.
6472func sqlCheckIfExists (db * sql.DB , username string , email string ) (userExists bool ) {
6573 getUsernameQuery := "SELECT Username FROM `users` WHERE Username = ?"
6674 usernameSelect , err := db .Query (getUsernameQuery , username )
6775 defer usernameSelect .Close ()
6876 if err != nil {
6977 panic (err )
7078 }
79+
80+ //Creates and executes SQL query from row Email Where Email is the email-adress entered by the user.
7181 getEmailQuery := "SELECT Email FROM `users` WHERE Email = ?"
7282 emailSelect , err := db .Query (getEmailQuery , email )
83+
7384 defer emailSelect .Close ()
7485 if err != nil {
7586 panic (err )
7687 }
88+
89+ //Scans the query results and translates them to strings, then checks if they are both empty.
7790 var usernameSelectScan string
7891 var emailSelectScan string
7992 usernameSelect .Next ()
@@ -89,9 +102,13 @@ func sqlCheckIfExists(db *sql.DB, username string, email string) (userExists boo
89102
90103//Registers the userdata into the database if the username and email are unique.
91104func SqlRegister (db * sql.DB , username string , password string , email string ) (allowRegister bool ) {
105+ //Checks if the username or email exists in the database.
92106 if sqlCheckIfExists (db , username , email ) {
107+ //If it exists returns false
93108 return false
109+
94110 } else {
111+ //If it doesnt exist it creates and executes an SQL query to INSERT the new userdata into the database.
95112 insertQuery := "INSERT INTO `users` (Username, Email, Authentication) VALUES (?, ?, ?)"
96113 insert , err := db .Query (insertQuery , username , email , hash (username , password ))
97114 defer insert .Close ()
@@ -105,17 +122,20 @@ func SqlRegister(db *sql.DB, username string, password string, email string) (al
105122
106123//Attempts to login the user with the username and password.
107124func SqlLogin (db * sql.DB , username string , password string ) (allowLogin bool ) {
125+ //Creates and executes SELECT Authentication from Users table Where username is the username entered by the user.
108126 loginQuery := "SELECT Authentication FROM `users` WHERE Username = ?"
109127 login , err := db .Query (loginQuery , username )
110128 defer login .Close ()
111129 if err != nil {
112130 return false
113131 }
114- login .Next ()
115132
133+ //Scans through the query results and saves it to a variable.
116134 var passwordCheck string
135+ login .Next ()
117136 login .Scan (& passwordCheck )
118137
138+ //Checks if the password entered by the user is the same as the hashed password that belongs to the username that was entered.
119139 if hash (username , password ) == passwordCheck {
120140 return true
121141 } else {
@@ -125,6 +145,7 @@ func SqlLogin(db *sql.DB, username string, password string) (allowLogin bool) {
125145
126146//Creates a random token for specified user.
127147func SqlCreateToken (db * sql.DB , username string ) (allowCreateToken bool ) {
148+ //Creates and executes SQL UPDATE Users table with a token that is generated from the username + unixtime for the user.
128149 createTokenQuery := "UPDATE `users` SET Token = ? WHERE Username = ?"
129150 rand .Seed (time .Now ().UnixNano ())
130151 random := strconv .Itoa (rand .Intn (1000 ))
@@ -139,6 +160,7 @@ func SqlCreateToken(db *sql.DB, username string) (allowCreateToken bool) {
139160
140161//Deletes the token for the user.
141162func SqlDeleteToken (db * sql.DB , username string ) (allowDeleteToken bool ) {
163+ //UPDATE the Token row in Users table in sql to contain NULL effectively deleting the token.
142164 deleteTokenQuery := "UPDATE `users` SET Token = NULL WHERE Username = ?"
143165 delete , err := db .Query (deleteTokenQuery , username )
144166 defer delete .Close ()
@@ -151,6 +173,7 @@ func SqlDeleteToken(db *sql.DB, username string) (allowDeleteToken bool) {
151173
152174//Requests the token for the specified user from the database.
153175func SqlGetToken (db * sql.DB , username string ) (allowGetToken bool , tokenString string ) {
176+ //SELECTS Token FROM users table for the user and returns the token.
154177 getTokenQuery := "SELECT Token FROM `users` WHERE Username = ?"
155178 token , err := db .Query (getTokenQuery , username )
156179 defer token .Close ()
0 commit comments