Golang HTTP User Authentication Yabi Series 6 | Golang Web Development | WebAssembly Auth System

Опубликовано: 29 Сентябрь 2024
на канале: Maharlikans Code
223
13

In this Golang Web Development Series #26, 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
#GolangWebDevelopment26
#GolangUserAuthenticationSystem
#MySQLDatabase
#YabiSeries6
#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/smtp.go:
package yabi

import (
"errors"
"strings"
"github.com/itrepablik/sulat"
)
...
var SGC = sulat.SGC{}
func SetSendGridAPI(s sulat.SGC) sulat.SGC {
if len(strings.TrimSpace(s.SendGridAPIKey)) == 0 {
return sulat.SGC{}
}
if len(strings.TrimSpace(s.SendGridEndPoint)) == 0 {
s.SendGridEndPoint = "/v3/mail/send"
}
if len(strings.TrimSpace(s.SendGridHost)) == 0 {
s.SendGridHost = "https://api.sendgrid.com"
}
SGC = sulat.SGC{
SendGridAPIKey: s.SendGridAPIKey,
SendGridEndPoint: s.SendGridEndPoint,
SendGridHost: s.SendGridHost,
}
return SGC
}
func init() {
SGC = SetSendGridAPI(SGC)
}
func SendEmailSendGrid(e EmailConfig) (bool, error) {
if len(strings.TrimSpace(e.From)) == 0 {
return false, errors.New("email from is missing")
}
if len(strings.TrimSpace(e.To)) == 0 {
return false, errors.New("email to is missing")
}
if len(strings.TrimSpace(e.DefaultTemplate)) == 0 {
return false, errors.New("email template is missing")
}
newEmailContent := ""
switch e.DefaultTemplate {
case EmailFormatNewUser:
newEmailContent = YabiHTMLHeader + NewUserActivation(e.EmailConfirmationURL, e.UserName,
e.SiteName, e.SiteSupportEmail) + YabiHTMLFooter
}
htmlContent, err := sulat.SetHTML(&sulat.EmailHTMLFormat{
IsFullHTML: false,
HTMLHeader: YabiHTMLHeader,
HTMLBody: newEmailContent,
HTMLFooter: YabiHTMLFooter,
})
mailOpt := &sulat.SendMail{
Subject: e.Subject,
From: sulat.NewEmail(e.FromAlias, e.From),
To: sulat.NewEmail(e.ToAlias, e.To),
CC: sulat.NewEmail(e.CCAlias, e.CC),
BCC: sulat.NewEmail(e.BCCAlias, e.BCC),
}
_, err = sulat.SendEmailSG(mailOpt, htmlContent, &SGC)
if err != nil {
return false, err
}
return true, nil
}
auth.go:
func CreateUser(dbCon *sql.DB, u User, e EmailConfig, confirmPassword string, tos bool) (int64, error) {
...
if !u.IsActive {
if len(strings.TrimSpace(e.From)) == 0 {
return 0, errors.New("From email address is required")
}
if len(strings.TrimSpace(e.To)) == 0 {
return 0, errors.New("To email address is required")
}
emailSubject := "Activate your new account"
if len(strings.TrimSpace(e.Subject)) gt 0 {
emailSubject = e.Subject
}
emailHTMLHeader := YabiHTMLHeader
if len(strings.TrimSpace(e.CustomizeHeaderTemplate)) gt 0 {
emailHTMLHeader = e.CustomizeHeaderTemplate
}
emailHTMLBody := NewUserActivation(e.EmailConfirmationURL, u.UserName, e.SiteName, e.SiteSupportEmail)
if len(strings.TrimSpace(e.CustomizeBodyTemplate)) gt 0 {
emailHTMLBody = e.CustomizeBodyTemplate
}
emailHTMLFooter := YabiHTMLFooter
if len(strings.TrimSpace(e.CustomizeFooterTemplate)) gt 0 {
emailHTMLFooter = e.CustomizeFooterTemplate
}
mailOpt := &sulat.SendMail{
Subject: emailSubject,
From: sulat.NewEmail(e.FromAlias, e.From),
To: sulat.NewEmail(e.ToAlias, e.To),
CC: sulat.NewEmail(e.CCAlias, e.CC),
BCC: sulat.NewEmail(e.BCCAlias, e.BCC),
}
htmlContent, err := sulat.SetHTML(&sulat.EmailHTMLFormat{
IsFullHTML: false,
HTMLHeader: emailHTMLHeader,
HTMLBody: emailHTMLBody,
HTMLFooter: emailHTMLFooter,
})
_, err = sulat.SendEmailSG(mailOpt, htmlContent, &SGC)
if err != nil {
itrlog.Error("SendGrid error: ", err)
}
}
hsPassword, err := sakto.HashAndSalt([]byte(u.Password))
if err != nil {
return 0, err
}
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
}
ins.Exec(u.UserName, hsPassword, u.Email, u.FirstName, u.MiddleName, u.LastName, u.Suffix, u.IsSuperUser,
u.IsAdmin, time.Now(), u.IsActive)
lid, err := GetLastInsertedID(dbCon, "id", YabiUser)
defer ins.Close()
return lid, nil
}

Get the full source codes:
https://github.com/maharlikanscode/Go...