Skip to content

Implement account deletion #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pkg/db/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,19 @@ func GetAccountUsername(id ksuid.KSUID) (string, error) {
return account.Username, nil
}

func DeleteAccount(id ksuid.KSUID) error {
result, err := accounts.DeleteOne(nil, bson.M{"id": id})
if err != nil {
return err
}

if result.DeletedCount == 0 {
return errors.New("no account found with the provided ID")
}

return nil
}

func (acc *Account) PasswordMatches(password string) bool {
return bcrypt.CompareHashAndPassword(acc.Password, []byte(password)) == nil
}
Expand Down
48 changes: 48 additions & 0 deletions pkg/web/api/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type accountTimeInfo struct {
var changeEmailTokens = make(map[ksuid.KSUID]passwordChangeInfo)
var discordLinkTokens = make(map[ksuid.KSUID]accountTimeInfo)
var forgotPasswordTokens = make(map[ksuid.KSUID]accountTimeInfo)
var deleteAccountTokens = make(map[ksuid.KSUID]accountTimeInfo)

var ts *turnstile.Turnstile

Expand Down Expand Up @@ -622,3 +623,50 @@ func ForgotPasswordHandler(w http.ResponseWriter, r *http.Request) {
core.SendEmail(email, "Forgot password", "To change the password to "+email+" go to https://meteorclient.com/changePassword?token="+token.String()+" . The link is valid for 15 minutes.")
core.Json(w, core.J{})
}

func DeleteAccountHandler(w http.ResponseWriter, r *http.Request) {
// Get account
account, err := db.GetAccount(r)
if err != nil {
core.JsonError(w, "Could not get account.")
return
}

token := ksuid.New()
deleteAccountTokens[token] = accountTimeInfo{account.ID, time.Now()}

core.SendEmail(account.Email, "Confirm account deletion", "To delete your account go to https://meteorclient.com/confirmDeleteAccount?token="+token.String()+" . The link is valid for 15 minutes.")
core.Json(w, core.J{})
}

func ConfirmDeleteAccountHandler(w http.ResponseWriter, r *http.Request) {
// Validate token
tokenStr := r.URL.Query().Get("token")

token, err := ksuid.Parse(tokenStr)
if err != nil {
core.JsonError(w, "Invalid token.")
return
}

info, exists := deleteAccountTokens[token]
if !exists {
core.JsonError(w, "Invalid token.")
return
}

delete(deleteAccountTokens, token)

if time.Now().Sub(info.time).Minutes() > 15 {
core.JsonError(w, "Invalid token.")
return
}

err = db.DeleteAccount(info.accountId)
if err != nil {
core.JsonError(w, err.Error())
return
}

core.Json(w, core.J{})
}
2 changes: 2 additions & 0 deletions pkg/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ func Main() {
r.Get("/confirmChangeEmail", api.ConfirmChangeEmailHandlerApi)
r.Post("/changePassword", auth.Auth(api.ChangePasswordHandler))
r.Post("/changePasswordToken", api.ChangePasswordTokenHandler)
r.Post("/deleteAccount", auth.Auth(api.DeleteAccountHandler))
r.Get("/confirmDeleteAccount", api.ConfirmDeleteAccountHandler)
})

// /api/online
Expand Down