Example #1
0
// user auth by token
//
//  in  : string
//  out : {
//          "user":*User,
//          "token":*Token,
//        }
//
func (_ *UserApi) Auth(v interface{}) *Res {
	token, ok := v.(string)
	if !ok {
		return Fail(paramTypeError(""))
	}
	// get token
	t, err := model.GetValidToken(token)
	if err != nil {
		return Fail(err)
	}
	if t == nil {
		return Fail(ERR_USER_MISSING)
	}
	// get user
	user, err := model.GetUserBy("id", t.UserId)
	if err != nil {
		return Fail(err)
	}
	if user == nil {
		return Fail(ERR_USER_MISSING)
	}

	// return data
	return Success(map[string]interface{}{
		"token": token,
		"user":  user,
	})
}
Example #2
0
// update user profile
//
//  in  : *UserProfileForm
//  out : {
//          "user":*User,
//        }
//
func (_ *UserApi) UpdateProfile(v interface{}) *Res {
	form, ok := v.(*UserProfileForm)
	if !ok {
		return Fail(paramTypeError(new(UserProfileForm)))
	}
	u := &model.User{
		Name:    form.User,
		Nick:    form.Nick,
		Email:   form.Email,
		Url:     form.Url,
		Profile: form.Profile,
		Id:      form.Id,
	}
	u.AvatarUrl = utils.GravatarLink(u.Email)
	if u.Url == "" {
		u.Url = "#"
	}

	u2, err := model.GetUserBy("email", u.Email)
	if err != nil {
		return Fail(err)
	}
	if u2 != nil && u2.Id != u.Id {
		return Fail(ERR_USER_EMAIL)
	}

	if err := model.UpdateUser(u); err != nil {
		return Fail(err)
	}
	u, err = model.GetUserBy("id", u.Id)
	if err != nil {
		return Fail(err)
	}
	return Success(map[string]interface{}{
		"user": u,
	})
}
Example #3
0
// user login
//
//  in  : *UserLoginForm
//  out : {
//          "user":*User,
//          "token":*Token,
//        }
//
func (_ *UserApi) Login(v interface{}) *Res {
	form, ok := v.(*UserLoginForm)
	if !ok {
		return Fail(paramTypeError(new(UserLoginForm)))
	}
	// get user
	user, err := model.GetUserBy("name", form.Name)
	if err != nil {
		return Fail(err)
	}
	if user == nil {
		return Fail(ERR_USER_MISSING)
	}

	// check password
	if !user.CheckPassword(form.Password) {
		return Fail(ERR_USER_WRONG_PASSWORD)
	}

	// create token
	if form.Remember == 0 {
		form.Remember = 3600
	} else {
		form.Remember = form.Remember * 3600 * 24
	}
	token, err := model.CreateToken(user.Id, form.Remember)
	if err != nil {
		return Fail(err)
	}

	// return data
	return Success(map[string]interface{}{
		"token": token,
		"user":  user,
	})
}