我是Golang的新手,我正在从Node.js服务器切换到Golang服务器,我正在尝试重写我之前为Node编写的应用程序。
我想在用户登录时设置模板变量,但我不确定如何,是的,我已经尝试过谷歌搜索它。
这是我的注册用户控制器:
func (c User) RegisterUser(user_email, user_password,
user_password_confirmation, user_first_name,
user_last_name string) revel.Result {
// Validate Email
c.Validation.Required(user_email).Message("Username is required")
c.Validation.Email(user_email).Message("Email is not a valid email")
c.Validation.MinSize(user_email, 5).Message("Email must be greater than 5 characters")
// Validate Password
c.Validation.Required(user_password).Message("Password is required")
c.Validation.MinSize(user_password, 5).Message("Password must be greater than 5 characters.")
// Validate Password Confirmation
c.Validation.Required(user_password_confirmation).Message("Password Confirmation is required")
c.Validation.MinSize(user_password_confirmation, 5).Message("Password must be greater than 5 characters.")
c.Validation.Required(user_password == user_password_confirmation).Message("Your passwords do not match")
// Validate First Name
c.Validation.Required(user_first_name).Message("First Name is required")
c.Validation.MinSize(user_first_name, 3).Message("Your First Name must be greater than 3 characters")
// Validate Last Name
c.Validation.Required(user_last_name).Message("Last Name is required")
c.Validation.MinSize(user_last_name, 3).Message("Your Last Name must be greater than 3 characters")
// If anything wasn't right, set flash and display errors to user
if c.Validation.HasErrors() {
c.Validation.Keep()
c.FlashParams()
return c.Redirect(User.Register)
}
// Hash The Users Password
password := []byte(user_password_confirmation)
hashedPassword, err := bcrypt.GenerateFromPassword(password, 10)
if err != nil {
c.Flash.Error("Invalid Username or Password Combination")
return c.Redirect(User.Register)
}
// Connect to database
db, err := sql.Open("mysql", "root:******@/WebRTC")
if err != nil {
c.Flash.Error("Unable to connect to database. Try again later!")
return c.Redirect(User.Register)
}
defer db.Close()
// Ping the database, ensure there is a connection
err = db.Ping()
if err != nil {
c.Flash.Error("Unable to connect to database. Try again later!")
return c.Redirect(User.Register)
}
// Prepare SQL Statement for Security
stmtIns, err := db.Prepare("INSERT INTO users (user_email, user_first_name, user_last_name, " +
"user_password_hash, user_created_at, user_created_ip, user_last_ip) VALUES (?, ?, ?, ?, ?, ?, ?)")
if err != nil {
c.Flash.Error("Unable to Register you. Please try again later")
return c.Redirect(User.Register)
}
defer stmtIns.Close()
// Insert Data into Database
_, err = stmtIns.Exec(user_email, user_first_name, user_last_name, hashedPassword,
time.Now().Local(), c.Request.RemoteAddr, c.Request.RemoteAddr)
if err != nil {
c.Flash.Error("Unable to register you. Please try again later")
return c.Redirect(User.Register)
}
// Here I want to add the global template variable
return c.Redirect(User.Register)
}
我已经读过c.RenderArgs
,但它似乎没有做我想要的。
我希望能够设置用户的用户名,以便我可以在导航栏中显示它,以便他们知道他们已登录。
使用c.RenderArgs
,你就走在正确的轨道上,除非我误解了你的意图。
下面是如何使用它的示例(或者更确切地说,演练)(取自 Revel 的预订示例应用程序):
init.go:注册一个拦截器以在采取行动之前添加用户的信息(
AddUser
将在Render
之前触发)。app.go:检查用户是否已连接,如果是,则将用户数据存储在
RenderArgs
图中,如下所示:
c.RenderArgs["user"] = user
第 31 行处理模型。
标头.html:传入用户名模板变量。
我希望这有所帮助。
编辑:我可能应该补充一点,用户的用户名在登录时存储在会话中。它用于检索更多信息。