In this Golang Web Development Series #30, 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
#GolangWebDevelopment30
#GolangUserAuthenticationSystem
#MySQLDatabase
#YabiSeries10
#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:
yabi/user.go:
package yabi
import (
"database/sql"
"errors"
"fmt"
"gowebapp/config"
"strings"
"time"
"github.com/itrepablik/itrlog"
"github.com/itrepablik/sakto"
"github.com/itrepablik/sulat"
"github.com/itrepablik/timaan"
)
// LoginUser validate the user's account from the use
func LoginUser(dbCon *sql.DB, u User, isSiteKeepMe bool, expireInDays int) (bool, error) {
// Check if userName is empty
if len(strings.TrimSpace(u.UserName)) == 0 {
return false, errors.New("Username is Required")
}
// Check if password is empty
if len(strings.TrimSpace(u.Password)) == 0 {
return false, errors.New("Password is Required")
}
// Get the user's stored hash password.
pwHash, err := GetUserPassword(dbCon, u.UserName)
if err != nil {
return false, errors.New("Oops!, error getting user's credential, please try again")
}
// Now, match the two passwords, check if it's verified or not.
isPassHashMatch, err := sakto.CheckPasswordHash(u.Password, pwHash)
if err != nil {
return false, errors.New("Oops!, either of your username or password is wrong, please try again, thank you")
}
if isPassHashMatch {
// Get the user's information from the "yabi_user" table
mUser := GetUserInfo(dbCon, u.UserName)
// Generate new timaan token for the new user's session token
// Must convert all different type of values to a string value
tokenPayload := timaan.TP{
"USER_ID": fmt.Sprint(mUser.ID),
"USERNAME": fmt.Sprint(mUser.UserName),
"EMAIL": fmt.Sprint(mUser.Email),
"FIRST_NAME": fmt.Sprint(mUser.FirstName),
"MIDDLE_NAME": fmt.Sprint(mUser.MiddleName),
"LAST_NAME": fmt.Sprint(mUser.LastName),
"SUFFIX": fmt.Sprint(mUser.Suffix),
"IS_SUPER_USER": fmt.Sprint(mUser.IsSuperUser),
"IS_ADMIN": fmt.Sprint(mUser.IsAdmin),
"LAST_LOGIN": fmt.Sprint(mUser.LastLogin),
"DATE_JOINED": fmt.Sprint(mUser.DateJoined),
}
// Set the user's cookie expiry in days, if not provided, yabi use its default value to 30 days
if expireInDays lt 1 {
expireInDays = ExpireCookieInDays // expire in 30 days
}
// Check if the isSiteKeepMe = true or not
var tokenExpiry int64 = time.Now().Add(time.Minute * 30).Unix() // default to 30 minutes
if isSiteKeepMe {
tokenExpiry = time.Now().Add(time.Hour * time.Duration(24*expireInDays)).Unix()
}
fmt.Println("tokenExpiry: ", tokenExpiry)
tok := timaan.TK{
TokenKey: mUser.UserName,
Payload: tokenPayload,
ExpireOn: tokenExpiry,
}
_, err = timaan.GenerateToken(mUser.UserName, tok)
if err != nil {
itrlog.Error("error generating token during login: ", err)
return false, errors.New("Oops!, there was an error during encoding process, please try again, thank you")
}
// Get the user's stored hash password from the yabi_user table
return true, nil
}
return false, errors.New("Invalid Credentials, either of your username or password is wrong, please try again, thank you")
}
// GetUserInfo gets the user's information from the "yabi_user" table
func GetUserInfo(dbCon *sql.DB, userName string) *User {
var u User
err := dbCon.QueryRow("SELECT id, username, email, first_name, middle_name, last_name, suffix, is_superuser, "+
"is_admin, IFNULL(last_login, NOW()), date_joined FROM "+YabiUser+" WHERE username = ? ORDER BY ID DESC LIMIT 1", userName).Scan(&u.ID,
&u.UserName, &u.Email, &u.FirstName, &u.MiddleName, &u.LastName, &u.Suffix, &u.IsSuperUser,
&u.IsAdmin, &u.LastLogin, &u.DateJoined)
if err != nil {
itrlog.Error("error getting username: ", userName, ":", err)
return &User{}
}
return &u
}
Get the full source codes:
https://github.com/maharlikanscode/Go...