In this Golang Web Development Series #27, 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
#GolangWebDevelopment27
#GolangUserAuthenticationSystem
#MySQLDatabase
#YabiSeries7
#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:
auth.go:
...
// AuthRouters are the collection of all URLs for the Auth App.
func AuthRouters(r *mux.Router) {
r.NotFoundHandler = http.HandlerFunc(PageNotFound)
r.HandleFunc("/api/v1/user/login", LoginUserEndpoint).Methods("POST")
r.HandleFunc("/api/v1/user/register", RegisterUserEndpoint).Methods("POST")
r.HandleFunc("/login", Login).Methods("GET")
r.HandleFunc("/register", Register).Methods("GET")
r.HandleFunc("/account_activation_sent", AccountActivationSent).Methods("GET")
//r.HandleFunc("/activation/{token}", ActivateAccount).Methods("GET")
}
...
// PageNotFound function is to render the 404 not found page.
func PageNotFound(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles(config.SiteRootTemplate+"front/404.html",
config.SiteHeaderTemplateCommon, config.SiteFooterAccountTemplateCommon))
data := contextData{
"PageTitle": "Page Not Found (404) | " + config.SiteShortName,
"PageMetaDesc": "Page Not Found error 404 " + config.SiteShortName,
"CanonicalURL": r.RequestURI,
"CsrfToken": csrf.Token(r),
"Settings": config.SiteSettings,
}
tmpl.Execute(w, data)
}
// AccountActivationSent function is to render the account activation sent page.
func AccountActivationSent(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles(config.SiteRootTemplate+"front/account_activation_sent.html",
config.SiteHeaderTemplateCommon, config.SiteFooterAccountTemplateCommon))
data := contextData{
"PageTitle": "Email Activation Sent | " + config.SiteShortName,
"PageMetaDesc": config.SiteShortName + " new user account activation sent",
"CanonicalURL": r.RequestURI,
"CsrfToken": csrf.Token(r),
"Settings": config.SiteSettings,
}
tmpl.Execute(w, data)
}
...
func RegisterUserEndpoint(w http.ResponseWriter, r *http.Request) {
...
rt := timaan.RandomToken()
emailConfirmPayload := timaan.TP{
"USERNAME": userName,
"EMAIL": email,
}
tok := timaan.TK{
TokenKey: rt,
Payload: emailConfirmPayload,
ExpireOn: time.Now().Add(time.Minute * 30).Unix(),
}
newToken, err := timaan.GenerateToken(rt, tok)
if err != nil {
itrlog.Error(err)
}
confirmURL := "http://127.0.0.1:8081/activate/" + newToken
fmt.Println(confirmURL)
newUserEmailConfirmation := yabi.EmailConfig{
From: config.SiteEmail,
FromAlias: "Support Team",
To: email,
Subject: "Activate your " + config.SiteShortName + " account",
DefaultTemplate: yabi.EmailFormatNewUser,
EmailConfirmationURL: confirmURL,
}
if isActive {
_, err := yabi.CreateUser(dbYabi, newUser, newUserEmailConfirmation, confirmPassword, tos)
if err != nil {
itrlog.Error(err)
w.Write([]byte(`{ "IsSuccess": "false", "AlertTitle": "New User Creation Failed!",
"AlertMsg": "` + err.Error() + `", "AlertType": "error" }`))
return
}
w.Write([]byte(`{ "IsSuccess": "true", "AlertTitle": "New User",
"AlertMsg": "You've successfully created a new ` + config.SiteShortName + `'s account.",
"AlertType": "success", "RedirectURL": "http://127.0.0.1:8081/account_activation_sent" }`))
} else {
_, err := yabi.CreateUser(dbYabi, newUser, newUserEmailConfirmation, confirmPassword, tos)
if err != nil {
itrlog.Error(err)
w.Write([]byte(`{ "IsSuccess": "false", "AlertTitle": "New User Creation Failed!",
"AlertMsg": "` + err.Error() + `", "AlertType": "error" }`))
return
}
w.Write([]byte(`{ "IsSuccess": "true", "AlertTitle": "Registration is Successful",
"AlertMsg": "You've successfully created your new ` + config.SiteShortName + `'s account, please ",
"AlertType": "success", "RedirectTo": "` + config.SiteBaseURL + `",
"RedirectURL": "http://127.0.0.1:8081/account_activation_sent" }`))
}
}
Get the full source codes:
https://github.com/maharlikanscode/Go...