50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
|
package restserver
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"net/http"
|
||
|
|
||
|
"git.ukamnya.ru/stulyaganov/RestApiv2/internal/bd/model"
|
||
|
"git.ukamnya.ru/stulyaganov/RestApiv2/pkg/utils/validator"
|
||
|
)
|
||
|
|
||
|
func (r *RestServer) chekUserRegistr(next http.HandlerFunc) http.HandlerFunc {
|
||
|
return func(w http.ResponseWriter, res *http.Request) {
|
||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
||
|
w.Header().Set("Content-Type", "application/json")
|
||
|
users := &model.User{}
|
||
|
valid := validator.NewValidator(8, 32, 1)
|
||
|
err := json.NewDecoder(res.Body).Decode(users)
|
||
|
if err != nil {
|
||
|
r.logger.Error(err)
|
||
|
}
|
||
|
user, err := r.db.User().FindByLogin(users.Login)
|
||
|
if err != nil {
|
||
|
r.logger.Error(err)
|
||
|
}
|
||
|
if user.Login != "" {
|
||
|
io.WriteString(w, `{"data":"Такой Логин уже занят попробуйте придумать другой"}`)
|
||
|
return
|
||
|
}
|
||
|
user, err = r.db.User().FindByEmail(users.Email)
|
||
|
if err != nil {
|
||
|
r.logger.Error(err)
|
||
|
return
|
||
|
}
|
||
|
if user.Email != "" {
|
||
|
io.WriteString(w, `{"data":"Такой email уже используется другим пользователем"}`)
|
||
|
return
|
||
|
}
|
||
|
_, err = valid.CheckValid(users.Password)
|
||
|
if err != nil {
|
||
|
io.WriteString(w, fmt.Sprintf(`{"data":"%s"}`, err))
|
||
|
return
|
||
|
}
|
||
|
next(w, res.WithContext(context.WithValue(res.Context(), ContextKeyUser, users)))
|
||
|
}
|
||
|
}
|