Add user registration with password hashing and restructure handlers directory
This commit is contained in:
@@ -8,7 +8,6 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -34,7 +33,7 @@ func main() {
|
|||||||
var newAccounts = appContext.PostService.GetNewAccounts(existingAccountIds, newPosts)
|
var newAccounts = appContext.PostService.GetNewAccounts(existingAccountIds, newPosts)
|
||||||
|
|
||||||
// Save to database
|
// Save to database
|
||||||
log.Printf("Fetched %d posts, %d accounts; %d new posts and %d new accounts\n", len(posts), len(existingAccountIds), len(newPosts), len(newAccounts))
|
log.Printf("Fetched %d posts; %d existing posts; %d new posts and %d new accounts\n", len(posts), len(existingPostIds), len(newPosts), len(newAccounts))
|
||||||
|
|
||||||
// Additional logging
|
// Additional logging
|
||||||
if newAccounts != nil {
|
if newAccounts != nil {
|
||||||
@@ -56,8 +55,8 @@ func main() {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// https://seefnasrul.medium.com/create-your-first-go-rest-api-with-jwt-authentication-in-gin-framework-dbe5bda72817
|
// https://seefnasrul.medium.com/create-your-first-go-rest-api-with-jwt-authentication-in-gin-framework-dbe5bda72817
|
||||||
engine := gin.Default()
|
|
||||||
r := server.SetupRouter(engine, appContext)
|
r := server.SetupRouter(appContext)
|
||||||
err := r.Run(":8080")
|
err := r.Run(":8080")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
@@ -10,6 +10,7 @@ import (
|
|||||||
type AppContext struct {
|
type AppContext struct {
|
||||||
Db *gorm.DB
|
Db *gorm.DB
|
||||||
PostService *services.PostService
|
PostService *services.PostService
|
||||||
|
UserService *services.UserService
|
||||||
Instance string
|
Instance string
|
||||||
Tag string
|
Tag string
|
||||||
}
|
}
|
10
internal/auth/password.go
Normal file
10
internal/auth/password.go
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func HashPassword(password string) (string, error) {
|
||||||
|
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||||
|
return string(bytes), err
|
||||||
|
}
|
@@ -1,35 +0,0 @@
|
|||||||
package handlers_admin
|
|
||||||
|
|
||||||
import (
|
|
||||||
"CatsOfMastodonBotGo/internal"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
|
||||||
|
|
||||||
type AdminDashboardHandler struct {
|
|
||||||
AppContext *internal.AppContext
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewAdminDashboardHandler(appContext *internal.AppContext) *AdminDashboardHandler {
|
|
||||||
return &AdminDashboardHandler{
|
|
||||||
AppContext: appContext,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (appContext *AdminDashboardHandler) AdminHomePage(c *gin.Context) {
|
|
||||||
c.JSON(200, gin.H{
|
|
||||||
"YouAreOn": "AdminDashboardHomePage",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (appContext *AdminDashboardHandler) ApproveMedia(c *gin.Context) {
|
|
||||||
c.JSON(200, gin.H{
|
|
||||||
"YouAreOn": "ApproveMedia",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (appContext *AdminDashboardHandler) RejectMedia(c *gin.Context) {
|
|
||||||
c.JSON(200, gin.H{
|
|
||||||
"YouAreOn": "RejectMedia",
|
|
||||||
})
|
|
||||||
}
|
|
@@ -31,10 +31,14 @@ func SetupAppContext() *internal.AppContext {
|
|||||||
//Setup PostService
|
//Setup PostService
|
||||||
var postService = services.NewPostService(db)
|
var postService = services.NewPostService(db)
|
||||||
|
|
||||||
|
// Setup UserService
|
||||||
|
var userService = services.NewUserService(db)
|
||||||
|
|
||||||
// Inititlize AppContext
|
// Inititlize AppContext
|
||||||
var appContext = &internal.AppContext{
|
var appContext = &internal.AppContext{
|
||||||
Db: db,
|
Db: db,
|
||||||
PostService: postService,
|
PostService: postService,
|
||||||
|
UserService: userService,
|
||||||
Instance: instance,
|
Instance: instance,
|
||||||
Tag: tag,
|
Tag: tag,
|
||||||
}
|
}
|
||||||
|
@@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func AddMigrations(db *gorm.DB) {
|
func AddMigrations(db *gorm.DB) {
|
||||||
db.AutoMigrate(&models.Post{}, &models.MediaAttachment{}, &models.Account{})
|
db.AutoMigrate(&models.Post{}, &models.MediaAttachment{}, &models.Account{}, models.ComUser{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
6
internal/models/requestModels/register.go
Normal file
6
internal/models/requestModels/register.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package requestmodels
|
||||||
|
|
||||||
|
type RegisterInput struct {
|
||||||
|
Username string `json:"username" binding:"required"`
|
||||||
|
Password string `json:"password" binding:"required"`
|
||||||
|
}
|
@@ -2,15 +2,15 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"CatsOfMastodonBotGo/internal"
|
"CatsOfMastodonBotGo/internal"
|
||||||
handlers_admin "CatsOfMastodonBotGo/internal/handlers/admin"
|
handlers_admin "CatsOfMastodonBotGo/internal/web/handlers/admin"
|
||||||
handlers_api "CatsOfMastodonBotGo/internal/handlers/api"
|
handlers_api "CatsOfMastodonBotGo/internal/web/handlers/api"
|
||||||
handlers_home "CatsOfMastodonBotGo/internal/handlers/home"
|
handlers_home "CatsOfMastodonBotGo/internal/web/handlers/home"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SetupRouter(r *gin.Engine, appContext *internal.AppContext) *gin.Engine {
|
func SetupRouter(appContext *internal.AppContext) *gin.Engine {
|
||||||
|
r := gin.Default()
|
||||||
|
|
||||||
adminDashboardHandler := handlers_admin.NewAdminDashboardHandler(appContext)
|
adminDashboardHandler := handlers_admin.NewAdminDashboardHandler(appContext)
|
||||||
homePageHandler := handlers_home.NewMainPageHandler(appContext)
|
homePageHandler := handlers_home.NewMainPageHandler(appContext)
|
||||||
@@ -19,6 +19,8 @@ func SetupRouter(r *gin.Engine, appContext *internal.AppContext) *gin.Engine {
|
|||||||
admin := r.Group("/admin")
|
admin := r.Group("/admin")
|
||||||
|
|
||||||
admin.GET("/", adminDashboardHandler.AdminHomePage)
|
admin.GET("/", adminDashboardHandler.AdminHomePage)
|
||||||
|
admin.POST("/register", adminDashboardHandler.Register)
|
||||||
|
admin.POST("/login", adminDashboardHandler.Login)
|
||||||
admin.POST("/approve", adminDashboardHandler.ApproveMedia)
|
admin.POST("/approve", adminDashboardHandler.ApproveMedia)
|
||||||
admin.POST("/reject", adminDashboardHandler.RejectMedia)
|
admin.POST("/reject", adminDashboardHandler.RejectMedia)
|
||||||
|
|
||||||
|
@@ -1 +1,18 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
|
import (
|
||||||
|
"CatsOfMastodonBotGo/internal/models"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UserService struct {
|
||||||
|
db *gorm.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUserService(db *gorm.DB) *UserService {
|
||||||
|
return &UserService{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (us *UserService) CreateUser(user models.ComUser) int {
|
||||||
|
return int(us.db.Create(&user).RowsAffected)
|
||||||
|
}
|
75
internal/web/handlers/admin/adminDash.go
Normal file
75
internal/web/handlers/admin/adminDash.go
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
package handlers_admin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"CatsOfMastodonBotGo/internal"
|
||||||
|
"CatsOfMastodonBotGo/internal/models"
|
||||||
|
requestmodels "CatsOfMastodonBotGo/internal/models/requestModels"
|
||||||
|
"net/http"
|
||||||
|
"CatsOfMastodonBotGo/internal/auth"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
type AdminDashboardHandler struct {
|
||||||
|
AppContext *internal.AppContext
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAdminDashboardHandler(appContext *internal.AppContext) *AdminDashboardHandler {
|
||||||
|
return &AdminDashboardHandler{
|
||||||
|
AppContext: appContext,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (appContext *AdminDashboardHandler) AdminHomePage(c *gin.Context) {
|
||||||
|
c.JSON(200, gin.H{
|
||||||
|
"YouAreOn": "AdminDashboardHomePage",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (appContext *AdminDashboardHandler) ApproveMedia(c *gin.Context) {
|
||||||
|
c.JSON(200, gin.H{
|
||||||
|
"YouAreOn": "ApproveMedia",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (appContext *AdminDashboardHandler) RejectMedia(c *gin.Context) {
|
||||||
|
c.JSON(200, gin.H{
|
||||||
|
"YouAreOn": "RejectMedia",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (appContext *AdminDashboardHandler) Register(c *gin.Context) {
|
||||||
|
|
||||||
|
var input requestmodels.RegisterInput
|
||||||
|
|
||||||
|
if err := c.ShouldBindJSON(&input); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
hashedPassword, err := auth.HashPassword(input.Password)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var user = models.ComUser{
|
||||||
|
Username: input.Username,
|
||||||
|
Password: hashedPassword,
|
||||||
|
}
|
||||||
|
if appContext.AppContext.UserService.CreateUser(user) == 0 {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{
|
||||||
|
"success": false,
|
||||||
|
"error": "failed to create user",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(200, gin.H{
|
||||||
|
"success": true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (appContext *AdminDashboardHandler) Login(c *gin.Context) {
|
||||||
|
c.JSON(200, gin.H{
|
||||||
|
"YouAreOn": "Login",
|
||||||
|
})
|
||||||
|
}
|
Reference in New Issue
Block a user