Finalize 2FA implementation

This commit is contained in:
2024-06-20 08:49:49 +07:00
parent 0e16d59df9
commit c5185d5107
8 changed files with 87 additions and 77 deletions

View File

@ -3,17 +3,14 @@ package signinHandler
import (
"errors"
"github.com/a-h/templ"
"github.com/fossyy/filekeeper/db"
totpHandler "github.com/fossyy/filekeeper/handler/auth/totp"
"net/http"
"strings"
"time"
"github.com/fossyy/filekeeper/cache"
"github.com/fossyy/filekeeper/logger"
"github.com/fossyy/filekeeper/session"
"github.com/fossyy/filekeeper/types"
"github.com/fossyy/filekeeper/utils"
signinView "github.com/fossyy/filekeeper/view/signin"
"net/http"
"strings"
)
var log *logger.AggregatedLogger
@ -81,7 +78,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
}
email := r.Form.Get("email")
password := r.Form.Get("password")
userData, err := db.DB.GetUser(email)
userData, err := cache.GetUser(email)
if err != nil {
component := signinView.Main("Filekeeper - Sign in Page", types.Message{
Code: 0,
@ -99,16 +96,16 @@ func POST(w http.ResponseWriter, r *http.Request) {
if email == userData.Email && utils.CheckPasswordHash(password, userData.Password) {
if userData.Totp != "" {
id := utils.GenerateRandomString(32)
totpHandler.TotpInfoList[id] = &totpHandler.TotpInfo{
ID: id,
UserID: userData.UserID,
Secret: userData.Totp,
Email: userData.Email,
Username: userData.Username,
CreateTime: time.Now(),
storeSession := session.Create()
storeSession.Values["user"] = types.User{
UserID: userData.UserID,
Email: email,
Username: userData.Username,
Totp: userData.Totp,
Authenticated: false,
}
http.Redirect(w, r, "/auth/totp/"+id, http.StatusSeeOther)
storeSession.Save(w)
http.Redirect(w, r, "/auth/totp", http.StatusSeeOther)
return
}