Adding response to 2FA

This commit is contained in:
2024-06-19 21:34:50 +07:00
parent b890f5442c
commit c1059072a6
7 changed files with 141 additions and 0 deletions

47
handler/auth/totp/totp.go Normal file
View File

@ -0,0 +1,47 @@
package totpHandler
import (
"fmt"
"github.com/fossyy/filekeeper/session"
"github.com/fossyy/filekeeper/types"
totpView "github.com/fossyy/filekeeper/view/totp"
"github.com/xlzd/gotp"
"net/http"
"time"
)
func GET(w http.ResponseWriter, r *http.Request) {
component := totpView.Main("Filekeeper - 2FA Page")
err := component.Render(r.Context(), w)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
return
}
func POST(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
code := r.Form.Get("code")
_, user, key := session.GetSession(r)
totp := gotp.NewDefaultTOTP(user.Totp)
if totp.Verify(code, time.Now().Unix()) {
storeSession, err := session.Get(key)
if err != nil {
return
}
fmt.Println(storeSession)
storeSession.Values["user"] = types.User{
UserID: user.UserID,
Email: user.Email,
Username: user.Username,
Totp: "",
Authenticated: true,
}
http.Redirect(w, r, "/user", http.StatusFound)
return
} else {
fmt.Fprint(w, "wrong")
}
}