100 lines
2.7 KiB
Go
100 lines
2.7 KiB
Go
package restserver
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
|
|
"git.ukamnya.ru/stulyaganov/RestApiv2/internal/bd/model"
|
|
"git.ukamnya.ru/stulyaganov/RestApiv2/pkg/utils/password"
|
|
)
|
|
|
|
func (r *RestServer) configureRouterUser() {
|
|
r.router.HandleFunc("/api/register", r.chekUserRegistr(r.HandleFuncRegUser())).Methods("POST")
|
|
r.router.HandleFunc("/api/login", r.chekUserLogin(r.HandleFuncLoginUser())).Methods("POST")
|
|
r.router.HandleFunc("/api/logout", r.chekUserLogout(r.HandleFuncLogOutUser())).Methods("GET")
|
|
r.router.HandleFunc("/api/refresh", r.checkUserRefresh(r.HandleFuncRefresh())).Methods("GET")
|
|
}
|
|
|
|
func (r *RestServer) HandleFuncRegUser() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, res *http.Request) {
|
|
users := res.Context().Value(ContextKeyUser).(*model.User)
|
|
|
|
hash, err := password.HashPassword(users.Password)
|
|
|
|
if err != nil {
|
|
r.logger.Error(err)
|
|
}
|
|
users.Password = string(*hash)
|
|
err = r.db.User().Create(users)
|
|
if err != nil {
|
|
r.logger.Error(err)
|
|
} else {
|
|
// Reftoken, err := r.db.Token().FindByLogin(users.Login)
|
|
// if err != nil {
|
|
// fmt.Println(err)
|
|
// }
|
|
// if Reftoken != nil {
|
|
// users.RefreshToken = *Reftoken
|
|
// } else {
|
|
// r.db.Token().Create(users.Login, users.RefreshToken)
|
|
// }
|
|
|
|
// data := &model.UserData{
|
|
// Email: users.Email,
|
|
// PermisionLVL: users.PermisionLVL,
|
|
// AccesToken: users.AccesToken,
|
|
// RefreshToken: users.RefreshToken,
|
|
// }
|
|
// err = json.NewEncoder(w).Encode(data)
|
|
// if err != nil {
|
|
// r.logger.Errorln(err)
|
|
// }
|
|
io.WriteString(w, `{"data":"Пользователь успешно зарегистрирован"}`)
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (r *RestServer) HandleFuncLoginUser() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, res *http.Request) {
|
|
users := res.Context().Value(ContextKeyUser).(*model.User)
|
|
data := &model.UserData{
|
|
Login: users.Login,
|
|
Email: users.Email,
|
|
PermisionLVL: users.PermisionLVL,
|
|
AccesToken: users.AccesToken,
|
|
}
|
|
err := json.NewEncoder(w).Encode(data)
|
|
if err != nil {
|
|
r.logger.Errorln(err)
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
func (r *RestServer) HandleFuncLogOutUser() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, res *http.Request) {
|
|
fmt.Println("Logout")
|
|
}
|
|
}
|
|
|
|
func (r *RestServer) HandleFuncRefresh() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, res *http.Request) {
|
|
users := res.Context().Value(ContextKeyUser).(*model.User)
|
|
data := &model.UserData{
|
|
Login: users.Login,
|
|
Email: users.Email,
|
|
PermisionLVL: users.PermisionLVL,
|
|
AccesToken: users.AccesToken,
|
|
}
|
|
err := json.NewEncoder(w).Encode(data)
|
|
if err != nil {
|
|
r.logger.Errorln(err)
|
|
}
|
|
|
|
}
|
|
}
|