Add Redis support for session and user management
This commit is contained in:
@ -1,14 +1,16 @@
|
||||
package forgotPasswordVerifyHandler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/fossyy/filekeeper/app"
|
||||
"github.com/fossyy/filekeeper/cache"
|
||||
forgotPasswordHandler "github.com/fossyy/filekeeper/handler/forgotPassword"
|
||||
"github.com/fossyy/filekeeper/session"
|
||||
"github.com/fossyy/filekeeper/types"
|
||||
"github.com/fossyy/filekeeper/utils"
|
||||
"github.com/fossyy/filekeeper/view/client/forgotPassword"
|
||||
signupView "github.com/fossyy/filekeeper/view/client/signup"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
@ -21,11 +23,13 @@ func init() {
|
||||
func GET(w http.ResponseWriter, r *http.Request) {
|
||||
code := r.PathValue("code")
|
||||
|
||||
email := forgotPasswordHandler.UserForgotPassword[code]
|
||||
_, ok := forgotPasswordHandler.ListForgotPassword[email]
|
||||
|
||||
if !ok {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
_, err := app.Server.Cache.GetCache(r.Context(), "ForgotPassword:"+code)
|
||||
if err != nil {
|
||||
if errors.Is(err, redis.Nil) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
@ -33,7 +37,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
|
||||
Code: 3,
|
||||
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())
|
||||
@ -44,15 +48,20 @@ func GET(w http.ResponseWriter, r *http.Request) {
|
||||
func POST(w http.ResponseWriter, r *http.Request) {
|
||||
code := r.PathValue("code")
|
||||
|
||||
email := forgotPasswordHandler.UserForgotPassword[code]
|
||||
data, ok := forgotPasswordHandler.ListForgotPassword[email]
|
||||
|
||||
if !ok {
|
||||
data, err := app.Server.Cache.GetCache(r.Context(), "ForgotPassword:"+code)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
var userData *forgotPasswordHandler.ForgotPassword
|
||||
|
||||
err := r.ParseForm()
|
||||
err = json.Unmarshal([]byte(data), &userData)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = r.ParseForm()
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
app.Server.Logger.Error(err.Error())
|
||||
@ -82,19 +91,19 @@ func POST(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
err = app.Server.Database.UpdateUserPassword(data.User.Email, hashedPassword)
|
||||
err = app.Server.Database.UpdateUserPassword(userData.User.Email, hashedPassword)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
app.Server.Logger.Error(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
delete(forgotPasswordHandler.ListForgotPassword, data.User.Email)
|
||||
delete(forgotPasswordHandler.UserForgotPassword, data.Code)
|
||||
app.Server.Cache.DeleteCache(r.Context(), "ForgotPasswordCode:"+userData.User.Email)
|
||||
app.Server.Cache.DeleteCache(r.Context(), "ForgotPassword:"+code)
|
||||
|
||||
session.RemoveAllSessions(data.User.Email)
|
||||
session.RemoveAllSessions(userData.User.Email)
|
||||
|
||||
cache.DeleteUser(data.User.Email)
|
||||
app.Server.Service.DeleteUser(userData.User.Email)
|
||||
|
||||
component := forgotPasswordView.ChangeSuccess("Filekeeper - Forgot Password Page")
|
||||
err = component.Render(r.Context(), w)
|
||||
|
Reference in New Issue
Block a user