Add JWT auth and .env support for admin login
This commit is contained in:
@@ -3,6 +3,7 @@ package handlers
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"CatsOfMastodonBotGo/internal/auth"
|
||||
"CatsOfMastodonBotGo/internal/config"
|
||||
"CatsOfMastodonBotGo/internal/domain/requestModels"
|
||||
"CatsOfMastodonBotGo/internal/services"
|
||||
@@ -12,6 +13,7 @@ import (
|
||||
|
||||
type AdminDashboardHandler struct {
|
||||
PostService services.PostService
|
||||
Jwt auth.JwtTokenGenerator
|
||||
}
|
||||
|
||||
var AdminDashboardHandlerInstance *AdminDashboardHandler
|
||||
@@ -19,6 +21,7 @@ var AdminDashboardHandlerInstance *AdminDashboardHandler
|
||||
func InitAdminDashboardHandler() {
|
||||
AdminDashboardHandlerInstance = &AdminDashboardHandler{
|
||||
PostService: *services.PostServiceInstance,
|
||||
Jwt: *auth.JwtTokenGeneratorInstance,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,13 +67,13 @@ func (ps *AdminDashboardHandler) Login(c *gin.Context) {
|
||||
}
|
||||
|
||||
if input.Password == config.Config.AdminPassword { // Its more than enough for this project
|
||||
// token, err := ps.ps.Jwt.GenerateToken(map[string]interface{}{"role": "admin"})
|
||||
// if err != nil {
|
||||
// c.JSON(http.StatusInternalServerError, gin.H{"error": "Token generation failed"})
|
||||
// return
|
||||
// }
|
||||
token, err := ps.Jwt.GenerateToken(map[string]interface{}{"role": "admin"})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Token generation failed"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Login successful", "token": "yoy"}) // TODO: Add token
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Login successful", "token": token})
|
||||
|
||||
} else {
|
||||
c.JSON(401, gin.H{
|
||||
|
Reference in New Issue
Block a user