package controllers import ( "net/http" "git.aiterp.net/AiteRP/aitestory/view" "git.aiterp.net/AiteRP/aitestory/viewmodel" "git.aiterp.net/gisle/wrouter" "git.aiterp.net/gisle/wrouter/auth" ) // UserController serves and handles the login form var UserController = wrouter.Router{} func userLogin(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool { //var err error if (req.Method != "GET" && req.Method != "POST") || len(req.URL.Path) > len(path) { return false } if req.Method == "GET" && user != nil { http.Redirect(w, req, "/", 302) return true } ul := viewmodel.UserLogin{} if req.Method == "POST" { req.ParseForm() wa := WikiAthenticator{} newUser, err := wa.Login(req.Form.Get("username"), req.Form.Get("password")) if err == nil { sess := auth.OpenSession(newUser) http.SetCookie(w, &http.Cookie{Name: auth.SessionCookieName, Value: sess.ID, Expires: sess.Time.Add(auth.SessionMaxTime), Path: "/", HttpOnly: true}) http.Redirect(w, req, "/", 302) return true } ul.UserName = req.Form.Get("username") ul.Error = err.Error() } ul.Setup(user) view.Render(w, "user/login", 200, ul) return true } func userLogout(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool { //var err error if user != nil { auth.CloseSession(user.Session.ID) } http.Redirect(w, req, "/", 302) return true } func init() { UserController.Function("/login", userLogin) UserController.Function("/logout", userLogout) }