2022-03-12 18:06:39 +00:00
|
|
|
// package controllers
|
2022-02-23 12:07:09 +00:00
|
|
|
|
2022-03-12 18:06:39 +00:00
|
|
|
// import (
|
|
|
|
// "errors"
|
|
|
|
// "net/http"
|
2022-02-23 12:07:09 +00:00
|
|
|
|
2022-03-12 18:06:39 +00:00
|
|
|
// "git.technical.kiwi/go/otfe/misc/cookie"
|
|
|
|
// "git.technical.kiwi/go/otfe/misc/helpers"
|
|
|
|
// "git.technical.kiwi/go/otfe/misc/rand"
|
|
|
|
// "git.technical.kiwi/go/otfe/models"
|
|
|
|
// )
|
2022-02-23 12:07:09 +00:00
|
|
|
|
2022-03-12 18:06:39 +00:00
|
|
|
// //Session controllers
|
|
|
|
// type Session struct{}
|
2022-02-23 12:07:09 +00:00
|
|
|
|
2022-03-12 18:06:39 +00:00
|
|
|
// type pageData struct {
|
|
|
|
// Title string
|
|
|
|
// Err string
|
|
|
|
// User models.User
|
|
|
|
// }
|
2022-02-23 12:07:09 +00:00
|
|
|
|
2022-03-12 18:06:39 +00:00
|
|
|
// //New login form
|
|
|
|
// func (s *Session) New(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// var err error
|
|
|
|
// data := pageData{Title: "Login"}
|
|
|
|
// data.Err, err = cookie.Read(r, "error")
|
|
|
|
// if err == nil {
|
|
|
|
// cookie.Delete(w, "error")
|
|
|
|
// }
|
|
|
|
// t(w, data, "/static/login.gtpl")
|
|
|
|
// }
|
2022-02-23 12:07:09 +00:00
|
|
|
|
2022-03-12 18:06:39 +00:00
|
|
|
// //Create a new session
|
|
|
|
// func (s *Session) Create(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// r.ParseForm()
|
|
|
|
// //Get email and password and check they are not empty
|
|
|
|
// email := r.Form.Get("email")
|
|
|
|
// password := r.Form.Get("password")
|
2022-02-23 12:07:09 +00:00
|
|
|
|
2022-03-12 18:06:39 +00:00
|
|
|
// //Check if user exists
|
|
|
|
// var user models.User
|
2022-02-23 12:07:09 +00:00
|
|
|
|
2022-03-12 18:06:39 +00:00
|
|
|
// //Check password is correct
|
|
|
|
// if user.Read("email", email) == nil &&
|
|
|
|
// helpers.CheckPasswordHash(password, user.Password) == nil {
|
|
|
|
// id, _ := rand.B64String(32)
|
|
|
|
// sess := models.Session{ID: id, UserID: user.ID}
|
|
|
|
// sess.Create()
|
|
|
|
// cookie.Create(w, "session", sess.ID)
|
|
|
|
// http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
// } else {
|
|
|
|
// loginFail(w, r, errors.New("Email or password incorrect"))
|
|
|
|
// }
|
|
|
|
// }
|
2022-02-23 12:07:09 +00:00
|
|
|
|
2022-03-12 18:06:39 +00:00
|
|
|
// //Delete session
|
|
|
|
// func (s *Session) Delete(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// id, err := cookie.Read(r, "session")
|
|
|
|
// //Check user is logged in
|
|
|
|
// if err == nil {
|
|
|
|
// cookie.Delete(w, "session")
|
|
|
|
// var session models.Session
|
|
|
|
// session.Delete(id)
|
|
|
|
// http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
// }
|
2022-02-23 12:07:09 +00:00
|
|
|
|
2022-03-12 18:06:39 +00:00
|
|
|
// }
|
2022-02-23 12:07:09 +00:00
|
|
|
|
2022-03-12 18:06:39 +00:00
|
|
|
// func loginFail(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
|
// cookie.Create(w, "error", err.Error())
|
|
|
|
// http.Redirect(w, r, "/login", http.StatusFound)
|
|
|
|
// }
|