I. Preface
Used gorm to manipulate the database, the backend is based on the gin framework, just a simple example of a backend implementation of registration and login interacting with the database.
II. Catalog structure
-templates
--
--
-
III. Codes
Click to view code
<!DOCTYPE html>
<html>
<head>
<title>enrollment</title>
</head>
<body>
<h1>用户enrollment</h1>
<form action="/register" method="POST">
<label for="username">user ID:</label>
<input type="text" name="username" required><br><br>
<label for="password">cryptographic:</label>
<input type="password" name="password" required><br><br>
<label for="email">inbox:</label>
<input type="text" name="email" required><br><br>
<label for="age">(a person's) age:</label>
<input type="text" name="age" required><br><br>
<button type="submit">enrollment</button>
</form>
<!-- Error message display -->
{{ if .error }}
<p style="color:red">{{ .error }}</p>
{{ end }}
</body>
</html>
Click to view code
<!DOCTYPE html>
<html>
<head>
<title>log in</title>
</head>
<body>
<h1>用户log in</h1>
<form action="/login" method="POST">
<label for="username">user ID:</label>
<input type="text" name="username" required><br><br>
<label for="password">cryptographic:</label>
<input type="password" name="password" required><br><br>
<button type="submit">log in</button>
</form>
<!-- Error message display -->
{{ if .error }}
<p style="color:red">{{ .error }}</p>
{{ end }}
<!-- Successful Information Display -->
{{ if .success }}
<p style="color:red">{{ .success }}</p>
{{ end }}
</body>
</html>
Click to view code
package main
import (
"log"
"net/http"
_ "/go-sql-driver/mysql"
"/gin-gonic/gin"
"/jinzhu/gorm"
)
var db *
var err error
// Define the data table
type User struct {
Username string `gorm: "unique" json: "username"`
Password string `json: "password"`
Age string `json: "age"`
Email string `json: "email"`
}
// Connect to the database, using gorm
func ConDB(){
dsn := "root:root@(127.0.0.1:3306)/mydb?charset=utf8mb4&parseTime=True&loc=Local"
db,err = ("mysql",dsn)
if err!=nil{
(err)
}
// Migrate the data table
(&User{})
}
func main() {
// Register the route
r := ()
// Connect to the database
ConDB()
//Load the template
("templates/*")
//Register route, realize login and register, get get page, post submit data
("/register",ShowRegisterPage)
("/register",Regist)
//Login
("/login",ShowLoginPage)
("/login",Login)
(":8080")
}
// register page
func ShowRegisterPage(c *){
(,"",nil)
}
//Registration submission
func Regist(c *) {
// Get the form data
username := ("username")
password := ("password")
age := ("age")
email := ("email")
//Query whether the user exists
user := new(User) //initialize user with default id=0
("username = ?" ,username).First(user)
(*user)
if ! =0 {
(,"",{"error": "User already exists"}))
return
} else {
newuser := User{
Username: username, {"error": "user already exists"}) return } else { newuser := User{
Password: password,
Age: age,
Password: password, Age: age, Email: email, }
}
(&newuser)
(,"",{"success": "Registration successful, please log in"})
}
}
//Login page
func ShowLoginPage(c *){
(,"",nil)
}
// Login Routing
func Login(c *){
// Get the data
username := ("username")
password := ("password")
//Query the user and verify the password
var loginuser User
(&loginuser, "username = ?" ,username)
if ==0 {
(,"",{"error": "User does not exist!"})
return
}else {
// ("first:-+++++>>>",loginuser)
// ("find::->>>>>",loginuser)
if password == {
(, "Login successful!")
} else {
(,"",{"error": "Wrong username or password"})
}
}
}