func (VS *Server) login(c *gin.Context) {
	username := c.Request.FormValue("username")
	password := c.Request.FormValue("password")
	workerID := c.Request.FormValue("workerID")

	// Check that the Village introduced in the command line exists
	// if !VS.existLocalVillage(c, "h_general", "login", "VS.existLocalVillage") {
	if !VS.existLocalVillage(c, "h_general", "login", "VS.existLocalVillage") {
		// Check that if is not Central, and if the DB is empty (localvillage does not exist), then we redirect to import database for start
		if localVillage != "Central" {
			c.Redirect(http.StatusFound, "/settings/new-import/")
		} else {
			VS.getLocalVillage(c, "h_general", "login", "VS.getLocalVillage")
		}
		return
	}

	var user User
	exist := true
	if workerID != "" {
		id := bson.ObjectIdHex(workerID)
		user = VS.getUserByID(c, id, "h_general", "login", "user = v.getUserByID")
	} else {
		user, exist = VS.getUserByUsername(c, username, "h_general", "login", "user, exist = VS.getUserByUsername")
		// Every time the manager logs in we put his ID in the global variable
		if user.Role == Manager {
			if ManagerID != user.ID {
				DateLastManagerChange = getTime()
				ManagerID = user.ID
			}
		}
		if !exist {
			render(c, gin.H{}, "wrong-password.html")
			return
		}
	}

	if user.Role != Worker {
		ManagerID = user.ID
	}

	if user.Password == "" {
		setSession(c, user.Username, string(user.Role))
		if user.Role == Worker {
			fmt.Println("Redirecting to /set-password-worker")
			c.Redirect(http.StatusFound, "/settings/set-password-worker")
		} else {
			fmt.Println("Redirecting to /set-password")
			c.Redirect(http.StatusFound, "/settings/set-password")
		}
		return
	}

	// Check if the username/password combination is valid
	err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
	if err == nil {
		// If the username/password is valid set the session cookie
		setSession(c, user.Username, string(user.Role))
		fmt.Println("Redirecting to /dashboard from login")
		c.Redirect(http.StatusFound, "/dashboard")
	} else {
		// Clear the cookie just in case
		// BUG
		// c.SetCookie("valuevillages", "", -1, "", "", http.SameSiteDefaultMode, false, true)
		c.Set("isLoggedIn", false)
		render(c, gin.H{}, "wrong-password.html")
	}

}
// This handler checks if the user and password is correct and sets the cookie for the user
This handler checks if the user and password is correct and sets the cookie for the user
// This handler checks if the user and password is correct and sets the cookie for the user
func (VS *Server) login(c *gin.Context) {
	username := c.Request.FormValue("username")
	password := c.Request.FormValue("password")
	workerID := c.Request.FormValue("workerID")
	if !VS.existLocalVillage(c, "h_general", "login", "VS.existLocalVillage") {
		if localVillage != "Central" {
			c.Redirect(http.StatusFound, "/settings/new-import/")
		} else {
			VS.getLocalVillage(c, "h_general", "login", "VS.getLocalVillage")
		}
		return
	}
	var user User
	exist := true
	if workerID != "" {
		id := bson.ObjectIdHex(workerID)
		user = VS.getUserByID(c, id, "h_general", "login", "user = v.getUserByID")
	} else {
		user, exist = VS.getUserByUsername(c, username, "h_general", "login", "user, exist = VS.getUserByUsername")
		if user.Role == Manager {
			if ManagerID != user.ID {
				DateLastManagerChange = getTime()
				ManagerID = user.ID
			}
		}
		if !exist {
			render(c, gin.H{}, "wrong-password.html")
			return
		}
	}
	if user.Role != Worker {
		ManagerID = user.ID
	}
	if user.Password == "" {
		setSession(c, user.Username, string(user.Role))
		if user.Role == Worker {
			fmt.Println("Redirecting to /set-password-worker")
			c.Redirect(http.StatusFound, "/settings/set-password-worker")
		} else {
			fmt.Println("Redirecting to /set-password")
			c.Redirect(http.StatusFound, "/settings/set-password")
		}
		return
	}
	err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
	if err == nil {
		setSession(c, user.Username, string(user.Role))
		fmt.Println("Redirecting to /dashboard from login")
		c.Redirect(http.StatusFound, "/dashboard")
	} else {
		c.Set("isLoggedIn", false)
		render(c, gin.H{}, "wrong-password.html")
	}
}
func ( VS * Server ) login ( c * gin . Context ) { username := c . Request . FormValue ( " " ) password := c . Request . FormValue ( " " ) workerID := c . Request . FormValue ( " " ) if ! VS . existLocalVillage ( c , " " , " " , " " ) { if localVillage != " " { c . Redirect ( http . StatusFound , " " ) } else { VS . getLocalVillage ( c , " " , " " , " " ) } return } var user User exist := true if workerID != " " { id := bson . ObjectIdHex ( workerID ) user = VS . getUserByID ( c , id , " " , " " , " " ) } else { user , exist = VS . getUserByUsername ( c , username , " " , " " , " " ) if user . Role == Manager { if ManagerID != user . ID { DateLastManagerChange = getTime ( ) ManagerID = user . ID } } if ! exist { render ( c , gin . H { } , " " ) return } } if user . Role != Worker { ManagerID = user . ID } if user . Password == " " { setSession ( c , user . Username , string ( user . Role ) ) if user . Role == Worker { fmt . Println ( " " ) c . Redirect ( http . StatusFound , " " ) } else { fmt . Println ( " " ) c . Redirect ( http . StatusFound , " " ) } return } err := bcrypt . CompareHashAndPassword ( [ ] byte ( user . Password ) , [ ] byte ( password ) ) if err == nil { setSession ( c , user . Username , string ( user . Role ) ) fmt . Println ( " " ) c . Redirect ( http . StatusFound , " " ) } else { c . Set ( " " , false ) render ( c , gin . H { } , " " ) } }
This handler checks if the user and password is correct and sets the cookie for the user
This handler checks if the user and password is correct and sets the cookie for the user
// Check that the Village introduced in the command line exists // if !VS.existLocalVillage(c, "h_general", "login", "VS.existLocalVillage") { // Check that if is not Central, and if the DB is empty (localvillage does not exist), then we redirect to import database for start // Every time the manager logs in we put his ID in the global variable // Check if the username/password combination is valid // If the username/password is valid set the session cookie // Clear the cookie just in case // BUG // c.SetCookie("valuevillages", "", -1, "", "", http.SameSiteDefaultMode, false, true)