Commit e7ce371e by Torkel Ödegaard

working on login && auth

parent 56d449b2
......@@ -55,9 +55,8 @@ func (self *HttpServer) ListenAndServe() {
}
// register default route
self.router.GET("/", self.AuthMiddleware(), self.index)
self.router.GET("/login/*_", self.index)
self.router.GET("/dashboard/*_", self.index)
self.router.GET("/", self.authMiddleware(), self.index)
self.router.GET("/dashboard/*_", self.authMiddleware(), self.index)
self.router.Run(":" + self.port)
}
......@@ -66,22 +65,6 @@ func (self *HttpServer) index(c *gin.Context) {
c.HTML(200, "index.html", &indexViewModel{title: "hello from go"})
}
func (self *HttpServer) login(c *gin.Context) {
c.HTML(200, "login.html", &indexViewModel{title: "hello from go"})
}
func (self *HttpServer) AuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
session, _ := sessionStore.Get(c.Request, "grafana-session")
// if session.Values["login"] == nil {
// c.Writer.Header().Set("Location", "/login/login#login")
// c.Abort(302)
// }
session.Values["asd"] = 1
session.Save(c.Request, c.Writer)
}
}
func CacheHeadersMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Add("Cache-Control", "max-age=0, public, must-revalidate, proxy-revalidate")
......
......@@ -41,7 +41,7 @@ func (self *HttpServer) postDashboard(c *gin.Context) {
var command saveDashboardCommand
if c.EnsureBody(&command) {
err := self.store.Save(&models.Dashboard{Data: command.dashboard})
err := self.store.Save(&models.Dashboard{Data: command.Dashboard})
if err == nil {
c.JSON(200, gin.H{"status": "saved"})
return
......
package api
import "github.com/gin-gonic/gin"
func init() {
addRoutes(func(self *HttpServer) {
self.router.GET("/login/*_", self.index)
self.router.POST("/login", self.loginPost)
})
}
type loginJsonModel struct {
Email string `json:"email" binding:"required"`
Password string `json:"password" binding:"required"`
Remember bool `json:"remember"`
}
func (self *HttpServer) loginPost(c *gin.Context) {
var loginModel loginJsonModel
if c.EnsureBody(&loginModel) {
if loginModel.Email == "manu" && loginModel.Password == "123" {
session, _ := sessionStore.Get(c.Request, "grafana-session")
session.Values["login"] = true
session.Save(c.Request, c.Writer)
c.JSON(200, gin.H{"status": "you are logged in"})
} else {
c.JSON(401, gin.H{"status": "unauthorized"})
}
}
}
func (self *HttpServer) authMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
session, _ := sessionStore.Get(c.Request, "grafana-session")
if c.Request.URL.Path != "/login" && session.Values["login"] == nil {
c.Writer.Header().Set("Location", "/login")
c.Abort(302)
}
session.Save(c.Request, c.Writer)
}
}
package api
type saveDashboardCommand struct {
id string `json:"id"`
title string `json:"title"`
dashboard map[string]interface{}
Id string `json:"id"`
Title string `json:"title"`
Dashboard map[string]interface{}
}
type errorResponse struct {
message string `json:"message"`
Message string `json:"message"`
}
type indexViewModel struct {
......@@ -15,5 +15,5 @@ type indexViewModel struct {
}
func newErrorResponse(message string) *errorResponse {
return &errorResponse{message: message}
return &errorResponse{Message: message}
}
Subproject commit c690d4aae84fc39d7c4bf9a0a820a71f69059b78
Subproject commit bfe1ef07330fcfcdbc3aa9ddd4eef827ee48f316
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment