Пример #1
0
func LoginPost() func(c *gin.Context) {
	return func(c *gin.Context) {
		username := c.PostForm("username")
		password := c.PostForm("password")
		redirect_to := c.DefaultQuery("redirect_to", "/index")
		if common.IsIn(redirect_to, settings.ALLOWED_REDIRECTS) == false {
			redirect_to = "/index"
		}
		w, err := webclient.Init(username, password)
		if err != nil {
			c.Redirect(302, settings.SERVER_URL+"/auth/login"+"?redirect_to="+redirect_to)
		} else {
			user := userstorage.FindWrapper(w.UserId, w.TokenId)
			if user == nil {
				userstorage.AddWrapper(w)
			}
			cookie_userid := &http.Cookie{Name: settings.USERID_COOKIE_FIELD_NAME, Value: w.UserId, Path: "/", Domain: settings.SERVER_ADDR}
			cookie_token := &http.Cookie{Name: settings.TOKEN_COOKIE_FIELD_NAME, Value: w.TokenId, Path: "/", Domain: settings.SERVER_ADDR}
			http.SetCookie(c.Writer, cookie_userid)
			http.SetCookie(c.Writer, cookie_token)

			c.Redirect(302, settings.SERVER_URL+redirect_to)
		}
	}
}
Пример #2
0
func Login() func(c *gin.Context) {
	template_name := "login.html"
	server_addr := settings.SERVER_ADDR
	server_proto := settings.SERVER_PROTO
	server_port := settings.SERVER_PORT
	post_url := server_proto + "://" + server_addr + ":" + server_port + "/auth/login"
	data := gin.H{"post_url": post_url, "static_url": settings.STATIC_URL}
	return func(c *gin.Context) {
		redirect_to := c.DefaultQuery("redirect_to", "/index")
		if common.IsIn(redirect_to, settings.ALLOWED_REDIRECTS) == false {
			redirect_to = "/index"
		}
		if auth.IsAuthorized(c) == true {
			c.Redirect(302, redirect_to)
		} else {
			data["post_url"] = post_url + "?redirect_to=" + redirect_to
			c.HTML(200, template_name, data)
		}
	}
}