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

User pages and a bunch of other things #37

Merged
merged 19 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
26 changes: 22 additions & 4 deletions controllers/authentication.go → auth/authentication.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controllers
package auth

import (
"fmt"
Expand All @@ -10,7 +10,10 @@ import (
"github.com/labstack/echo/v4"
)

func signToken(user models.User) (time.Time, string, error) {
// https://github.com/hay-i/chronologger/issues/35
var SecretKey = "my_secret"

func SignToken(user models.User) (time.Time, string, error) {
expirationTime := time.Now().Add(1 * time.Hour)
claims := &jwt.StandardClaims{
Subject: user.Username,
Expand All @@ -22,7 +25,7 @@ func signToken(user models.User) (time.Time, string, error) {
return expirationTime, signedToken, err
}

func setCookie(signedToken string, expiry time.Time, c echo.Context) {
func SetCookie(signedToken string, expiry time.Time, c echo.Context) {
cookie := new(http.Cookie)
cookie.Name = "token"
cookie.Value = signedToken
Expand All @@ -33,7 +36,7 @@ func setCookie(signedToken string, expiry time.Time, c echo.Context) {
c.SetCookie(cookie)
}

func parseToken(tokenString string) (jwt.MapClaims, error) {
func ParseToken(tokenString string) (jwt.MapClaims, error) {
token, _ := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
Expand All @@ -48,3 +51,18 @@ func parseToken(tokenString string) (jwt.MapClaims, error) {
return nil, fmt.Errorf("Invalid or expired token")
}
}

func IsLoggedIn(c echo.Context) bool {
cookie, err := c.Cookie("token")
if err != nil {
return false
} else {
_, err = ParseToken(cookie.Value)

if err != nil {
return false
} else {
return true
}
}
}
12 changes: 0 additions & 12 deletions components/base.templ
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,3 @@ templ PageBase() {
}
}
}

templ Home() {
@base() {
@head()
@body() {
<main class="width-wrap">
<h1>Chronologger</h1>
<a href={ templ.SafeURL("/templates") }>Our Templates</a>
</main>
}
}
}
71 changes: 0 additions & 71 deletions components/base_templ.go

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

21 changes: 21 additions & 0 deletions components/home.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package components

templ Home(signedIn bool) {
@base() {
@head()
@body() {
<main class="width-wrap">
<h1>Chronologger</h1>
<a href={ templ.SafeURL("/templates") }>Our Templates</a>
if signedIn {
<a href={ templ.SafeURL("/my-templates") }>My Templates</a>
<a href={ templ.SafeURL("/profile") }>My Profile</a>
<a href={ templ.SafeURL("/logout") }>Logout</a>
} else {
<a href={ templ.SafeURL("/register") }>Register</a>
<a href={ templ.SafeURL("/login") }>Login</a>
}
</main>
}
}
}
142 changes: 142 additions & 0 deletions components/home_templ.go

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

17 changes: 17 additions & 0 deletions controllers/home.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package controllers

import (
"go.mongodb.org/mongo-driver/mongo"

"github.com/hay-i/chronologger/auth"
"github.com/hay-i/chronologger/components"
"github.com/labstack/echo/v4"
)

func Home(database *mongo.Database) echo.HandlerFunc {
haydenrou marked this conversation as resolved.
Show resolved Hide resolved
return func(c echo.Context) error {
component := components.Home(auth.IsLoggedIn(c))

return render(c, component)
}
}
18 changes: 18 additions & 0 deletions controllers/renderer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package controllers

import (
"net/http"

"github.com/a-h/templ"
"github.com/labstack/echo/v4"
)

func render(c echo.Context, component templ.Component) error {
return component.Render(c.Request().Context(), c.Response().Writer)
}

func redirect(c echo.Context, url string) error {
// Ideally I wanted to send in http.StatusCreated, but it seems that the redirect doesn't work with that status code
// See: https://github.com/labstack/echo/issues/229#issuecomment-1518502318
return c.Redirect(http.StatusFound, url)
}
Loading