Exemplo n.º 1
0
func (ctx *LoginController) Login() {

	if ctx.Ctx.Input.Method() == "POST" {
		login := models.User{}

		if err := ctx.ParseForm(&login); err != nil {
			error := map[string]string{"status": "ERROR", "errorMsg": "An error occured"}
			ctx.Data["json"] = &error
			ctx.ServeJson()
		} else {
			error := login.GetOne("Email")
			if error == nil {
				if login.Password != security.Sha512(ctx.GetString("password")) {
					error := map[string]string{"status": "ERROR", "errorMsg": "Invalid username or password"}
					ctx.Data["json"] = &error
					ctx.ServeJson()
				} else {
					success := map[string]string{"status": "SUCCESS", "email": login.Email, "userid": strconv.FormatInt(login.Id, 10), "name": login.Name}
					ctx.Data["json"] = &success
					ctx.ServeJson()
				}
			} else {
				error := map[string]string{"status": "ERROR", "errorMsg": "Invalid username or password"}
				ctx.Data["json"] = &error
				ctx.ServeJson()
			}
		}
	}
}
Exemplo n.º 2
0
func (u *User) Save() (int64, error) {
	o := orm.NewOrm()
	o.Using("default")

	u.Password = security.Sha512(u.Password)

	id, err := o.Insert(u)
	if err != nil {
		return 0, errors.New(fmt.Sprintf("Couldn't insert new row. Reason: ", err))
	}
	return id, nil
}