Move login-code to http

This commit is contained in:
Jan Bader 2016-12-19 22:47:45 +01:00
parent 227028f99d
commit 8231b3d176
2 changed files with 45 additions and 37 deletions

View File

@ -13,40 +13,4 @@ func main() {
h := &http.Handler{UserService=us}
h.Serve()
}
func restricted(c *gin.Context) {
claims, ok := verifyLogin(c)
if !ok {
c.Redirect(http.StatusTemporaryRedirect, "/login")
return
}
name := claims["name"].(string)
c.String(http.StatusOK, "Welcome "+name+"!")
}
func login(c *gin.Context) {
if _, ok := verifyLogin(c); ok {
c.Redirect(http.StatusTemporaryRedirect, "/api/v1/hello")
return
}
c.HTML(http.StatusOK, "login", nil)
}
func logout(c *gin.Context) {
clearLogin(c)
}
func loginPost(c *gin.Context) {
username, _ := c.GetPostForm("username")
password, _ := c.GetPostForm("password")
if username != "jan" || password != "passwort" {
c.AbortWithStatus(http.StatusUnauthorized)
return
}
loginSuccess(c, username, "Jan Bader")
}
}

View File

@ -48,3 +48,47 @@ func (h *Handler) Serve() {
router.Run(":1323")
}
func restricted(c *gin.Context) {
claims, ok := verifyLogin(c)
if !ok {
c.Redirect(http.StatusTemporaryRedirect, "/login")
return
}
name := claims["name"].(string)
c.String(http.StatusOK, "Welcome "+name+"!")
}
func verifyLogin(c *gin.Context) error {
tokenString, err := c.Cookie(authCookie)
if err != nil {
return nil, err
}
}
func login(c *gin.Context) {
if _, ok := verifyLogin(c); ok {
c.Redirect(http.StatusTemporaryRedirect, "/api/v1/hello")
return
}
c.HTML(http.StatusOK, "login", nil)
}
func logout(c *gin.Context) {
clearLogin(c)
}
func loginPost(c *gin.Context) {
username, _ := c.GetPostForm("username")
password, _ := c.GetPostForm("password")
if username != "jan" || password != "passwort" {
c.AbortWithStatus(http.StatusUnauthorized)
return
}
loginSuccess(c, username, "Jan Bader")
}