Skip to content

Commit

Permalink
Fixed few bugs in code
Browse files Browse the repository at this point in the history
  • Loading branch information
KostLinux committed Jun 2, 2024
1 parent a39e718 commit 296cf8f
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 19 deletions.
3 changes: 1 addition & 2 deletions bin/api/generate_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (

func generateAPIKey() (string, error) {
key := make([]byte, 16)
_, err := rand.Read(key)
if err != nil {
if _, err := rand.Read(key); err != nil {
return "", err
}

Expand Down
2 changes: 1 addition & 1 deletion controller/api/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func GetUsers(c *gin.Context) {
users, err := sql.GetUser()
users, err := sql.GetUsers()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
Expand Down
19 changes: 10 additions & 9 deletions middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,29 @@ import (
)

func Authentication() gin.HandlerFunc {
return func(c *gin.Context) {
// before request
session := sessions.Default(c)
return func(ctx *gin.Context) {
session := sessions.Default(ctx)
sessionID := session.Get("userID")

if sessionID == nil {
// Check for API key in the request
apiKey := c.GetHeader("X-API-Key")
apiKey := ctx.GetHeader("X-API-Key")
if apiKey != os.Getenv("STATUSPAGE_API_KEY") {
c.AbortWithStatus(http.StatusUnauthorized)
ctx.AbortWithStatus(http.StatusUnauthorized)
return
}

} else {

Check failure on line 26 in middleware/auth.go

View workflow job for this annotation

GitHub Actions / Verify Backend Code Quality

unnecessary trailing newline (whitespace)
userID := sessionID.(uint)

user, err := sql.GetUserByID(userID)
if err != nil {
c.AbortWithStatus(http.StatusUnauthorized)
ctx.AbortWithStatus(http.StatusUnauthorized)
return
}
c.Set("userID", user.ID)
}

c.Next()
ctx.Set("userID", user.ID)
}
ctx.Next()
}
}
2 changes: 1 addition & 1 deletion middleware/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewRouter(router *gin.Engine) *gin.Engine {
// Authentication
router.POST("/sign-up", api.SignUp)
router.POST("/sign-in", api.SignIn)
router.GET("/sign-out", api.SignOut)
router.POST("/sign-out", api.SignOut)

// API Handling
apiGroup := router.Group(os.Getenv("API_PATH"), Authentication())
Expand Down
8 changes: 4 additions & 4 deletions middleware/sites.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ func WelcomePageMiddleware() gin.HandlerFunc {
}

func LoginPageMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
if helper.IsUserAuthenticated(c) {
return func(ctx *gin.Context) {
if helper.IsUserAuthenticated(ctx) {
// If user is authenticated, serve the logout page
c.HTML(http.StatusOK, "authenticated.html", gin.H{})
ctx.HTML(http.StatusOK, "authenticated.html", gin.H{})
} else {
// If user is not authenticated, serve the authenticate page
c.HTML(http.StatusOK, "login.html", gin.H{})
ctx.HTML(http.StatusOK, "login.html", gin.H{})
}
}
}
2 changes: 1 addition & 1 deletion repository/db/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

var DB *gorm.DB

func GetUser() (model.User, error) {
func GetUsers() (model.User, error) {
var user model.User

transaction := DB.First(&user)
Expand Down
23 changes: 23 additions & 0 deletions views/login_page/assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,29 @@ document.addEventListener('DOMContentLoaded', (event) => {
});
});
});

// Handle sign-out button click
const signOutForm = document.querySelector('form[action="/sign-out"]');
signOutForm.addEventListener('submit', function(event) {
event.preventDefault(); // Prevent default form submission

// Submit form using Fetch
fetch(signOutForm.action, {
method: 'POST',
credentials: 'same-origin'
})
.then(response => {
if (!response.ok) {
throw new Error('Sign out failed');
}
// Redirect on successful response
window.location.href = '/';
})
.catch(error => {
// Handle network error
showBanner('Network error', 'error');
});
});
});

function showBanner(message, type) {
Expand Down
2 changes: 1 addition & 1 deletion views/login_page/authenticated.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">Users</button>
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">Ping</button>
</div>
<form action="/sign-out" method="GET">
<form action="/sign-out" method="POST">
<input type="submit" value="Logout" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">
</form>
</div>
Expand Down

0 comments on commit 296cf8f

Please sign in to comment.