Пример #1
0
// POST /signin route, authenticates user
func SignInPost(c *gin.Context) {
	session := sessions.Default(c)
	user := &models.User{}
	if err := c.Bind(user); err == nil {
		userDB, _ := models.GetUserByEmail(user.Email)
		if userDB.Id == 0 {
			logrus.Errorf("Login error, IP: %s, Email: %s", c.ClientIP(), user.Email)
			session.AddFlash("Email or password incorrect")
			session.Save()
			c.Redirect(http.StatusFound, "/signin")
			return
		}
		if err := bcrypt.CompareHashAndPassword([]byte(userDB.Password), []byte(user.Password)); err != nil {
			logrus.Errorf("Login error, IP: %s, Email: %s", c.ClientIP(), user.Email)
			session.AddFlash("Email or password incorrect")
			session.Save()
			c.Redirect(http.StatusFound, "/signin")
			return
		}

		session.Set("UserId", userDB.Id)
		session.Save()
		c.Redirect(http.StatusFound, "/")
		return

	} else {
		session.AddFlash("Please, fill out form correctly.")
		session.Save()
		c.Redirect(http.StatusFound, "/signin")
		return
	}
}
Пример #2
0
// POST /signup route, creates new user
func SignUpPost(c *gin.Context) {
	session := sessions.Default(c)
	user := &models.User{}
	if err := c.Bind(user); err == nil {
		userDB, _ := models.GetUserByEmail(user.Email)
		if userDB.Id != 0 {
			session.AddFlash("User exists")
			session.Save()
			c.Redirect(http.StatusFound, "/signup")
			return
		}
		//create user
		err := user.HashPassword()
		if err != nil {
			session.AddFlash("Error whilst registering user.")
			session.Save()
			logrus.Errorf("Error whilst registering user: %v", err)
			c.Redirect(http.StatusFound, "/signup")
			return
		}

		if err := user.Insert(); err != nil {
			session.AddFlash("Error whilst registering user.")
			session.Save()
			logrus.Errorf("Error whilst registering user: %v", err)
			c.Redirect(http.StatusFound, "/signup")
			return
		}

		session.Set("UserId", user.Id)
		session.Save()
		c.Redirect(http.StatusFound, "/")
		return

	} else {
		session.AddFlash(err.Error())
		session.Save()
		c.Redirect(http.StatusFound, "/signup")
		return
	}
}