Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dashboard layout #38

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ services:
- "${DB_PORT}:5432"
volumes:
- psql_volume_bp:/var/lib/postgresql/data
networks:
- dev_network

minio:
image: minio/minio:latest
environment:
MINIO_SERVER_URL: http://localhost:${MINIO_PORT:-9000}
MINIO_ROOT_USER: ${MINIO_ROOT_USER}
MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD}
command: server /data
Expand All @@ -22,13 +25,17 @@ services:
volumes:
- minio_data:/data
restart: unless-stopped
networks:
- dev_network

mailhog:
image: mailhog/mailhog:latest
ports:
- "1025:1025" # SMTP server
- "8025:8025" # Web UI for email testing
restart: unless-stopped
networks:
- dev_network

redis:
image: redis:latest
Expand All @@ -37,6 +44,11 @@ services:
volumes:
- redis_data:/data
restart: unless-stopped
networks:
- dev_network

networks:
dev_network:

volumes:
psql_volume_bp:
Expand Down
27 changes: 18 additions & 9 deletions internal/app/container.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package app

import (
"sync"

"keizer-auth/internal/database"
"keizer-auth/internal/repositories"
"keizer-auth/internal/services"
"sync"
)

type Container struct {
DB database.Service
AuthService *services.AuthService
SessionService *services.SessionService
EmailService *services.EmailService
DB database.Service
AuthService *services.AuthService
SessionService *services.SessionService
EmailService *services.EmailService
AccountService *services.AccountService
ApplicationService *services.ApplicationService
}

var (
Expand All @@ -27,14 +28,22 @@ func GetContainer() *Container {
rds := database.NewRedisClient()

userRepo := repositories.NewUserRepository(gormDB)
accountRepo := repositories.NewAccountRepository(gormDB)
applicationRepo := repositories.NewApplicationRepository(gormDB)
userAccountRepo := repositories.NewUserAccountRepository(gormDB)
redisRepo := repositories.NewRedisRepository(rds)

authService := services.NewAuthService(userRepo, redisRepo)
sessionService := services.NewSessionService(redisRepo, userRepo)
accountService := services.NewAccountService(accountRepo, userAccountRepo)
applicationService := services.NewApplicationService(applicationRepo, accountRepo)

container = &Container{
DB: db,
AuthService: authService,
SessionService: sessionService,
DB: db,
AuthService: authService,
SessionService: sessionService,
AccountService: accountService,
ApplicationService: applicationService,
}
})
return container
Expand Down
8 changes: 6 additions & 2 deletions internal/app/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package app
import "keizer-auth/internal/controllers"

type ServerControllers struct {
Auth *controllers.AuthController
Auth *controllers.AuthController
Account *controllers.AccountController
Application *controllers.ApplicationController
}

func GetControllers(container *Container) *ServerControllers {
return &ServerControllers{
Auth: controllers.NewAuthController(container.AuthService, container.SessionService),
Auth: controllers.NewAuthController(container.AuthService, container.SessionService),
Account: controllers.NewAccountController(container.AccountService),
Application: controllers.NewApplicationController(container.ApplicationService),
}
}
15 changes: 15 additions & 0 deletions internal/app/middlewares.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package app

import (
"keizer-auth/internal/middlewares"
)

type ServerMiddlewares struct {
Auth *middlewares.AuthMiddleware
}

func GetMiddlewares(container *Container) *ServerMiddlewares {
return &ServerMiddlewares{
Auth: middlewares.NewAuthMiddleware(container.SessionService),
}
}
3 changes: 3 additions & 0 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package constants

const UserContextKey = "currentUser"
55 changes: 55 additions & 0 deletions internal/controllers/account_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package controllers

import (
"keizer-auth/internal/models"
"keizer-auth/internal/services"
"keizer-auth/internal/utils"
"keizer-auth/internal/validators"

"github.com/gofiber/fiber/v2"
)

type AccountController struct {
accountService *services.AccountService
}

func NewAccountController(
accountService *services.AccountService,
) *AccountController {
return &AccountController{accountService: accountService}
}

func (self *AccountController) Get(c *fiber.Ctx) error {
user := utils.GetCurrentUser(c)
accounts, err := self.accountService.GetAccountsByUser(user.ID)
if err != nil {
return c.SendStatus(fiber.StatusInternalServerError)
}
return c.JSON(accounts)
}

func (self *AccountController) Create(c *fiber.Ctx) error {
var err error
user := utils.GetCurrentUser(c)
body := new(validators.CreateAccount)

if err := c.BodyParser(body); err != nil {
return c.
Status(fiber.StatusBadRequest).
JSON(fiber.Map{"error": "Invalid request body"})
}

if err := body.ValidateFile(); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": err.Error(),
})
}

account := new(models.Account)
account, err = self.accountService.Create(body.Name, user.ID)
if err != nil {
return c.SendStatus(fiber.StatusInternalServerError)
}

return c.JSON(account)
}
57 changes: 57 additions & 0 deletions internal/controllers/applicaton_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package controllers

import (
"keizer-auth/internal/models"
"keizer-auth/internal/services"
"keizer-auth/internal/utils"
"keizer-auth/internal/validators"

"github.com/gofiber/fiber/v2"
)

type ApplicationController struct {
applicationService *services.ApplicationService
}

func NewApplicationController(
applicationService *services.ApplicationService,
) *ApplicationController {
return &ApplicationController{
applicationService: applicationService,
}
}

func (self *ApplicationController) Get(c *fiber.Ctx) error {
user := utils.GetCurrentUser(c)
applications, err := self.applicationService.Get(user.ID, user.ID)
if err != nil {
return c.SendStatus(fiber.StatusInternalServerError)
}
return c.JSON(applications)
}

func (self *ApplicationController) Create(c *fiber.Ctx) error {
var err error
user := utils.GetCurrentUser(c)
body := new(validators.CreateApplication)

if err := c.BodyParser(body); err != nil {
return c.
Status(fiber.StatusBadRequest).
JSON(fiber.Map{"error": "Invalid request body"})
}

if err := body.ValidateFile(); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": err.Error(),
})
}

account := new(models.Account)
account, err = self.applicationService.Create(body.Name, account.ID user.ID)

Check failure on line 51 in internal/controllers/applicaton_controller.go

View workflow job for this annotation

GitHub Actions / build

syntax error: unexpected name user in argument list; possibly missing comma or )

Check failure on line 51 in internal/controllers/applicaton_controller.go

View workflow job for this annotation

GitHub Actions / build

syntax error: unexpected name user in argument list; possibly missing comma or )
if err != nil {
return c.SendStatus(fiber.StatusInternalServerError)
}

return c.JSON(account)
}
29 changes: 10 additions & 19 deletions internal/controllers/auth_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package controllers
import (
"errors"
"fmt"

"keizer-auth/internal/models"
"keizer-auth/internal/services"
"keizer-auth/internal/utils"
Expand Down Expand Up @@ -53,6 +52,9 @@ func (ac *AuthController) SignIn(c *fiber.Ctx) error {
"error": "User is not verified. Please verify your account before signing in.",
})
}
if user.Type != models.Dashboard {
return c.SendStatus(fiber.StatusBadRequest)
}

isValid, err := ac.authService.VerifyPassword(
body.Password,
Expand All @@ -65,7 +67,7 @@ func (ac *AuthController) SignIn(c *fiber.Ctx) error {
}
if !isValid {
return c.
Status(fiber.StatusUnauthorized).
Status(fiber.StatusBadRequest).
JSON(fiber.Map{"error": "Invalid email or password. Please try again."})
}

Expand All @@ -76,8 +78,10 @@ func (ac *AuthController) SignIn(c *fiber.Ctx) error {
JSON(fiber.Map{"error": "Something went wrong, Failed to create session"})
}

fmt.Printf("%v", sessionId)
fmt.Print(sessionId)
utils.SetSessionCookie(c, sessionId)
return c.JSON(fiber.Map{"message": "signed in successfully"})
return c.JSON(user)
}

func (ac *AuthController) SignUp(c *fiber.Ctx) error {
Expand Down Expand Up @@ -149,23 +153,10 @@ func (ac *AuthController) VerifyOTP(c *fiber.Ctx) error {
}

utils.SetSessionCookie(c, sessionID)
return c.JSON(fiber.Map{"message": "OTP Verified!"})
return c.JSON(user)
}

func (ac *AuthController) VerifyTokenHandler(c *fiber.Ctx) error {
sessionID := utils.GetSessionCookie(c)
if sessionID == "" {
return c.
Status(fiber.StatusUnauthorized).
JSON(fiber.Map{"error": "Unauthorized"})
}

user := new(models.User)
if err := ac.sessionService.GetSession(sessionID, user); err != nil {
return c.
Status(fiber.StatusUnauthorized).
JSON(fiber.Map{"error": "Unauthorized"})
}

func (ac *AuthController) Profile(c *fiber.Ctx) error {
user := utils.GetCurrentUser(c)
return c.JSON(user)
}
39 changes: 33 additions & 6 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package database
import (
"context"
"fmt"
"keizer-auth/internal/models"
"log"
"os"
"strconv"
"time"

"keizer-auth/internal/models"

_ "github.com/joho/godotenv/autoload"
"gorm.io/driver/postgres"
"gorm.io/gorm"
Expand Down Expand Up @@ -42,7 +41,6 @@ var (
)

func New() Service {
// Reuse Connection
if dbInstance != nil {
return dbInstance
}
Expand Down Expand Up @@ -83,10 +81,39 @@ func GetDB() *gorm.DB {
}

func autoMigrate(db *gorm.DB) error {
return db.AutoMigrate(
&models.User{},
user := &models.User{}
if err := user.BeforeMigrate(db); err != nil {
return err
}

if err := db.AutoMigrate(
user,
&models.Domain{},
)
&models.Account{},
&models.UserAccount{},
); err != nil {
return err
}

if err := db.Exec(`
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM information_schema.constraint_column_usage
WHERE table_name = 'user_accounts'
AND constraint_name = 'check_role'
) THEN
ALTER TABLE user_accounts
ADD CONSTRAINT check_role
CHECK (role IN ('admin', 'member'));
END IF;
END $$;
`).Error; err != nil {
return err
}

return nil
}

// Health checks the health of the database connection by pinging the database.
Expand Down
Loading
Loading