Update struct attribute names and pass register logic from handler to controller

This commit is contained in:
Akhilesh Pandey 2023-07-18 00:57:06 +05:30
parent 959fb7b462
commit ee683aa2c8
3 changed files with 15 additions and 15 deletions

View File

@ -23,8 +23,8 @@ import (
)
type LoginInput struct {
Username string `json:"username"`
Password string `json:"password"`
LoginIdentifier string `json:"login_identifier"`
Password string `json:"password"`
}
/*
@ -34,15 +34,15 @@ func (c *Controller) Login(ctx context.Context, session *auth.Session,
in *LoginInput) (*types.TokenResponse, error) {
// no auth check required, password is used for it.
user, err := findUserFromUID(ctx, c.principalStore, in.Username)
user, err := findUserFromUID(ctx, c.principalStore, in.LoginIdentifier)
if errors.Is(err, store.ErrResourceNotFound) {
user, err = findUserFromEmail(ctx, c.principalStore, in.Username)
user, err = findUserFromEmail(ctx, c.principalStore, in.LoginIdentifier)
}
// always return not found for security reasons.
if err != nil {
log.Ctx(ctx).Debug().Err(err).
Str("user_uid", in.Username).
Str("user_uid", in.LoginIdentifier).
Msgf("failed to retrieve user during login.")
return nil, usererror.ErrNotFound
}

View File

@ -14,8 +14,8 @@ import (
type RegisterInput struct {
Email string `json:"email"`
DisplayName string `json:"displayname"`
Username string `json:"username"`
DisplayName string `json:"display_name"`
UID string `json:"uid"`
Password string `json:"password"`
}
@ -24,10 +24,15 @@ type RegisterInput struct {
* This differs from the Create method as it doesn't require auth, but has limited
* functionalities (unable to create admin user for example).
*/
func (c *Controller) Register(ctx context.Context, in *CreateInput) (*types.TokenResponse, error) {
func (c *Controller) Register(ctx context.Context, in *RegisterInput) (*types.TokenResponse, error) {
// TODO: allow to configure if open register is allowed.
user, err := c.CreateNoAuth(ctx, in, false)
user, err := c.CreateNoAuth(ctx, &CreateInput{
UID: in.UID,
Email: in.Email,
DisplayName: in.DisplayName,
Password: in.Password,
}, false)
if err != nil {
return nil, fmt.Errorf("failed to create user: %w", err)
}

View File

@ -25,12 +25,7 @@ func HandleRegister(userCtrl *user.Controller) http.HandlerFunc {
return
}
tokenResponse, err := userCtrl.Register(ctx, &user.CreateInput{
UID: in.Username,
Email: in.Email,
DisplayName: in.DisplayName,
Password: in.Password,
})
tokenResponse, err := userCtrl.Register(ctx, in)
if err != nil {
render.TranslatedUserError(w, err)
return