Skip to content

Commit

Permalink
temp commi
Browse files Browse the repository at this point in the history
  • Loading branch information
Sudarsh1010 committed Nov 24, 2024
1 parent c5ed242 commit 98dcdf7
Show file tree
Hide file tree
Showing 71 changed files with 2,702 additions and 406 deletions.
25 changes: 25 additions & 0 deletions :w
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package controllers

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

"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 := self.applicationService.Get(user.ID, user.ID)
}
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 )
if err != nil {
return c.SendStatus(fiber.StatusInternalServerError)
}

return c.JSON(account)
}
23 changes: 6 additions & 17 deletions internal/controllers/auth_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,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 @@ -75,6 +78,7 @@ 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(user)
Expand Down Expand Up @@ -152,22 +156,7 @@ func (ac *AuthController) VerifyOTP(c *fiber.Ctx) error {
return c.JSON(user)
}

func (ac *AuthController) VerifyTokenHandler(c *fiber.Ctx) error {
sessionID := utils.GetSessionCookie(c)
fmt.Print("\n")
fmt.Print(sessionID)
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

0 comments on commit 98dcdf7

Please sign in to comment.