reset token updates

This commit is contained in:
Cam 2024-01-31 14:35:28 -06:00
parent bba9377b9f
commit 6a29ac0117
No known key found for this signature in database
GPG Key ID: 367B7C7EBD84A8BD
8 changed files with 132 additions and 9 deletions

View File

@ -37,6 +37,35 @@ func (handler *resetTokenHandler) Handle(params account.ResetTokenParams) middle
}
// Need to create new token and invalidate all other resources
token, err := createToken()
if err != nil {
logrus.Errorf("error creating token for request '%v': %v", params.Body.EmailAddress, err)
return account.NewResetTokenInternalServerError()
}
a.Token = token
if _, err := str.UpdateAccount(a, tx); err != nil {
logrus.Errorf("error updating account for request '%v': %v", params.Body.EmailAddress, err)
return account.NewResetTokenInternalServerError()
}
if err := str.DeletePasswordResetRequestByAccountId(a.Id, tx); err != nil {
logrus.Errorf("error deleting password reset requests for request '%v', but continuing on: %v", params.Body.EmailAddress, err)
}
environmentIds, err := str.DeleteEnvironmentByAccountID(a.Id, tx)
if err != nil {
logrus.Errorf("error deleting environments for request '%v', but continuing on: %v", params.Body.EmailAddress, err)
}
if err := str.DeleteFrontendsByEnvironmentIds(tx, environmentIds...); err != nil {
logrus.Errorf("error deleting frontends for request '%v', but continuing on: %v", params.Body.EmailAddress, err)
}
if err := str.DeleteSharesByEnvironmentIds(tx, environmentIds...); err != nil {
logrus.Errorf("error deleting shares for request '%v', but continuing on: %v", params.Body.EmailAddress, err)
}
if err := tx.Commit(); err != nil {
logrus.Errorf("error committing '%v' (%v): %v", params.Body.EmailAddress, a.Email, err)
@ -45,5 +74,5 @@ func (handler *resetTokenHandler) Handle(params account.ResetTokenParams) middle
logrus.Infof("reset token for '%v'", a.Email)
return account.NewResetTokenOK()
return account.NewResetTokenOK().WithPayload(&account.ResetTokenOKBody{Token: token})
}

View File

@ -82,3 +82,23 @@ func (str *Store) DeleteEnvironment(id int, tx *sqlx.Tx) error {
}
return nil
}
func (str *Store) DeleteEnvironmentByAccountID(accountId int, tx *sqlx.Tx) ([]int, error) {
stmt, err := tx.Prepare("update environments set updated_at = current_timestamp, deleted = true where account_id = $1 returning id")
if err != nil {
return nil, errors.Wrap(err, "error preparing environments delete by account_id statement")
}
rows, err := stmt.Query(accountId)
if err != nil {
return nil, errors.Wrap(err, "error executing environments delete by account_id statement")
}
var is []int
for rows.Next() {
var i int
if err := rows.Scan(&i); err != nil {
return nil, errors.Wrap(err, "error scanning environment id")
}
is = append(is, i)
}
return is, nil
}

View File

@ -1,8 +1,10 @@
package store
import (
"fmt"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
"strings"
)
type Frontend struct {
@ -146,3 +148,21 @@ func (str *Store) DeleteFrontend(id int, tx *sqlx.Tx) error {
}
return nil
}
func (str *Store) DeleteFrontendsByEnvironmentIds(tx *sqlx.Tx, environmentIds ...int) error {
queryStrs := make([]string, 0, len(environmentIds))
queryVals := make([]interface{}, 0, len(environmentIds))
for i, v := range environmentIds {
queryStrs = append(queryStrs, fmt.Sprintf("$%d", i))
queryVals = append(queryVals, v)
}
stmt, err := tx.Prepare(fmt.Sprintf("update frontends set updated_at = current_timestamp, deleted = true where environment_id in (%s)", strings.Join(queryStrs, ",")))
if err != nil {
return errors.Wrap(err, "error preparing frontends delete by environment_id statement")
}
_, err = stmt.Exec(queryVals...)
if err != nil {
return errors.Wrap(err, "error executing frontends delete by environment_id statement")
}
return nil
}

View File

@ -98,3 +98,15 @@ func (str *Store) DeleteMultiplePasswordResetRequests(ids []int, tx *sqlx.Tx) er
}
return nil
}
func (str *Store) DeletePasswordResetRequestByAccountId(accountId int, tx *sqlx.Tx) error {
stmt, err := tx.Prepare("update password_reset_requests set updated_at = current_timestamp, deleted = true where account_id = $1")
if err != nil {
return errors.Wrap(err, "error preparing password_reset_requests by account_id delete statement")
}
_, err = stmt.Exec(accountId)
if err != nil {
return errors.Wrap(err, "error executing password_reset_requests by account_id delete statement")
}
return nil
}

View File

@ -1,8 +1,10 @@
package store
import (
"fmt"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
"strings"
)
type Share struct {
@ -111,3 +113,21 @@ func (str *Store) DeleteShare(id int, tx *sqlx.Tx) error {
}
return nil
}
func (str *Store) DeleteSharesByEnvironmentIds(tx *sqlx.Tx, environmentIds ...int) error {
queryStrs := make([]string, 0, len(environmentIds))
queryVals := make([]interface{}, 0, len(environmentIds))
for i, v := range environmentIds {
queryStrs = append(queryStrs, fmt.Sprintf("$%d", i))
queryVals = append(queryVals, v)
}
stmt, err := tx.Prepare(fmt.Sprintf("update shares set updated_at = current_timestamp, deleted = true where environment_id in (%s)", strings.Join(queryStrs, ",")))
if err != nil {
return errors.Wrap(err, "error preparing Shares delete by environment_id statement")
}
_, err = stmt.Exec(queryVals...)
if err != nil {
return errors.Wrap(err, "error executing Shares delete by environment_id statement")
}
return nil
}

View File

@ -9,12 +9,21 @@ const App = () => {
const [user, setUser] = useState();
useEffect(() => {
const localUser = localStorage.getItem("user");
if(localUser) {
setUser(JSON.parse(localUser));
console.log("reloaded user", localUser);
function checkUserData() {
const localUser = localStorage.getItem("user");
if(localUser) {
console.log(localUser)
setUser(JSON.parse(localUser));
console.log("reloaded user", localUser);
}
}
}, []);
document.addEventListener('storage', checkUserData)
return () => {
document.removeEventListener('storage', checkUserData)
}
}, []);
const logout = () => {
setUser(null);

View File

@ -8,18 +8,30 @@ const ResetToken = (props) => {
console.log("I should reset my token")
account.resetToken({ body: { "emailAddress": props.user.email } }).then(resp => {
console.log(resp)
let user = JSON.parse(localStorage.getItem('user'))
localStorage.setItem('user', JSON.stringify({
"email": user.email,
"token": resp.data.token
}));
document.dispatchEvent(new Event('storage'))
}).catch(err => {
console.log("err", err);
});
props.onHide();
}
return (
<div>
<Modal show={props.show} onHide={props.onHide} centered>
<Modal.Header closeButton>Are you Sure?</Modal.Header>
<Modal.Header closeButton>WARNING - Are you Sure?</Modal.Header>
<Modal.Body>
TEST
<Button variant={"light"} onClick={resetToken}>Reset Password</Button>
<div>
Reseting your token will remove all environments, frontends, and shares you've created.
</div>
<div style={{display: 'flex', alignItems:'center', justifyContent: 'center'}}>
<Button variant={"light"} onClick={resetToken}>Reset Password</Button>
<Button variant={"dark"} onClick={props.onHide}>Cancel</Button>
</div>
</Modal.Body>
</Modal>
</div>

View File

@ -40,6 +40,7 @@ const Login = (props) => {
localStorage.setItem('user', JSON.stringify(user))
console.log(user)
console.log('login succeeded', resp)
document.dispatchEvent(new Event('storage'))
} else {
console.log('login failed')
setMessage(errorMessage);