This commit is contained in:
Your Name 2023-10-07 09:48:40 -04:00
parent 29e82ce611
commit 21f094358a
4 changed files with 44 additions and 27 deletions

View File

@ -9,21 +9,6 @@ import (
"time"
)
func RecoverByEmail(email string) {
u, ok := Get(email)
if !ok {
return
}
recoverycode := generatePassword(16)
u.Delete("recoverycode")
u.Delete("recoverytime")
u.Set("recoverycode", string(recoverycode))
u.Set("recoverytime", fmt.Sprintf("%d", time.Now().Add(time.Minute*60).Unix()))
SendRecoveryEmail(email, email, string(recoverycode))
}
func SendRegistrationEmail(recipient, username, password string) bool {
data := struct {
SiteName string

13
main.go
View File

@ -128,19 +128,8 @@ func Get(username string) (*UserData, bool) {
log.Printf("scsusers.Get: %s", err.Error())
return nil, false
}
q = fmt.Sprintf("select meta_key, meta_value, id from %s_meta where user=?", c.TablePrefix)
rows, err := c.db.Query(q, u.UserID)
if err != nil && err != sql.ErrNoRows {
log.Printf("scsuser.Get: select: %s", err.Error())
return u, false
}
for rows.Next() {
var m metadata
rows.Scan(m.Key, m.Value, m.ID)
u.Meta[m.Key] = m
}
return u, true
return u, u.LoadMeta()
}
func GetUserid(username string) int64 {

19
meta.go
View File

@ -1,6 +1,7 @@
package scsusers
import (
"database/sql"
"fmt"
"log"
)
@ -10,6 +11,24 @@ type Metadata struct {
MetaValue string `db:"meta_value"`
}
func (u *UserData) LoadMeta() bool {
q:= fmt.Sprintf("select meta_key, meta_value, id from %s_meta where user=?", c.TablePrefix)
rows, err := c.db.Query(q, u.UserID)
if err != nil && err != sql.ErrNoRows {
log.Printf("scsuser.Get: select: %s", err.Error())
return false
}
for rows.Next() {
var m metadata
rows.Scan(m.Key, m.Value, m.ID)
u.Meta[m.Key] = m
log.Printf("Loaded meta key %s id %d value %s", m.Key, m.ID, m.Value)
}
return true
}
func (u *UserData) Get(key string) (string, bool) {
tmp, ok := u.Meta[key]
return tmp.Value, ok

24
recovery.go Normal file
View File

@ -0,0 +1,24 @@
package scsusers
import (
"fmt"
"log"
"time"
)
func RecoverByEmail(email string) {
u, ok := Get(email)
if !ok {
return
}
log.Printf("%#v", u)
recoverycode := generatePassword(16)
u.Delete("recoverycode")
u.Delete("recoverytime")
u.Set("recoverycode", string(recoverycode))
u.Set("recoverytime", fmt.Sprintf("%d", time.Now().Add(time.Minute*60).Unix()))
SendRecoveryEmail(email, email, string(recoverycode))
}