Skip to content
Permalink
Browse files

06-User-Login add session

  • Loading branch information...
bonfy committed Sep 12, 2018
1 parent d966fd8 commit 6f54950b8a6faa86a648f05978e3eab6877343ee
Showing with 70 additions and 10 deletions.
  1. +9 −1 controller/g.go
  2. +8 −8 controller/home.go
  3. +51 −0 controller/utils.go
  4. +2 −1 main.go
@@ -1,14 +1,22 @@
package controller

import "html/template"
import (
"html/template"

"github.com/gorilla/sessions"
)

var (
homeController home
templates map[string]*template.Template
sessionName string
store *sessions.CookieStore
)

func init() {
templates = PopulateTemplates()
store = sessions.NewCookieStore([]byte("something-very-secret"))
sessionName = "go-mega"
}

// Startup func
@@ -11,6 +11,7 @@ type home struct{}
func (h home) registerRoutes() {
http.HandleFunc("/", indexHandler)
http.HandleFunc("/login", loginHandler)
http.HandleFunc("/logout", logoutHandler)
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
@@ -20,13 +21,6 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
templates[tpName].Execute(w, &v)
}

func check(username, password string) bool {
if username == "bonfy" && password == "abc123" {
return true
}
return false
}

func loginHandler(w http.ResponseWriter, r *http.Request) {
tpName := "login.html"
vop := vm.LoginViewModelOp{}
@@ -48,14 +42,20 @@ func loginHandler(w http.ResponseWriter, r *http.Request) {
v.AddError("password must longer than 6")
}

if !check(username, password) {
if !vm.CheckLogin(username, password) {
v.AddError("username password not correct, please input again")
}

if len(v.Errs) > 0 {
templates[tpName].Execute(w, &v)
} else {
setSessionUser(w, r, username)
http.Redirect(w, r, "/", http.StatusSeeOther)
}
}
}

func logoutHandler(w http.ResponseWriter, r *http.Request) {
clearSession(w, r)
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
}
@@ -1,8 +1,11 @@
package controller

import (
"errors"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"os"
)

@@ -40,3 +43,51 @@ func PopulateTemplates() map[string]*template.Template {
}
return result
}

// session

func getSessionUser(r *http.Request) (string, error) {
var username string
session, err := store.Get(r, sessionName)
if err != nil {
return "", err
}

val := session.Values["user"]
fmt.Println("val:", val)
username, ok := val.(string)
if !ok {
return "", errors.New("can not get session user")
}
fmt.Println("username:", username)
return username, nil
}

func setSessionUser(w http.ResponseWriter, r *http.Request, username string) error {
session, err := store.Get(r, sessionName)
if err != nil {
return err
}
session.Values["user"] = username
err = session.Save(r, w)
if err != nil {
return err
}
return nil
}

func clearSession(w http.ResponseWriter, r *http.Request) error {
session, err := store.Get(r, sessionName)
if err != nil {
return err
}

session.Options.MaxAge = -1

err = session.Save(r, w)
if err != nil {
return err
}

return nil
}
@@ -5,6 +5,7 @@ import (

"github.com/bonfy/go-mega-code/controller"
"github.com/bonfy/go-mega-code/model"
"github.com/gorilla/context"
_ "github.com/jinzhu/gorm/dialects/mysql"
)

@@ -17,5 +18,5 @@ func main() {
// Setup Controller
controller.Startup()

http.ListenAndServe(":8888", nil)
http.ListenAndServe(":8888", context.ClearHandler(http.DefaultServeMux))
}

0 comments on commit 6f54950

Please sign in to comment.
You can’t perform that action at this time.