Move login-code to http
This commit is contained in:
@ -13,40 +13,4 @@ func main() {
|
|||||||
|
|
||||||
h := &http.Handler{UserService=us}
|
h := &http.Handler{UserService=us}
|
||||||
h.Serve()
|
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")
|
|
||||||
}
|
|
44
http/http.go
44
http/http.go
@ -48,3 +48,47 @@ func (h *Handler) Serve() {
|
|||||||
|
|
||||||
router.Run(":1323")
|
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")
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user