Add Redis support for session and user management

This commit is contained in:
2024-09-08 00:03:43 +07:00
parent b9ac29d301
commit 2c5de2b336
26 changed files with 642 additions and 548 deletions

View File

@ -2,7 +2,6 @@ package userHandlerResetPassword
import (
"github.com/fossyy/filekeeper/app"
"github.com/fossyy/filekeeper/cache"
"github.com/fossyy/filekeeper/session"
"github.com/fossyy/filekeeper/types"
"github.com/fossyy/filekeeper/utils"
@ -14,7 +13,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
userSession := r.Context().Value("user").(types.User)
currentPassword := r.Form.Get("currentPassword")
password := r.Form.Get("password")
user, err := cache.GetUser(userSession.Email)
user, err := app.Server.Service.GetUser(r.Context(), userSession.Email)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
@ -38,7 +37,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
}
session.RemoveAllSessions(userSession.Email)
cache.DeleteUser(userSession.Email)
app.Server.Service.DeleteUser(userSession.Email)
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return

View File

@ -11,15 +11,21 @@ func DELETE(w http.ResponseWriter, r *http.Request) {
_, mySession, _ := session.GetSession(r)
otherSession, _ := session.Get(id)
if session.GetSessionInfo(mySession.Email, otherSession.ID) == nil {
if _, err := session.GetSessionInfo(mySession.Email, otherSession.ID); err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
otherSession.Delete()
session.RemoveSessionInfo(mySession.Email, otherSession.ID)
component := userView.SessionTable(session.GetSessions(mySession.Email))
sessions, err := session.GetSessions(mySession.Email)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
component := userView.SessionTable(sessions)
err := component.Render(r.Context(), w)
err = component.Render(r.Context(), w)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return

View File

@ -5,7 +5,6 @@ import (
"encoding/base64"
"fmt"
"github.com/fossyy/filekeeper/app"
"github.com/fossyy/filekeeper/cache"
"github.com/fossyy/filekeeper/view/client/user/totp"
"image/png"
"net/http"
@ -75,7 +74,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
return
}
cache.DeleteUser(userSession.Email)
app.Server.Service.DeleteUser(userSession.Email)
component := userTotpSetupView.Main("Filekeeper - 2FA Setup Page", base64Str, secret, userSession, types.Message{
Code: 1,
Message: "Your TOTP setup is complete! Your account is now more secure.",

View File

@ -17,24 +17,28 @@ var errorMessages = map[string]string{
func GET(w http.ResponseWriter, r *http.Request) {
var component templ.Component
userSession := r.Context().Value("user").(types.User)
sessions, err := session.GetSessions(userSession.Email)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if err := r.URL.Query().Get("error"); err != "" {
message, ok := errorMessages[err]
if !ok {
message = "Unknown error occurred. Please contact support at bagas@fossy.my.id for assistance."
}
component = userView.Main("Filekeeper - User Page", userSession, session.GetSessions(userSession.Email), types.Message{
component = userView.Main("Filekeeper - User Page", userSession, sessions, types.Message{
Code: 0,
Message: message,
})
} else {
component = userView.Main("Filekeeper - User Page", userSession, session.GetSessions(userSession.Email), types.Message{
component = userView.Main("Filekeeper - User Page", userSession, sessions, types.Message{
Code: 1,
Message: "",
})
}
err := component.Render(r.Context(), w)
err = component.Render(r.Context(), w)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
app.Server.Logger.Error(err.Error())