databag/net/server/internal/api_addAccountApp.go

40 lines
793 B
Go
Raw Normal View History

2022-01-18 05:48:42 +00:00
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 {
2022-01-19 20:07:57 +00:00
ErrResponse(w, http.StatusUnauthorized, err)
2022-01-18 05:48:42 +00:00
return
}
data, res := securerandom.Bytes(4)
if res != nil {
2022-01-19 20:07:57 +00:00
ErrResponse(w, http.StatusInternalServerError, res)
2022-01-18 05:48:42 +00:00
return
}
2022-01-18 06:56:00 +00:00
token := hex.EncodeToString(data)
2022-01-18 05:48:42 +00:00
2022-01-18 06:56:00 +00:00
accountToken := store.AccountToken{
2022-01-18 05:48:42 +00:00
AccountID: id,
TokenType: "attach",
2022-01-18 06:56:00 +00:00
Token: token,
2022-01-18 05:48:42 +00:00
Expires: time.Now().Unix() + APP_ATTACHEXPIRE,
};
2022-01-19 20:07:57 +00:00
if err := store.DB.Create(&accountToken).Error; err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
2022-01-18 05:48:42 +00:00
return
}
2022-01-18 06:56:00 +00:00
WriteResponse(w, token);
2022-01-18 05:48:42 +00:00
}