21 | if !fromProtected {
22 |
23 | Go Home Page
24 |
25 | } else {
26 |
31 | Go Todo List Page
32 |
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2023 Enrique Marín
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/views/home.templ:
--------------------------------------------------------------------------------
1 | package views
2 |
3 | import "github.com/gofiber/fiber/v2"
4 |
5 | templ HomeIndex(fromProtected bool) {
6 |
7 |
Welcome to our TodoList !!
8 |
9 | Here you can keep track of all your tasks and have an overview of your responsibilities.
10 |
2 |
3 | # Go/Fiber🧬+>Templ to-do list app with user login and HTMx-powered frontend (Demo)
4 |
5 | A full-stack application using Golang's Fiber framework with session-based authentication. Once we are authenticated we can enter a view from which we can manage a list of tasks (list, update and delete). Requests to the backend are controlled by [>htmx](https://htmx.org/) ([hypermedia](https://hypermedia.systems/) only).
6 |
7 |  
8 |
9 |
10 |
11 | >[!NOTE]
12 | >***This application is an clone of this [repository](https://github.com/emarifer/rust-axum-askama-htmx-todoapp) of mine (rust-axum-askama-htmx-todoapp), but made in `Golang`.***
13 |
14 |
15 |
16 | ### 🤔 Explanation
17 |
18 | The application allows us to perform a complete CRUD on the database, in this case SQLite3.
19 |
20 | The DB stores both a table with the users and another table for each user's to-do. Both tables are related using a foreign key.
21 |
22 | In this application we have used the Fiber framework, which, as it says in its documentation, is "an Express-inspired web framework". This makes creating web applications much easier for people coming from the world of JavaScript. Fiber is a Go web framework built on top of Fasthttp, the fastest HTTP engine for Go. It's designed to ease things up for fast development with zero memory allocation and performance in mind.
23 |
24 | Authentication (via session) is done using built-in middleware. On the other hand, a [dependency](https://github.com/sujit-baniya/flash) is also used to handle flash messages.
25 |
26 | Finally, using the Fiber framework also allows us centralized error handling.
27 |
28 |
29 |
30 | >[!IMPORTANT]
31 | >***In this application, instead of using the html/template package (gofiber/template/html, specifically), we use the [a-h/templ](https://github.com/a-h/templ) library. This amazing library implements a templating language (very similar to JSX) that compiles to Go code. Templ will allow us to write code almost identical to Go (with expressions, control flow, if/else, for loops, etc.) and have autocompletion thanks to strong typing. This means that errors appear at compile time and any calls to these templates (which are compiled as Go functions) from the handlers side will always require the correct data, minimizing errors and thus increasing the security and speed of our coding.***
32 |
33 | The use of >htmx allows behavior similar to that of a SPA, without page reloads when switching from one route to another or when making requests (via AJAX) to the backend.
34 |
35 | On the other hand, the styling of the views is achieved through Tailwind CSS and DaisyUI that are obtained from their respective CDNs. Likewise, the `SweetAlert2` library is used, a substitute for javascript pop-up boxes. In the same way it is obtained from its respective CDN.
36 |
37 | Finally, minimal use of [_hyperscript](https://hyperscript.org/) is made to achieve the action of closing alerts when they are displayed and the action of showing/hiding the password in corresponding text input fields.
38 |
39 | >[!NOTE]
40 | >***This application is identical, although much more improved, to that of a previous [repository](https://github.com/emarifer/gofiber-htmx-todolist) of mine, which is developed in GoFiber-template/html instead of [Templ](https://templ.guide/) templating language, as in this case.***
41 |
42 | ---
43 |
44 | ## 🖼️ Screenshots:
45 |
46 |
47 |
48 | ###### Todo List Page with success alert & Sign Up Page with error alert:
49 |
50 |
51 |
52 |
53 |
54 | ###### Task update page & popup alert based on SweetAlert2:
55 |
56 |
57 |
58 |
59 |
60 | ###### Centralized handling of 404 & 500 errors:
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 | ---
69 |
70 | ## 👨🚀 Setup:
71 |
72 | Besides the obvious prerequisite of having Go! on your machine, you must have Air installed for hot reloading when editing code.
73 |
74 | Start the app in development mode:
75 |
76 | ```
77 | $ air # This also compiles the view templates automatically / Ctrl + C to stop the application
78 | ```
79 |
80 | Build for production (previously it is necessary to regenerate the templates with the `templ generate` command; see explanation below):
81 |
82 | ```
83 | $ go build -ldflags="-s -w" -o ./bin/main . # ./bin/main to run the application / Ctrl + C to stop the application
84 | ```
85 |
86 | >[!TIP]
87 | >***In order to have autocompletion and syntax highlighting in VS Code for the Teml templating language, you will have to install the [templ-vscode](https://marketplace.visualstudio.com/items?itemName=a-h.templ) extension (for vim/nvim install this [plugin](https://github.com/joerdav/templ.vim)). To generate the Go code corresponding to these templates you will have to install the templ CLI:***
88 |
89 | ```
90 | $ go install github.com/a-h/templ/cmd/templ@latest
91 | ```
92 |
93 | >[!TIP]
94 | >***And then regenerate the templates with the command:***
95 |
96 | ```
97 | $ templ generate # The `templ generate --watch` command will watch the project folder to regenerate them every time we make a change to its code.
98 | ```
99 |
100 | >[!TIP]
101 | >***Review the documentation on Templ [installation](https://templ.guide/quick-start/installation) and [support](https://templ.guide/commands-and-tools/ide-support/) for your IDE.***
102 |
103 | ---
104 |
105 | ### Happy coding 😀!!
--------------------------------------------------------------------------------
/go.sum:
--------------------------------------------------------------------------------
1 | github.com/a-h/templ v0.2.476 h1:+H4hP4CwK4kfJwXsE6kHeFWMGtcVOVoOm/I64uzARBk=
2 | github.com/a-h/templ v0.2.476/go.mod h1:zQ95mSyadNTGHv6k5Fm+wQU8zkBMMbHCHg7eAvUZKNM=
3 | github.com/a-h/templ v0.2.747 h1:D0dQ2lxC3W7Dxl6fxQ/1zZHBQslSkTSvl5FxP/CfdKg=
4 | github.com/a-h/templ v0.2.747/go.mod h1:69ObQIbrcuwPCU32ohNaWce3Cb7qM5GMiqN1K+2yop4=
5 | github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
6 | github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
7 | github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
8 | github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
9 | github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
10 | github.com/gofiber/fiber/v2 v2.36.0/go.mod h1:tgCr+lierLwLoVHHO/jn3Niannv34WRkQETU8wiL9fQ=
11 | github.com/gofiber/fiber/v2 v2.51.0 h1:JNACcZy5e2tGApWB2QrRpenTWn0fq0hkFm6k0C86gKQ=
12 | github.com/gofiber/fiber/v2 v2.51.0/go.mod h1:xaQRZQJGqnKOQnbQw+ltvku3/h8QxvNi8o6JiJ7Ll0U=
13 | github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
14 | github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
15 | github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
16 | github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
17 | github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
18 | github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
19 | github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
20 | github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
21 | github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
22 | github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
23 | github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
24 | github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
25 | github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
26 | github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
27 | github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
28 | github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
29 | github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
30 | github.com/mattn/go-sqlite3 v1.14.18 h1:JL0eqdCOq6DJVNPSvArO/bIV9/P7fbGrV00LZHc+5aI=
31 | github.com/mattn/go-sqlite3 v1.14.18/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
32 | github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
33 | github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
34 | github.com/sujit-baniya/flash v0.1.8 h1:BwcrybCatPU30VMA9IBA5q3ZE0VSr5c7qTqwZrSvyRI=
35 | github.com/sujit-baniya/flash v0.1.8/go.mod h1:kmlAIkLDMlLshEeeE6fETEW8kSOopKN5WA3KXLmS/U0=
36 | github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
37 | github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
38 | github.com/valyala/fasthttp v1.38.0/go.mod h1:t/G+3rLek+CyY9bnIE+YlMRddxVAAGjhxndDB4i4C0I=
39 | github.com/valyala/fasthttp v1.50.0 h1:H7fweIlBm0rXLs2q0XbalvJ6r0CUPFWK3/bB4N13e9M=
40 | github.com/valyala/fasthttp v1.50.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA=
41 | github.com/valyala/fasthttp v1.55.0 h1:Zkefzgt6a7+bVKHnu/YaYSOPfNYNisSVBo/unVCf8k8=
42 | github.com/valyala/fasthttp v1.55.0/go.mod h1:NkY9JtkrpPKmgwV3HTaS2HWaJss9RSIsRVfcxxoHiOM=
43 | github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
44 | github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
45 | golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
46 | golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY=
47 | golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
48 | golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
49 | golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
50 | golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
51 | golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
52 | golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
53 | golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
54 | golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
55 | golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
56 | golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
57 | golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
58 | golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
59 | golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
60 | golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
61 | golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
62 | golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
63 | golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
64 | golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
65 | golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
66 | golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
67 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
68 |
--------------------------------------------------------------------------------
/handlers/auth_handlers.go:
--------------------------------------------------------------------------------
1 | package handlers
2 |
3 | import (
4 | "errors"
5 | "fmt"
6 | "strings"
7 |
8 | "github.com/a-h/templ"
9 | "github.com/emarifer/gofiber-templ-htmx/models"
10 | "github.com/emarifer/gofiber-templ-htmx/views"
11 | "github.com/emarifer/gofiber-templ-htmx/views/auth_views"
12 | "github.com/gofiber/fiber/v2"
13 | "github.com/gofiber/fiber/v2/middleware/adaptor"
14 | "github.com/sujit-baniya/flash"
15 | "golang.org/x/crypto/bcrypt"
16 | )
17 |
18 | /********** Handlers for Auth Views **********/
19 |
20 | // Render Home Page
21 | func HandleViewHome(c *fiber.Ctx) error {
22 | fromProtected := c.Locals(FROM_PROTECTED).(bool)
23 |
24 | hindex := views.HomeIndex(fromProtected)
25 | home := views.Home("", fromProtected, false, flash.Get(c), hindex)
26 |
27 | handler := adaptor.HTTPHandler(templ.Handler(home))
28 |
29 | return handler(c)
30 | }
31 |
32 | // Render Login Page with success/error messages & session management
33 | func HandleViewLogin(c *fiber.Ctx) error {
34 | fromProtected := c.Locals(FROM_PROTECTED).(bool)
35 |
36 | lindex := auth_views.LoginIndex(fromProtected)
37 | login := auth_views.Login(
38 | " | Login", fromProtected, false, flash.Get(c), lindex,
39 | )
40 |
41 | handler := adaptor.HTTPHandler(templ.Handler(login))
42 |
43 | if c.Method() == "POST" {
44 | // obtaining the time zone from the POST request of the login form
45 | tzone := ""
46 | if len(c.GetReqHeaders()["X-Timezone"]) != 0 {
47 | tzone = c.GetReqHeaders()["X-Timezone"][0]
48 | // fmt.Println("Tzone:", tzone)
49 | }
50 |
51 | var (
52 | user models.User
53 | err error
54 | )
55 | fm := fiber.Map{
56 | "type": "error",
57 | }
58 |
59 | // notice: in production you should not inform the user
60 | // with detailed messages about login failures
61 | if user, err = models.CheckEmail(c.FormValue("email")); err != nil {
62 | // fmt.Println(err)
63 | if strings.Contains(err.Error(), "no such table") ||
64 | strings.Contains(err.Error(), "database is locked") {
65 | // "no such table" is the error that SQLite3 produces
66 | // when some table does not exist, and we have only
67 | // used it as an example of the errors that can be caught.
68 | // Here you can add the errors that you are interested
69 | // in throwing as `500` codes.
70 | return fiber.NewError(
71 | fiber.StatusServiceUnavailable,
72 | "database temporarily out of service",
73 | )
74 | }
75 | fm["message"] = "There is no user with that email"
76 |
77 | return flash.WithError(c, fm).Redirect("/login")
78 | }
79 |
80 | err = bcrypt.CompareHashAndPassword(
81 | []byte(user.Password),
82 | []byte(c.FormValue("password")),
83 | )
84 | if err != nil {
85 | fm["message"] = "Incorrect password"
86 |
87 | return flash.WithError(c, fm).Redirect("/login")
88 | }
89 |
90 | session, err := store.Get(c)
91 | if err != nil {
92 | fm["message"] = fmt.Sprintf("something went wrong: %s", err)
93 |
94 | return flash.WithError(c, fm).Redirect("/login")
95 | }
96 |
97 | session.Set(AUTH_KEY, true)
98 | session.Set(USER_ID, user.ID)
99 | session.Set(TZONE_KEY, tzone)
100 |
101 | err = session.Save()
102 | if err != nil {
103 | fm["message"] = fmt.Sprintf("something went wrong: %s", err)
104 |
105 | return flash.WithError(c, fm).Redirect("/login")
106 | }
107 |
108 | fm = fiber.Map{
109 | "type": "success",
110 | "message": "You have successfully logged in!!",
111 | }
112 |
113 | return flash.WithSuccess(c, fm).Redirect("/todo/list")
114 | }
115 |
116 | return handler(c)
117 | }
118 |
119 | // Render Register Page with success/error messages
120 | func HandleViewRegister(c *fiber.Ctx) error {
121 | fromProtected := c.Locals(FROM_PROTECTED).(bool)
122 |
123 | rindex := auth_views.RegisterIndex(fromProtected)
124 | register := auth_views.Register(
125 | " | Register", fromProtected, false, flash.Get(c), rindex,
126 | )
127 |
128 | handler := adaptor.HTTPHandler(templ.Handler(register))
129 |
130 | if c.Method() == "POST" {
131 | user := models.User{
132 | Email: c.FormValue("email"),
133 | Password: c.FormValue("password"),
134 | Username: c.FormValue("username"),
135 | }
136 |
137 | err := models.CreateUser(user)
138 | if err != nil {
139 | if strings.Contains(err.Error(), "no such table") ||
140 | strings.Contains(err.Error(), "database is locked") {
141 | // "no such table" is the error that SQLite3 produces
142 | // when some table does not exist, and we have only
143 | // used it as an example of the errors that can be caught.
144 | // Here you can add the errors that you are interested
145 | // in throwing as `500` codes.
146 | return fiber.NewError(
147 | fiber.StatusServiceUnavailable,
148 | "database temporarily out of service",
149 | )
150 | }
151 | if strings.Contains(err.Error(), "UNIQUE constraint failed") {
152 | err = errors.New("the email is already in use")
153 | }
154 | fm := fiber.Map{
155 | "type": "error",
156 | "message": fmt.Sprintf("something went wrong: %s", err),
157 | }
158 |
159 | return flash.WithError(c, fm).Redirect("/register")
160 | }
161 |
162 | fm := fiber.Map{
163 | "type": "success",
164 | "message": "You have successfully registered!!",
165 | }
166 |
167 | return flash.WithSuccess(c, fm).Redirect("/login")
168 | }
169 |
170 | return handler(c)
171 | }
172 |
173 | // Authentication Middleware
174 | func AuthMiddleware(c *fiber.Ctx) error {
175 | fm := fiber.Map{
176 | "type": "error",
177 | }
178 |
179 | session, err := store.Get(c)
180 | if err != nil {
181 | fm["message"] = "You are not authorized"
182 |
183 | return flash.WithError(c, fm).Redirect("/login")
184 | }
185 |
186 | if session.Get(AUTH_KEY) == nil {
187 | fm["message"] = "You are not authorized"
188 |
189 | return flash.WithError(c, fm).Redirect("/login")
190 | }
191 |
192 | userId := session.Get(USER_ID)
193 | if userId == nil {
194 | fm["message"] = "You are not authorized"
195 |
196 | return flash.WithError(c, fm).Redirect("/login")
197 | }
198 |
199 | user, err := models.GetUserById(fmt.Sprint(userId.(uint64)))
200 | if err != nil {
201 | fm["message"] = "You are not authorized"
202 |
203 | return flash.WithError(c, fm).Redirect("/login")
204 | }
205 |
206 | c.Locals("userId", userId)
207 | c.Locals("username", user.Username)
208 | c.Locals(FROM_PROTECTED, true)
209 | // fromProtected = true
210 |
211 | return c.Next()
212 | }
213 |
214 | // Logout Handler
215 | func HandleLogout(c *fiber.Ctx) error {
216 | fm := fiber.Map{
217 | "type": "error",
218 | }
219 |
220 | session, err := store.Get(c)
221 | if err != nil {
222 | fm["message"] = "logged out (no session)"
223 |
224 | return flash.WithError(c, fm).Redirect("/login")
225 | }
226 |
227 | err = session.Destroy()
228 | if err != nil {
229 | fm["message"] = fmt.Sprintf("something went wrong: %s", err)
230 |
231 | return flash.WithError(c, fm).Redirect("/login")
232 | }
233 |
234 | fm = fiber.Map{
235 | "type": "success",
236 | "message": "You have successfully logged out!!",
237 | }
238 |
239 | c.Locals(FROM_PROTECTED, false)
240 | // fromProtected = false
241 |
242 | return flash.WithSuccess(c, fm).Redirect("/login")
243 | }
244 |
--------------------------------------------------------------------------------
/handlers/todo_handlers.go:
--------------------------------------------------------------------------------
1 | package handlers
2 |
3 | import (
4 | "fmt"
5 | "strconv"
6 | "strings"
7 |
8 | "github.com/a-h/templ"
9 | "github.com/emarifer/gofiber-templ-htmx/models"
10 | "github.com/emarifer/gofiber-templ-htmx/views/todo_views"
11 | "github.com/gofiber/fiber/v2"
12 | "github.com/gofiber/fiber/v2/middleware/adaptor"
13 | "github.com/sujit-baniya/flash"
14 | )
15 |
16 | /********** Handlers for Todo Views **********/
17 |
18 | // Render List Page with success/error messages
19 | func HandleViewList(c *fiber.Ctx) error {
20 | fromProtected := c.Locals(FROM_PROTECTED).(bool)
21 |
22 | todo := new(models.Todo)
23 | todo.CreatedBy = c.Locals("userId").(uint64)
24 |
25 | // fm := fiber.Map{"type": "error"}
26 |
27 | todosSlice, err := todo.GetAllTodos()
28 | if err != nil {
29 | if strings.Contains(err.Error(), "no such table") ||
30 | strings.Contains(err.Error(), "database is locked") {
31 | // "no such table" is the error that SQLite3 produces
32 | // when some table does not exist, and we have only
33 | // used it as an example of the errors that can be caught.
34 | // Here you can add the errors that you are interested
35 | // in throwing as `500` codes.
36 | return fiber.NewError(
37 | fiber.StatusServiceUnavailable,
38 | "database temporarily out of service",
39 | )
40 | }
41 | // fm["message"] = fmt.Sprintf("something went wrong: %s", err)
42 |
43 | // return flash.WithError(c, fm).Redirect("/todo/list")
44 | }
45 |
46 | tindex := todo_views.TodoIndex(todosSlice)
47 | tlist := todo_views.TodoList(
48 | " | Tasks List",
49 | fromProtected,
50 | false,
51 | flash.Get(c),
52 | c.Locals("username").(string),
53 | tindex,
54 | )
55 |
56 | handler := adaptor.HTTPHandler(templ.Handler(tlist))
57 |
58 | return handler(c)
59 | }
60 |
61 | // Render Create Todo Page with success/error messages
62 | func HandleViewCreatePage(c *fiber.Ctx) error {
63 | fromProtected := c.Locals(FROM_PROTECTED).(bool)
64 |
65 | if c.Method() == "POST" {
66 | newTodo := new(models.Todo)
67 | newTodo.CreatedBy = c.Locals("userId").(uint64)
68 | newTodo.Title = strings.Trim(c.FormValue("title"), " ")
69 | newTodo.Description = strings.Trim(c.FormValue("description"), " ")
70 |
71 | fm := fiber.Map{
72 | "type": "error",
73 | "message": "Task title empty!!",
74 | }
75 | if newTodo.Title == "" {
76 |
77 | return flash.WithError(c, fm).Redirect("/todo/list")
78 | }
79 |
80 | if _, err := newTodo.CreateTodo(); err != nil {
81 | if strings.Contains(err.Error(), "no such table") ||
82 | strings.Contains(err.Error(), "database is locked") {
83 | // "no such table" is the error that SQLite3 produces
84 | // when some table does not exist, and we have only
85 | // used it as an example of the errors that can be caught.
86 | // Here you can add the errors that you are interested
87 | // in throwing as `500` codes.
88 | return fiber.NewError(
89 | fiber.StatusServiceUnavailable,
90 | "database temporarily out of service",
91 | )
92 | }
93 | }
94 |
95 | fm = fiber.Map{
96 | "type": "success",
97 | "message": "Task successfully created!!",
98 | }
99 |
100 | return flash.WithSuccess(c, fm).Redirect("/todo/list")
101 | }
102 |
103 | cindex := todo_views.CreateIndex()
104 | create := todo_views.Create(
105 | " | Create Todo",
106 | fromProtected,
107 | false,
108 | flash.Get(c),
109 | c.Locals("username").(string),
110 | cindex,
111 | )
112 |
113 | handler := adaptor.HTTPHandler(templ.Handler(create))
114 |
115 | return handler(c)
116 | }
117 |
118 | // Render Edit Todo Page with success/error messages
119 | func HandleViewEditPage(c *fiber.Ctx) error {
120 | fromProtected := c.Locals(FROM_PROTECTED).(bool)
121 | session, _ := store.Get(c)
122 | tzone := session.Get(TZONE_KEY).(string)
123 |
124 | idParams, _ := strconv.Atoi(c.Params("id"))
125 | todoId := uint64(idParams)
126 |
127 | todo := new(models.Todo)
128 | todo.ID = todoId
129 | todo.CreatedBy = c.Locals("userId").(uint64)
130 |
131 | fm := fiber.Map{"type": "error"}
132 |
133 | recoveredTodo, err := todo.GetNoteById()
134 |
135 | if err != nil {
136 | if strings.Contains(err.Error(), "no such table") ||
137 | strings.Contains(err.Error(), "database is locked") {
138 | // "no such table" is the error that SQLite3 produces
139 | // when some table does not exist, and we have only
140 | // used it as an example of the errors that can be caught.
141 | // Here you can add the errors that you are interested
142 | // in throwing as `500` codes.
143 | return fiber.NewError(
144 | fiber.StatusServiceUnavailable,
145 | "database temporarily out of service",
146 | )
147 | }
148 |
149 | fm["message"] = fmt.Sprintf("something went wrong: %s", err)
150 |
151 | return flash.WithError(c, fm).Redirect("/todo/list")
152 | }
153 |
154 | if c.Method() == "POST" {
155 | todo.Title = strings.Trim(c.FormValue("title"), " ")
156 | todo.Description = strings.Trim(c.FormValue("description"), " ")
157 | if c.FormValue("status") == "on" {
158 | todo.Status = true
159 | } else {
160 | todo.Status = false
161 | }
162 |
163 | fm = fiber.Map{
164 | "type": "error",
165 | "message": "Task title empty!!",
166 | }
167 | if todo.Title == "" {
168 |
169 | return flash.WithError(c, fm).Redirect("/todo/list")
170 | }
171 |
172 | _, err := todo.UpdateTodo()
173 | if err != nil {
174 | if strings.Contains(err.Error(), "no such table") ||
175 | strings.Contains(err.Error(), "database is locked") {
176 | // "no such table" is the error that SQLite3 produces
177 | // when some table does not exist, and we have only
178 | // used it as an example of the errors that can be caught.
179 | // Here you can add the errors that you are interested
180 | // in throwing as `500` codes.
181 | return fiber.NewError(
182 | fiber.StatusServiceUnavailable,
183 | "database temporarily out of service",
184 | )
185 | }
186 |
187 | fm["message"] = fmt.Sprintf("something went wrong: %s", err)
188 |
189 | return flash.WithError(c, fm).Redirect("/todo/list")
190 | }
191 |
192 | fm = fiber.Map{
193 | "type": "success",
194 | "message": "Task successfully updated!!",
195 | }
196 |
197 | return flash.WithSuccess(c, fm).Redirect("/todo/list")
198 | }
199 |
200 | uindex := todo_views.UpdateIndex(recoveredTodo, tzone)
201 | update := todo_views.Update(
202 | fmt.Sprintf(" | Edit Todo #%d", recoveredTodo.ID),
203 | fromProtected,
204 | false,
205 | flash.Get(c),
206 | c.Locals("username").(string),
207 | uindex,
208 | )
209 |
210 | handler := adaptor.HTTPHandler(templ.Handler(update))
211 |
212 | return handler(c)
213 | }
214 |
215 | // Handler Remove Todo
216 | func HandleDeleteTodo(c *fiber.Ctx) error {
217 | idParams, _ := strconv.Atoi(c.Params("id"))
218 | todoId := uint64(idParams)
219 |
220 | todo := new(models.Todo)
221 | todo.ID = todoId
222 | todo.CreatedBy = c.Locals("userId").(uint64)
223 |
224 | fm := fiber.Map{"type": "error"}
225 |
226 | if err := todo.DeleteTodo(); err != nil {
227 | if strings.Contains(err.Error(), "no such table") ||
228 | strings.Contains(err.Error(), "database is locked") {
229 | // "no such table" is the error that SQLite3 produces
230 | // when some table does not exist, and we have only
231 | // used it as an example of the errors that can be caught.
232 | // Here you can add the errors that you are interested
233 | // in throwing as `500` codes.
234 | return fiber.NewError(
235 | fiber.StatusServiceUnavailable,
236 | "database temporarily out of service",
237 | )
238 | }
239 | fm["message"] = fmt.Sprintf("something went wrong: %s", err)
240 |
241 | return flash.WithError(c, fm).Redirect(
242 | "/todo/list",
243 | fiber.StatusSeeOther,
244 | )
245 | }
246 |
247 | fm = fiber.Map{
248 | "type": "success",
249 | "message": "Task successfully deleted!!",
250 | }
251 |
252 | return flash.WithSuccess(c, fm).Redirect("/todo/list", fiber.StatusSeeOther)
253 | }
254 |
--------------------------------------------------------------------------------
/assets/img/gopher-svgrepo-com.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------