Browse Source

Send verification email after registration

master
Immanuel Onyeka 10 months ago
parent
commit
5f853256e1
1 changed files with 33 additions and 5 deletions
  1. +33
    -5
      skouter.go

+ 33
- 5
skouter.go View File

@@ -1828,6 +1828,8 @@ func createUser(w http.ResponseWriter, db *sql.DB, r *http.Request) {
}

json.NewEncoder(w).Encode(user)
user.sendVerificationEmail()
}

func checkPassword(db *sql.DB, id int, pass string) bool {
@@ -3473,12 +3475,37 @@ func verificationToken(id int) (string, error) {
}

func verifyUser(w http.ResponseWriter, db *sql.DB, r *http.Request) {
var claims VerificationClaims
params, err := url.ParseQuery(r.URL.Path)
if err != nil {
w.WriteHeader(500)
log.Println(err)
return
}
tokenStr := params.Get("verification_token")
// Pull token payload into UserClaims
_, err = jwt.ParseWithClaims(tokenStr, &claims,
func(token *jwt.Token) (any, error) {
return []byte(os.Getenv("JWT_SECRET")), nil
})

if err != nil {
w.WriteHeader(500)
log.Println("Could not parse verification claim.")
return
}

if err = claims.Valid(); err != nil {
w.WriteHeader(500)
log.Println("Verification claim invalid. ID:", claims.Id)
return
}
}

func (user *User) sendVerification(w http.ResponseWriter,
db *sql.DB,
r *http.Request) {
func (user *User) sendVerificationEmail() {
auth := smtp.PlainAuth("",
os.Getenv("SMTP_USERNAME"),
os.Getenv("SMTP_PASSWORD"),
@@ -3489,7 +3516,8 @@ r *http.Request) {
message := `Subject: Email Verification
Welcome %s,
Click the link below to verify your email address
%s`
https://skouter.net?verification_token=%s`
t, err := verificationToken(user.Id)
if err != nil { return }
@@ -3505,7 +3533,7 @@ r *http.Request) {
return
}
fmt.Println("Email Sent Successfully!")
log.Println("Email Sent Successfully!")
}

func api(w http.ResponseWriter, r *http.Request) {


Loading…
Cancel
Save