add jwt authentication, and fix cours for next js
This commit is contained in:
1 parent
3026f89f2e
commit
05b8d55d0e
17 files changed
+471
-64
No files matched your search
@@ -13,12 +13,12 @@ import (
|
||||
|
||||
func (r *RestServer) configureRouterFilms() {
|
||||
r.router.HandleFunc("/api/hello", r.HandleHello()).Methods("GET")
|
||||
r.router.HandleFunc("/api/films", r.HendleFindAll()).Methods("GET")
|
||||
r.router.HandleFunc("/api/films/{id:[0-9]+}", r.HendleFindID()).Methods("GET")
|
||||
r.router.HandleFunc("/api/films/{name}", r.HeandleFilmsFindName()).Methods("GET")
|
||||
r.router.HandleFunc("/api/films/genres/{name}", r.HeandleFilmsSortGenres()).Methods("GET")
|
||||
r.router.HandleFunc("/api/films/page/{page:[0-9]+}", r.HendlePagination()).Methods("GET")
|
||||
r.router.HandleFunc("/api/films/last/", r.HeadleGetLastItem()).Methods("GET")
|
||||
r.router.HandleFunc("/api/films", r.checkJwtAccess(r.HendleFindAll())).Methods("GET")
|
||||
r.router.HandleFunc("/api/films/{id:[0-9]+}", r.checkJwtAccess(r.HendleFindID())).Methods("GET")
|
||||
r.router.HandleFunc("/api/films/{name}", r.checkJwtAccess(r.HeandleFilmsFindName())).Methods("GET")
|
||||
r.router.HandleFunc("/api/films/genres/{name}", r.checkJwtAccess(r.HeandleFilmsSortGenres())).Methods("GET")
|
||||
r.router.HandleFunc("/api/films/page/{page:[0-9]+}", r.checkJwtAccess(r.HendlePagination())).Methods("GET")
|
||||
r.router.HandleFunc("/api/films/last/", r.checkJwtAccess(r.HeadleGetLastItem())).Methods("GET")
|
||||
}
|
||||
|
||||
func (r *RestServer) HandleHello() http.HandlerFunc {
|
||||
@@ -64,10 +64,6 @@ func (r *RestServer) HendleFindAll() http.HandlerFunc {
|
||||
|
||||
func (r *RestServer) HeandleFilmsFindName() 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")
|
||||
name := mux.Vars(res)["name"]
|
||||
films, err := r.db.Films().FindByName(name)
|
||||
if err != nil {
|
||||
@@ -88,9 +84,6 @@ func (r *RestServer) HeandleFilmsFindName() http.HandlerFunc {
|
||||
|
||||
func (r *RestServer) HeandleFilmsSortGenres() 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")
|
||||
name := mux.Vars(res)["name"]
|
||||
films, err := r.db.Films().SortByGanres(name)
|
||||
if err != nil {
|
||||
@@ -106,9 +99,6 @@ func (r *RestServer) HeandleFilmsSortGenres() http.HandlerFunc {
|
||||
|
||||
func (r *RestServer) HendleFindID() 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")
|
||||
id := mux.Vars(res)["id"]
|
||||
fmt.Println(mux.Vars(res))
|
||||
film, err := r.db.Films().FindById(id)
|
||||
@@ -126,9 +116,6 @@ func (r *RestServer) HendleFindID() http.HandlerFunc {
|
||||
|
||||
func (r *RestServer) HendlePagination() 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")
|
||||
id := mux.Vars(res)["page"]
|
||||
films, err := r.db.Films().Pagination(id)
|
||||
if err != nil {
|
||||
@@ -145,9 +132,6 @@ func (r *RestServer) HendlePagination() http.HandlerFunc {
|
||||
|
||||
func (r *RestServer) HeadleGetLastItem() 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")
|
||||
films, err := r.db.Films().LastItem()
|
||||
if err != nil {
|
||||
r.logger.Errorln(err)
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package restserver
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"git.ukamnya.ru/stulyaganov/RestApiv2/pkg/utils/jwt"
|
||||
)
|
||||
|
||||
func (r *RestServer) checkJwtAccess(next http.HandlerFunc) http.HandlerFunc {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, res *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
str := res.Header.Values("Authorization")
|
||||
if str == nil {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
io.WriteString(w, `{"data":"Ошибка Авторизации"}`)
|
||||
return
|
||||
}
|
||||
|
||||
token := strings.Split(str[0], " ")
|
||||
chek, err := jwt.ValidateToken(token[1], []byte(os.Getenv("JWT_SECRET_KEY_ACCESS")))
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
io.WriteString(w, `{"data":"Ошибка Авторизации"}`)
|
||||
r.logger.Error(err)
|
||||
return
|
||||
}
|
||||
if chek {
|
||||
next(w, res)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
io.WriteString(w, `{"data":"Ошибка Авторизации"}`)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"git.ukamnya.ru/stulyaganov/RestApiv2/internal/bd"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/rs/cors"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -37,14 +38,19 @@ func (r *RestServer) Start() error {
|
||||
if err := r.configurebd(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
corsHendler := cors.New(cors.Options{
|
||||
AllowedOrigins: []string{"http://localhost:3000"},
|
||||
AllowedHeaders: []string{"Content-Type", "Authorization", "Set-Cookie"},
|
||||
AllowedMethods: []string{http.MethodGet, http.MethodPost, http.MethodOptions, http.MethodDelete, http.MethodPut},
|
||||
AllowCredentials: true,
|
||||
}).Handler(r.router)
|
||||
r.configureRouterFilms()
|
||||
r.configureRouterSiries()
|
||||
r.configureRouterUser()
|
||||
|
||||
r.logger.Info("Starting Server")
|
||||
r.logger.Info("Listen server on ", r.config.BindPort, " Port")
|
||||
return http.ListenAndServe(r.config.BindPort, r.router)
|
||||
return http.ListenAndServe(r.config.BindPort, corsHendler)
|
||||
}
|
||||
|
||||
func (r *RestServer) configureLogger() error {
|
||||
|
||||
@@ -12,20 +12,17 @@ import (
|
||||
)
|
||||
|
||||
func (r *RestServer) configureRouterSiries() {
|
||||
r.router.HandleFunc("/api/siries", r.HendleFindAllSiries()).Methods("GET")
|
||||
r.router.HandleFunc("/api/siries/{id:[0-9]+}", r.HendleFindIDSiries()).Methods("GET")
|
||||
r.router.HandleFunc("/api/siries/{name}", r.HeandleSiriesFindName()).Methods("GET")
|
||||
r.router.HandleFunc("/api/siries/genres/{name}", r.HeandleSiriesSortGenres()).Methods("GET")
|
||||
r.router.HandleFunc("/api/siries/page/{page:[0-9]+}", r.HendlePaginationSiries()).Methods("GET")
|
||||
r.router.HandleFunc("/api/siries/lastS/", r.HeadleGetLastItemSiries()).Methods("GET")
|
||||
r.router.HandleFunc("/api/siries", r.checkJwtAccess(r.HendleFindAllSiries())).Methods("GET")
|
||||
r.router.HandleFunc("/api/siries/{id:[0-9]+}", r.checkJwtAccess(r.HendleFindIDSiries())).Methods("GET")
|
||||
r.router.HandleFunc("/api/siries/{name}", r.checkJwtAccess(r.HeandleSiriesFindName())).Methods("GET")
|
||||
r.router.HandleFunc("/api/siries/genres/{name}", r.checkJwtAccess(r.HeandleSiriesSortGenres())).Methods("GET")
|
||||
r.router.HandleFunc("/api/siries/page/{page:[0-9]+}", r.checkJwtAccess(r.HendlePaginationSiries())).Methods("GET")
|
||||
r.router.HandleFunc("/api/siries/lastS/", r.checkJwtAccess(r.HeadleGetLastItemSiries())).Methods("GET")
|
||||
}
|
||||
|
||||
func (r *RestServer) HandleHelloSiries() 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")
|
||||
id := res.URL.Query().Get("id")
|
||||
fmt.Println(id)
|
||||
cout, err := r.db.Siries().GetCountFilms()
|
||||
@@ -63,9 +60,6 @@ func (r *RestServer) HendleFindAllSiries() http.HandlerFunc {
|
||||
|
||||
func (r *RestServer) HeandleSiriesFindName() 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")
|
||||
name := mux.Vars(res)["name"]
|
||||
siries, err := r.db.Siries().FindByName(name)
|
||||
if err != nil {
|
||||
@@ -86,9 +80,6 @@ func (r *RestServer) HeandleSiriesFindName() http.HandlerFunc {
|
||||
|
||||
func (r *RestServer) HeandleSiriesSortGenres() 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")
|
||||
name := mux.Vars(res)["name"]
|
||||
siries, err := r.db.Siries().SortByGanres(name)
|
||||
if err != nil {
|
||||
@@ -104,9 +95,6 @@ func (r *RestServer) HeandleSiriesSortGenres() http.HandlerFunc {
|
||||
|
||||
func (r *RestServer) HendleFindIDSiries() 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")
|
||||
id := mux.Vars(res)["id"]
|
||||
fmt.Println(mux.Vars(res))
|
||||
siries, err := r.db.Siries().FindById(id)
|
||||
@@ -123,9 +111,6 @@ func (r *RestServer) HendleFindIDSiries() http.HandlerFunc {
|
||||
|
||||
func (r *RestServer) HendlePaginationSiries() 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")
|
||||
id := mux.Vars(res)["page"]
|
||||
siries, err := r.db.Siries().Pagination(id)
|
||||
if err != nil {
|
||||
@@ -142,9 +127,6 @@ func (r *RestServer) HendlePaginationSiries() http.HandlerFunc {
|
||||
|
||||
func (r *RestServer) HeadleGetLastItemSiries() 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")
|
||||
siries, err := r.db.Siries().LastItem()
|
||||
if err != nil {
|
||||
r.logger.Errorln(err)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package restserver
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -11,8 +12,9 @@ import (
|
||||
|
||||
func (r *RestServer) configureRouterUser() {
|
||||
r.router.HandleFunc("/api/register", r.chekUserRegistr(r.HandleFuncRegUser())).Methods("POST")
|
||||
r.router.HandleFunc("/api/login", r.HandleFuncLoginUser()).Methods("POST")
|
||||
r.router.HandleFunc("/api/logout", r.HandleFuncLogOutUser()).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 {
|
||||
@@ -29,7 +31,27 @@ func (r *RestServer) HandleFuncRegUser() http.HandlerFunc {
|
||||
if err != nil {
|
||||
r.logger.Error(err)
|
||||
} else {
|
||||
io.WriteString(w, `{"data":"Пользователь создан"}`)
|
||||
// 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":"Пользователь успешно зарегистрирован"}`)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -38,12 +60,40 @@ func (r *RestServer) HandleFuncRegUser() http.HandlerFunc {
|
||||
|
||||
func (r *RestServer) HandleFuncLoginUser() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, res *http.Request) {
|
||||
fmt.Println("login")
|
||||
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("login")
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,12 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"git.ukamnya.ru/stulyaganov/RestApiv2/internal/bd/model"
|
||||
"git.ukamnya.ru/stulyaganov/RestApiv2/pkg/utils/jwt"
|
||||
"git.ukamnya.ru/stulyaganov/RestApiv2/pkg/utils/password"
|
||||
"git.ukamnya.ru/stulyaganov/RestApiv2/pkg/utils/validator"
|
||||
)
|
||||
|
||||
@@ -21,6 +25,9 @@ func (r *RestServer) chekUserRegistr(next http.HandlerFunc) http.HandlerFunc {
|
||||
err := json.NewDecoder(res.Body).Decode(users)
|
||||
if err != nil {
|
||||
r.logger.Error(err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
io.WriteString(w, `{"data":"Что-то пошло не так"}`)
|
||||
return
|
||||
}
|
||||
user, err := r.db.User().FindByLogin(users.Login)
|
||||
if err != nil {
|
||||
@@ -44,6 +51,133 @@ func (r *RestServer) chekUserRegistr(next http.HandlerFunc) http.HandlerFunc {
|
||||
io.WriteString(w, fmt.Sprintf(`{"data":"%s"}`, err))
|
||||
return
|
||||
}
|
||||
users.PermisionLVL = defaultPermLvl
|
||||
// jwtToken, _ := jwt.GenerateTokens(*users)
|
||||
|
||||
// cookie := http.Cookie{
|
||||
// Name: "refreshToken",
|
||||
// Value: jwtToken.RefreshToken,
|
||||
// Expires: time.Now().Add(time.Hour * 24 * 360),
|
||||
// HttpOnly: true,
|
||||
// }
|
||||
// http.SetCookie(w, &cookie)
|
||||
// users.AccesToken = jwtToken.AccesToken
|
||||
// users.RefreshToken = jwtToken.RefreshToken
|
||||
next(w, res.WithContext(context.WithValue(res.Context(), ContextKeyUser, users)))
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RestServer) chekUserLogin(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, res *http.Request) {
|
||||
users := &model.User{}
|
||||
err := json.NewDecoder(res.Body).Decode(users)
|
||||
if err != nil {
|
||||
r.logger.Error(err)
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
io.WriteString(w, `{"data":"Что-то пошло не так"}`)
|
||||
return
|
||||
}
|
||||
user, err := r.db.User().FindByLoginPas(users.Login)
|
||||
if err != nil {
|
||||
r.logger.Error(err)
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
io.WriteString(w, `{"data":"Что-то пошло не так"}`)
|
||||
return
|
||||
}
|
||||
if user.Login == "" {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
io.WriteString(w, `{"data":"Неверный логин или пароль"}`)
|
||||
return
|
||||
}
|
||||
checkPass := password.CheckValid(users.Password, user.Password)
|
||||
if !checkPass {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
io.WriteString(w, `{"data":"Неверный логин или пароль"}`)
|
||||
return
|
||||
}
|
||||
jwtToken, _ := jwt.GenerateTokens(*user)
|
||||
Reftoken, err := r.db.Token().FindByLogin(users.Login)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
io.WriteString(w, `{"data":"Что-то пошло не так "}`)
|
||||
return
|
||||
}
|
||||
if *Reftoken != "" {
|
||||
jwtToken.RefreshToken = *Reftoken
|
||||
} else {
|
||||
r.db.Token().Create(users.Login, jwtToken.RefreshToken)
|
||||
}
|
||||
cookie := http.Cookie{
|
||||
Name: "refreshToken",
|
||||
Value: jwtToken.RefreshToken,
|
||||
Path: "/",
|
||||
SameSite: http.SameSiteDefaultMode,
|
||||
Expires: time.Now().Add(time.Hour * 24 * 360),
|
||||
HttpOnly: true,
|
||||
}
|
||||
http.SetCookie(w, &cookie)
|
||||
users.AccesToken = jwtToken.AccesToken
|
||||
users.PermisionLVL = user.PermisionLVL
|
||||
users.Email = user.Email
|
||||
next(w, res.WithContext(context.WithValue(res.Context(), ContextKeyUser, users)))
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RestServer) chekUserLogout(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, res *http.Request) {
|
||||
checkCookie, err := res.Cookie("refreshToken")
|
||||
if err != nil {
|
||||
r.logger.Error(err)
|
||||
} else {
|
||||
if checkCookie.Value != "" {
|
||||
login, _ := r.db.Token().FindByToken(checkCookie.Value)
|
||||
if *login != "" {
|
||||
r.db.Token().DeleteByLogin(*login)
|
||||
}
|
||||
cookie := http.Cookie{
|
||||
Name: "refreshToken",
|
||||
Value: "",
|
||||
MaxAge: -1,
|
||||
Path: "/",
|
||||
HttpOnly: true,
|
||||
}
|
||||
http.SetCookie(w, &cookie)
|
||||
}
|
||||
}
|
||||
|
||||
next(w, res)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RestServer) checkUserRefresh(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, res *http.Request) {
|
||||
users := &model.User{}
|
||||
checkCookie, err := res.Cookie("refreshToken")
|
||||
if err != nil {
|
||||
r.logger.Error(err)
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
chekToken, err := jwt.ValidateToken(checkCookie.Value, []byte(os.Getenv("JWT_SECRET_KEY_REFRESH")))
|
||||
if err != nil {
|
||||
r.logger.Error(err)
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
login, err := r.db.Token().FindByToken(checkCookie.Value)
|
||||
if err != nil {
|
||||
r.logger.Error(err)
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
if *login != "" && chekToken {
|
||||
userdb, _ := r.db.User().FindByLoginPas(*login)
|
||||
jwtToken, _ := jwt.GenerateTokens(*userdb)
|
||||
users.Login = userdb.Login
|
||||
users.AccesToken = jwtToken.AccesToken
|
||||
users.PermisionLVL = userdb.PermisionLVL
|
||||
users.Email = userdb.Email
|
||||
next(w, res.WithContext(context.WithValue(res.Context(), ContextKeyUser, users)))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user