api: Add go files
I know this is a huge commit, but I can't be bothered to check this in part by part.
This commit is contained in:
61
api/owner_confirm_hex.go
Normal file
61
api/owner_confirm_hex.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func ownerConfirmHex(confirmHex string) error {
|
||||
if confirmHex == "" {
|
||||
return errorMissingField
|
||||
}
|
||||
|
||||
statement := `
|
||||
UPDATE owners
|
||||
SET confirmedEmail=true
|
||||
WHERE ownerHex IN (
|
||||
SELECT ownerHex FROM ownerConfirmHexes
|
||||
WHERE confirmHex=$1
|
||||
);
|
||||
`
|
||||
res, err := db.Exec(statement, confirmHex)
|
||||
if err != nil {
|
||||
logger.Errorf("cannot mark user's confirmedEmail as true: %v\n", err)
|
||||
return errorInternal
|
||||
}
|
||||
|
||||
count, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
logger.Errorf("cannot count rows affected: %v\n", err)
|
||||
return errorInternal
|
||||
}
|
||||
|
||||
if count == 0 {
|
||||
return errorNoSuchConfirmationToken
|
||||
}
|
||||
|
||||
statement = `
|
||||
DELETE FROM ownerConfirmHexes
|
||||
WHERE confirmHex=$1;
|
||||
`
|
||||
_, err = db.Exec(statement, confirmHex)
|
||||
if err != nil {
|
||||
logger.Warningf("cannot remove confirmation token: %v\n", err)
|
||||
// Don't return an error because this is not critical.
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ownerConfirmHexHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if confirmHex := r.FormValue("confirmHex"); confirmHex != "" {
|
||||
if err := ownerConfirmHex(confirmHex); err == nil {
|
||||
http.Redirect(w, r, fmt.Sprintf("%s/login?confirmed=true", os.Getenv("FRONTEND")), http.StatusTemporaryRedirect)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: include error message in the URL
|
||||
http.Redirect(w, r, fmt.Sprintf("%s/login?confirmed=false", os.Getenv("FRONTEND")), http.StatusTemporaryRedirect)
|
||||
}
|
Reference in New Issue
Block a user