mirror of
https://github.com/balzack/databag.git
synced 2025-02-12 03:29:16 +00:00
40 lines
800 B
Go
40 lines
800 B
Go
package databag
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
"encoding/hex"
|
|
"databag/internal/store"
|
|
"github.com/theckman/go-securerandom"
|
|
)
|
|
|
|
func AddAccountApp(w http.ResponseWriter, r *http.Request) {
|
|
|
|
id, err := AccountLogin(r)
|
|
if err != nil {
|
|
ErrResponse(w, http.StatusUnauthorized, err)
|
|
return
|
|
}
|
|
|
|
data, res := securerandom.Bytes(4)
|
|
if res != nil {
|
|
ErrResponse(w, http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
token := hex.EncodeToString(data)
|
|
|
|
accountToken := store.AccountToken{
|
|
AccountID: id,
|
|
TokenType: APP_ACCOUNTATTACH,
|
|
Token: token,
|
|
Expires: time.Now().Unix() + APP_ATTACHEXPIRE,
|
|
}
|
|
if err := store.DB.Create(&accountToken).Error; err != nil {
|
|
ErrResponse(w, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
WriteResponse(w, token)
|
|
}
|
|
|