Skip to content

Commit

Permalink
chore: Update database connection and setup UUID extension, add User …
Browse files Browse the repository at this point in the history
…model, and implement user services
  • Loading branch information
Ahmedhossamdev committed Jul 26, 2024
1 parent 8fa3fe0 commit 13f35b0
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 94 deletions.
14 changes: 0 additions & 14 deletions models/user.go

This file was deleted.

96 changes: 96 additions & 0 deletions routes/admin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package routes

import (
"Ahmedhossamdev/search-engine/db"
"Ahmedhossamdev/search-engine/utils"
"Ahmedhossamdev/search-engine/views"
"os"
"time"

"github.com/gofiber/fiber/v2"
"github.com/golang-jwt/jwt/v5"
)

type loginForm struct {
Email string `form:"email"`
Password string `form:"password"`
}

func LoginHandler(c *fiber.Ctx) error {
return render(c, views.Login())
}


func LoginPostHandler(c *fiber.Ctx) error {
input := loginForm{}

if err := c.BodyParser(&input); err != nil {
c.Status(500)
return c.SendString("<h2>Error: Something went wrong<h2>")
}

user := &db.User{}
user, err := user.LoginAsAdmin(input.Email, input.Password)

if err != nil {
c.Status(401)
return c.SendString("<h2>Error: Unauthorized")
}

signedToken, err := utils.CreateNewAuthToken(user.ID, user.Email, user.IsAdmin)

if err != nil {
c.Status(401)
return c.SendString("<h2>Error: Something went wrong while logging in <h2>")
}

cookie := fiber.Cookie {
Name: "admin",
Value: signedToken,
Expires: time.Now().Add(time.Hour * 24),
HTTPOnly: true,
}

c.Cookie(&cookie)
c.Append("HX-Redirect", "/")
return c.SendStatus(200)
}


func LogoutHandler (c *fiber.Ctx) error {
c.ClearCookie("admin")
c.Set("HX-Redirect", "/login")
return c.SendStatus(200)
}


type AdminClaims struct {
User string `json:"user"`
Id string `json:"id"`
jwt.RegisteredClaims `json:"claims"`
}

func AuthMiddleware(c *fiber.Ctx) error {
cookie := c.Cookies("adming")

if cookie == "" {
return c.Redirect("/login", 302)
}

token, err := jwt.ParseWithClaims(cookie, AdminClaims{}, func (token *jwt.Token)(interface{}, error) {
return []byte(os.Getenv("JWT_SECRET")), nil

})

if err != nil {
return c.Redirect("/login", 302)
}

_, ok := token.Claims.(*AdminClaims)
if ok && token.Valid {
c.Next()
}

return c.Redirect("login", 302)

}
34 changes: 4 additions & 30 deletions routes/route.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package routes

import (
"Ahmedhossamdev/search-engine/views"
"fmt"

"github.com/a-h/templ"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/adaptor"
Expand All @@ -20,10 +17,6 @@ func render(c *fiber.Ctx, component templ.Component, options ...func(*templ.Comp
}


type loginForm struct {
Email string `form:"email"`
Password string `form:"password"`
}

type settingsForm struct {
Amount int `form:"amount"`
Expand All @@ -34,31 +27,12 @@ type settingsForm struct {


func SetRoutes(app *fiber.App) {
app.Get("/", func(c *fiber.Ctx) error {
return render(c, views.Home())
})


app.Post("/", func(c *fiber.Ctx) error {
input := settingsForm{}
if err := c.BodyParser(&input); err != nil {
return c.SendString("<h1>Error: Something went wrong.. Please try again</h1>")
}
app.Get("/", AuthMiddleware ,LoginHandler)
app.Post("/",AuthMiddleware ,LoginPostHandler)

fmt.Println(input);
return c.SendStatus(200)
});
app.Get("/login", LoginHandler)
app.Post("/login", LoginPostHandler)


app.Get("login", func(c *fiber.Ctx) error {
return render(c, views.Login())
})

app.Post("login", func(c *fiber.Ctx) error {
input := loginForm{}
if err := c.BodyParser(&input); err != nil {
return c.SendString("<h1>Error: Something went wrong.. Please try again</h1>")
}
return c.SendStatus(200)
});
}
46 changes: 0 additions & 46 deletions services/user_services.go

This file was deleted.

2 changes: 1 addition & 1 deletion views/index.templ
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ templ template() {
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/full.min.css" rel="stylesheet" type="text/css"/>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/[email protected]" integrity="sha384-wS5l5IKJBvK6sPTKa2WZ1js3d947pvWXbPJ1OmWfEuxLgeHcEbjUUA5i9V5ZkpCw" crossorigin="anonymous"></script>
// <script src="https://unpkg.com/[email protected]/response-targets.js"></script>
<script src="https://unpkg.com/[email protected]/response-targets.js"></script>
</head>
<body>
{ children... }
Expand Down
2 changes: 1 addition & 1 deletion views/index_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion views/login.templ
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package views

templ Login() {
@template() {
<div class="flex justify-center items-center">
<div hx-ext="response-tragets" class="flex justify-center items-center">
<form
class="flex flex-col justify-center items-center gap-5 py-5"
hx-post="/login"
hx-target="#feedback"
hx-indicator="#indicator"
hx-target-errors="#feedback"
>
<label class="input input-bordered flex items-center gap-2 w-full">
Email
Expand Down
2 changes: 1 addition & 1 deletion views/login_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 13f35b0

Please sign in to comment.