Example #1
0
func (a *Application) login(c *echo.Context) error {
	id := c.Form("Id")
	user := a.Redis.GetUser(domain.ShaHashString(id))
	password := domain.HashPassword([]byte(c.Form("Password")), []byte(id))
	sessionKey := domain.HashPassword([]byte(id), []byte(user.CreateDate.String()))
	if user.Password == password {
		http.SetCookie(c.Response(), &http.Cookie{Name: "login", Value: sessionKey, MaxAge: 2592000})
		userAsJson, err := json.Marshal(user)
		if err != nil {
			fmt.Println(err)
		} else {
			a.Redis.Put(sessionKey, string(userAsJson))
			return c.Redirect(302, "/")
		}
	} else {
		fmt.Println("not a match")
	}

	return c.Redirect(302, "/login?failed=true")
}
Example #2
0
func (a *Application) createUser(c *echo.Context) error {
	userEmail := c.Form("Id")
	valid := domain.ValidateEmail(userEmail)
	if valid {
		role := domain.Role{Name: domain.Normal}

		if a.Redis.DbSize() == 0 {
			role = domain.Role{Name: domain.Admin}
		}

		hashedId := domain.ShaHashString(userEmail)
		user := &domain.User{
			Id:                      hashedId,
			Email:                   userEmail,
			Username:                c.Form("Username"),
			Password:                domain.HashPassword([]byte(c.Form("Password")), []byte(userEmail)),
			CreateDate:              time.Now().UTC(),
			EmailVerified:           false,
			EmailVerificationString: domain.HashPassword([]byte(userEmail), []byte(time.Now().String())),
			Role: role,
		}
		userJSON, err := json.Marshal(user)
		if err != nil {
			fmt.Println(err)
		} else {
			err := a.Redis.AddNewUser(user.Id, string(userJSON))
			if err == nil {
				email.SendVerificationEmail(user.Email,
					hashedId+"/"+user.EmailVerificationString,
					fromEmail,
					emailSendingPasswd)
				return c.Redirect(302, "/")
			}
		}
	} else {
		return c.Redirect(302, "/register?email=err")
	}
	return c.Redirect(302, "/?status=failed")
}
Example #3
0
func (a *Application) userAPI(c *echo.Context) error {
	return c.JSON(http.StatusOK, a.Redis.GetUser(domain.ShaHashString(c.P(0))))
}