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" "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 { func SendRegistrationEmail(recipient, username, password string) bool {
data := struct { data := struct {
SiteName string SiteName string

13
main.go
View File

@ -128,19 +128,8 @@ func Get(username string) (*UserData, bool) {
log.Printf("scsusers.Get: %s", err.Error()) log.Printf("scsusers.Get: %s", err.Error())
return nil, false 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() { return u, u.LoadMeta()
var m metadata
rows.Scan(m.Key, m.Value, m.ID)
u.Meta[m.Key] = m
}
return u, true
} }
func GetUserid(username string) int64 { func GetUserid(username string) int64 {

19
meta.go
View File

@ -1,6 +1,7 @@
package scsusers package scsusers
import ( import (
"database/sql"
"fmt" "fmt"
"log" "log"
) )
@ -10,6 +11,24 @@ type Metadata struct {
MetaValue string `db:"meta_value"` 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) { func (u *UserData) Get(key string) (string, bool) {
tmp, ok := u.Meta[key] tmp, ok := u.Meta[key]
return tmp.Value, ok 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))
}