In this Golang Web Development Series #25, we're building a complete Golang HTTP User Authentication System from scratch with the backend MySQL database by using Golang's official MySQL Database Driver. The Golang HTTP Authentication will consist of Golang User Registration, Golang Login Auth, Golang Password Reset, Golang Change Password, Golang Set Cookie, Golang Web Assembly (WASM), Golang Map Token, Golang Persisted Token, etc. with step by step guide here in Golang's Web Development Series.
#MaharlikansCode
#GolangWebDevelopment25
#GolangUserAuthenticationSystem
#MySQLDatabase
#YabiSeries5
#GolangWASM
#GolangWebAssembly
#GolangTutorial
#LearnGolangWebDevelopment
#Golang
#LifeAsSoftwareDeveloper
#Maharlikans
#FilipinoSoftwareDeveloper
Get Linode Account:
https://www.linode.com/?r=6aae17162e9...
If you go with extra mile for buying me a cup of coffee, I appreciate it guys: https://ko-fi.com/maharlikanscode
Source Codes:
package yabi
import (
"database/sql"
"errors"
"gowebapp/config"
"strings"
"time"
"github.com/itrepablik/sakto"
)
// CreateUser add a new user to the users collection
func CreateUser(dbCon *sql.DB, u User, confirmPassword string, tos bool) (int64, error) {
// Check if username is empty
if len(strings.TrimSpace(u.UserName)) == 0 {
return 0, errors.New("Username is Required")
}
// Check if the username is available or not
if !IsUserNameExist(dbCon, u.UserName) {
return 0, errors.New("Username is not available, please try again")
}
// Check if email is empty
if len(strings.TrimSpace(u.Email)) == 0 {
return 0, errors.New("Email is Required")
}
// Check if email address is valid or not
if !sakto.IsEmailValid(u.Email) {
return 0, errors.New("Invalid Email Address, please try again")
}
// Check if the email address is available or not
if !IsUserEmailExist(dbCon, u.Email) {
return 0, errors.New("Email is not available, please try again")
}
// Check if password is empty
if len(strings.TrimSpace(u.Password)) == 0 {
return 0, errors.New("Password is Required")
}
// Match both passwords
if strings.TrimSpace(confirmPassword) != strings.TrimSpace(u.Password) {
return 0, errors.New("Passwords didn't match, please try again")
}
// Check if Terms of service has been checked
if !tos {
return 0, errors.New("Terms of Service is Required, By joining " + config.SiteShortName + ", you're agreeing to our terms and conditions.")
}
// Hash and salt your plain text password
hsPassword, err := sakto.HashAndSalt([]byte(u.Password))
if err != nil {
return 0, err
}
// Now, insert the new user's information here
ins, err := dbCon.Prepare("INSERT INTO " + YabiUser + " (username, password, email, first_name, " +
"middle_name, last_name, suffix, is_superuser, is_admin, date_joined, is_active) VALUES" +
"(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
if err != nil {
return 0, err
}
// Pass on all the parameter values here
ins.Exec(u.UserName, hsPassword, u.Email, u.FirstName, u.MiddleName, u.LastName, u.Suffix, u.IsSuperUser,
u.IsAdmin, time.Now(), u.IsActive)
// Get the lastest inserted id
lid, err := GetLastInsertedID(dbCon, "id", YabiUser)
defer ins.Close()
return lid, nil
}
// GetLastInsertedID gets the latest inserted id for any specified table and it's auto_increment field
func GetLastInsertedID(dbCon *sql.DB, autoIDFieldName, tableName string) (int64, error) {
var id int64 = 0
err := dbCon.QueryRow("SELECT " + autoIDFieldName + " FROM " + tableName + " ORDER BY " + autoIDFieldName + " DESC LIMIT 1").Scan(&id)
if err != nil {
return 0, err
}
return id, nil
}
// IsUserNameExist check from the user's collection if it's existed or not, we don't allow to have a
// duplicate username, it must be a unique value
func IsUserNameExist(dbCon *sql.DB, userName string) bool {
var id int64 = 0
err := dbCon.QueryRow("SELECT id FROM "+YabiUser+" WHERE username = ?", userName).Scan(&id)
if err != nil {
if err == sql.ErrNoRows {
return true // returned no rows, the username is not found from the yabi table
}
return false
}
return false
}
// IsUserEmailExist check from the user's collection if it's existed or not, we don't allow to have a
// duplicate email, it must be a unique value
func IsUserEmailExist(dbCon *sql.DB, email string) bool {
var id int64 = 0
err := dbCon.QueryRow("SELECT id FROM "+YabiUser+" WHERE email = ?", email).Scan(&id)
if err != nil {
if err == sql.ErrNoRows {
return true // returned no rows, the email is not found from the yabi table
}
return false
}
return false
}
Get the full source codes:
https://github.com/maharlikanscode/Go...