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

Опубликовано: 05 Октябрь 2024
на канале: Maharlikans Code
188
6

In this Golang Web Development Series #22, 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
#GolangWebDevelopment22
#GolangUserAuthenticationSystem
#MySQLDatabase
#YabiSeries2
#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/model_user.go:
package yabi
import "time"
...
auth.wasm:
func register(this js.Value, args []js.Value) interface{} {
jsDoc := js.Global().Get("document")
if !jsDoc.Truthy() {
return js.Global().Call("eval", `Swal.fire("Oops!, Error", "Unable to get document objects", "error");`)
}
username := jsDoc.Call("getElementById", "username")
if !username.Truthy() {
return js.Global().Call("eval", `Swal.fire("Username is Required!", "Unable to get username object", "error");`)
}
email := jsDoc.Call("getElementById", "email")
if !email.Truthy() {
return js.Global().Call("eval", `Swal.fire("Email is Required!", "Unable to get email object", "error");`)
}
password := jsDoc.Call("getElementById", "password")
if !password.Truthy() {
return js.Global().Call("eval", `Swal.fire("Password is Required!", "Unable to get password object", "error");`)
}
confirmPassword := jsDoc.Call("getElementById", "confirmPassword")
if !confirmPassword.Truthy() {
return js.Global().Call("eval", `Swal.fire("Confirm Password is Required!", "Unable to get confirm password object", "error");`)
}
chkTOS := jsDoc.Call("getElementById", "chkTOS")
if !chkTOS.Truthy() {
return js.Global().Call("eval", `Swal.fire("Oops!, Error", "Unable to get terms of service object", "error");`)
}
csrfToken := jsDoc.Call("getElementById", "csrfToken")
if !csrfToken.Truthy() {
return js.Global().Call("eval", `Swal.fire("Oops!", "Unable to get the CSRF token", "error");`)
}
var pCSRFToken string = csrfToken.Get("value").String()
var pUserName string = username.Get("value").String()
var pEmail string = email.Get("value").String()
var pPassword string = password.Get("value").String()
var pConfirmPassword string = confirmPassword.Get("value").String()
var pTOS bool = chkTOS.Get("checked").Bool()
payLoad := map[string]interface{}{
"username": pUserName,
"email": pEmail,
"password": pPassword,
"confirmPassword": pConfirmPassword,
"tos": fmt.Sprint(pTOS),
"isActive": "false",
}
bytesRepresentation, err := json.Marshal(payLoad)
if err != nil {
return js.Global().Call("eval", `Swal.fire("Oops! Error", "Something went wrong with your user's registration", "error");`)
}
siteHost := "http://127.0.0.1:8081/api/v1/user/register"
client := &http.Client{}
req, err := http.NewRequest("POST", fmt.Sprintf(siteHost), bytes.NewBuffer(bytesRepresentation))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-CSRF-TOKEN", pCSRFToken)
if err != nil {
return js.Global().Call("eval", `Swal.fire("Oops! Error", "Something went wrong with your connection, please try again", "error");`)
}
var isSuccess bool = false
c1 := make(chan map[string]interface{}, 1)
var result2 map[string]interface{}
go func() {
resp, _ := client.Do(req)
defer resp.Body.Close()
json.NewDecoder(resp.Body).Decode(&result2)
c1 lt- result2
}()
var result map[string]interface{}
go func() interface{} {
for {
timeout := make(chan bool, 1)
go func() {
time.Sleep(time.Second * 1)
timeout lt- true
}()
select {
case result = lt-c1:
i := result["IsSuccess"]
mStatus := fmt.Sprint(i)
isSuccess, _ = strconv.ParseBool(mStatus)
alertTitle := fmt.Sprint(result["AlertTitle"])
alertMsg := fmt.Sprint(result["AlertMsg"])
alertType := fmt.Sprint(result["AlertType"])
msg := ""
if !isSuccess {
msg = `Swal.fire("` + alertTitle + `", "` + alertMsg + `", "` + alertType + `");`
} else {
msg = `Swal.fire("` + alertTitle + `", "` + alertMsg + `", "` + alertType + `");`
}
return APIResponse(isSuccess, msg)
break
case lt-timeout:
}
}
}()
return nil
}

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