diff --git a/Lab01/app/database/database.go b/Lab01/app/database/database.go index bebee4c..d1a18a0 100644 --- a/Lab01/app/database/database.go +++ b/Lab01/app/database/database.go @@ -30,14 +30,31 @@ func (db *Database) CreateUser(username string, password string) error { return result.Error } -func (db *Database) LoginUser(username string, password string) (bool, error) { +func (db *Database) LoginUser(username string, password string) bool { user := User{Username: username, Password: password} result := db.ORM.Where("username = ? AND password = ?", username, password).Find(&user) - if result.Error != nil { - return false, result.Error - } if result.RowsAffected == 1 { - return true, nil + db.ORM.Model(&user).Update("logged_in", true) + return true } - return false, nil + return false +} + +func (db *Database) LogoutUser(username string) bool { + user := User{Username: username} + result := db.ORM.Where("username = ? and logged_in = ?", username, true).Find(&user) + if result.RowsAffected == 1 { + db.ORM.Model(&user).Update("logged_in", false) + return true + } + return false +} + +func (db *Database) UserIsLoggedIn(username string) bool { + user := User{Username: username} + result := db.ORM.Where("username = ? and logged_in = ?", username, true).Find(&user) + if result.RowsAffected == 1 { + return true + } + return false } diff --git a/Lab01/app/database/types.go b/Lab01/app/database/types.go index d4e11f0..e1b2d2e 100644 --- a/Lab01/app/database/types.go +++ b/Lab01/app/database/types.go @@ -10,7 +10,7 @@ type Database struct { } type User struct { - Username string `gorm:"primaryKey"` - Password string - CreatedAt int64 `gorm:"autoCreateTime"` + Username string `gorm:"primaryKey"` + Password string + LoggedIn bool } diff --git a/Lab01/app/templates/index.tmpl b/Lab01/app/templates/_base.tmpl similarity index 56% rename from Lab01/app/templates/index.tmpl rename to Lab01/app/templates/_base.tmpl index 27f45d6..894ebe0 100644 --- a/Lab01/app/templates/index.tmpl +++ b/Lab01/app/templates/_base.tmpl @@ -1,27 +1,3 @@ - - - - - {{ .title }} - {{template "head" .}} - - -{{template "navbar" .}} - -
- {{if .loggedIn}} -
Welcome to the {{ .title }}.
- {{else}} -
- Login to continue. -
- {{end}} -
- -{{template "scripts" .}} - - - {{define "head"}} @@ -37,7 +13,7 @@ {{define "navbar"}}