databag/net/server/internal/api_addAccountAuthentication.go

42 lines
834 B
Go
Raw Normal View History

2022-03-08 18:18:31 +00:00
package databag
import (
"net/http"
"time"
"encoding/hex"
"databag/internal/store"
"github.com/theckman/go-securerandom"
)
func AddAccountAuthentication(w http.ResponseWriter, r *http.Request) {
2022-03-08 21:31:04 +00:00
account, err := AccountLogin(r)
2022-03-08 18:18:31 +00:00
if err != nil {
ErrResponse(w, http.StatusUnauthorized, err)
return
}
2022-03-08 21:31:04 +00:00
data, res := securerandom.Bytes(APP_RESETSIZE)
2022-03-08 18:18:31 +00:00
if res != nil {
ErrResponse(w, http.StatusInternalServerError, res)
return
}
token := hex.EncodeToString(data)
accountToken := store.AccountToken{
2022-03-08 21:31:04 +00:00
AccountID: account.ID,
TokenType: APP_TOKENRESET,
2022-03-08 18:18:31 +00:00
Token: token,
Expires: time.Now().Unix() + APP_RESETEXPIRE,
}
if err := store.DB.Create(&accountToken).Error; err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
WriteResponse(w, token)
}