feat: add simple Oauth2 logic for github
Some checks failed
Backend ci / build (push) Has been cancelled
Some checks failed
Backend ci / build (push) Has been cancelled
This commit is contained in:
@@ -1,17 +1,30 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/repositories"
|
||||
"os"
|
||||
|
||||
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/logger"
|
||||
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/repositories"
|
||||
"github.com/gin-gonic/gin"
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/oauth2/endpoints"
|
||||
)
|
||||
|
||||
type AuthHandlers struct {
|
||||
repo repositories.AuthRepository
|
||||
repo repositories.AuthRepository
|
||||
logger *logger.Logger
|
||||
}
|
||||
|
||||
var configGithub = &oauth2.Config{
|
||||
ClientID: os.Getenv("GITHUB_CLIENT_ID"),
|
||||
ClientSecret: os.Getenv("GITHUB_CLIENT_SECRET"),
|
||||
RedirectURL: "https://d3m0k1d.ru/",
|
||||
Scopes: []string{"user"},
|
||||
Endpoint: endpoints.GitHub,
|
||||
}
|
||||
|
||||
func NewAuthHandlers(repo repositories.AuthRepository) *AuthHandlers {
|
||||
return &AuthHandlers{repo: repo}
|
||||
return &AuthHandlers{repo: repo, logger: logger.New(false)}
|
||||
}
|
||||
|
||||
// Callback godoc
|
||||
@@ -21,7 +34,15 @@ func NewAuthHandlers(repo repositories.AuthRepository) *AuthHandlers {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} map[string]string
|
||||
// @Router /callback [get]
|
||||
func (h *AuthHandlers) Callback(c *gin.Context) {
|
||||
// @Router /callback/github [get]
|
||||
func (h *AuthHandlers) CallbackGithub(c *gin.Context) {
|
||||
h.logger.Info("CallbackGithub called")
|
||||
|
||||
token, err := configGithub.Exchange(c.Request.Context(), c.Query("code"))
|
||||
if err != nil {
|
||||
h.logger.Error("error request: " + err.Error())
|
||||
c.Status(500)
|
||||
}
|
||||
h.logger.Info("200 OK GET /callback/github")
|
||||
c.JSON(200, gin.H{"token": token})
|
||||
}
|
||||
|
||||
@@ -10,15 +10,14 @@ import (
|
||||
)
|
||||
|
||||
type PostHandlers struct {
|
||||
repo repositories.PostRepository
|
||||
repo repositories.PostRepository
|
||||
logger *logger.Logger
|
||||
}
|
||||
|
||||
func NewPostHandlers(repo repositories.PostRepository) *PostHandlers {
|
||||
return &PostHandlers{repo: repo}
|
||||
return &PostHandlers{repo: repo, logger: logger.New(false)}
|
||||
}
|
||||
|
||||
var log = logger.New(false)
|
||||
|
||||
// GetPosts godoc
|
||||
// @Summary Get all posts
|
||||
// @Description Get all posts
|
||||
@@ -31,10 +30,10 @@ func (h *PostHandlers) GetPosts(c *gin.Context) {
|
||||
var result []storage.PostReq
|
||||
result, err := h.repo.GetAll(c.Request.Context())
|
||||
if err != nil {
|
||||
log.Error("error request: " + err.Error())
|
||||
h.logger.Error("error request: " + err.Error())
|
||||
c.Status(500)
|
||||
}
|
||||
log.Info("200 OK GET /posts")
|
||||
h.logger.Info("200 OK GET /posts")
|
||||
c.JSON(200, result)
|
||||
}
|
||||
|
||||
@@ -54,15 +53,15 @@ func (h *PostHandlers) GetPost(c *gin.Context) {
|
||||
id_p := c.Param("id")
|
||||
id, err := strconv.Atoi(id_p)
|
||||
if err != nil {
|
||||
log.Error("error request: " + err.Error())
|
||||
h.logger.Error("error request: " + err.Error())
|
||||
c.Status(500)
|
||||
}
|
||||
result, err = h.repo.GetByID(c.Request.Context(), id)
|
||||
if err != nil {
|
||||
log.Error("error request: " + err.Error())
|
||||
h.logger.Error("error request: " + err.Error())
|
||||
c.Status(500)
|
||||
}
|
||||
log.Info("200 OK GET /posts/" + id_p)
|
||||
h.logger.Info("200 OK GET /posts/" + id_p)
|
||||
c.JSON(200, result)
|
||||
// TODO: added validaton for 400 response
|
||||
}
|
||||
@@ -112,7 +111,7 @@ func (h *PostHandlers) UpdatePost(c *gin.Context) {
|
||||
id_p := c.Param("id")
|
||||
id, err := strconv.Atoi(id_p)
|
||||
if err != nil {
|
||||
log.Error("error request: " + err.Error())
|
||||
h.logger.Error("error request: " + err.Error())
|
||||
c.Status(500)
|
||||
}
|
||||
var req storage.Post
|
||||
@@ -127,7 +126,7 @@ func (h *PostHandlers) UpdatePost(c *gin.Context) {
|
||||
}
|
||||
|
||||
c.JSON(200, req)
|
||||
log.Info("200 OK PUT /posts/" + id_p)
|
||||
h.logger.Info("200 OK PUT /posts/" + id_p)
|
||||
}
|
||||
|
||||
// DeletePost godoc
|
||||
@@ -139,5 +138,4 @@ func (h *PostHandlers) UpdatePost(c *gin.Context) {
|
||||
// @Success 200 {object} storage.Post
|
||||
// @Router /posts/{id} [delete]
|
||||
func DeletePost(c *gin.Context) {
|
||||
log.Info("DeletePost")
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ func Register(router *gin.Engine, db *sql.DB) {
|
||||
handler_posts := NewPostHandlers(repositories.NewPostRepository(db))
|
||||
handler_auth := NewAuthHandlers(repositories.NewAuthRepository(db))
|
||||
v1 := router.Group("api/v1")
|
||||
v1.GET("/callback", handler_auth.Callback)
|
||||
v1.GET("/callback", handler_auth.CallbackGithub)
|
||||
posts := v1.Group("posts")
|
||||
{
|
||||
posts.GET("/", handler_posts.GetPosts)
|
||||
|
||||
Reference in New Issue
Block a user