diff --git a/dashboard/src/main/auth/VerifyEmail.tsx b/dashboard/src/main/auth/VerifyEmail.tsx index 2293060182..d32aa017a5 100644 --- a/dashboard/src/main/auth/VerifyEmail.tsx +++ b/dashboard/src/main/auth/VerifyEmail.tsx @@ -36,13 +36,13 @@ export default class VerifyEmail extends Component { let formSection = (
- A verification email will be sent to + A verification email should have been sent to {this.context.user?.email} - Proceed below to verify your email and finish setting up your profile + Didn't get it? - +
); diff --git a/server/api/oauth_github_handler.go b/server/api/oauth_github_handler.go index e6e2cb0fb4..7cf0736e66 100644 --- a/server/api/oauth_github_handler.go +++ b/server/api/oauth_github_handler.go @@ -231,6 +231,11 @@ func (app *App) upsertUserFromToken(tok *oauth2.Token) (*models.User, error) { if err != nil { return nil, err } + + if !verified { + // non-fatal email verification flow + app.startEmailVerificationFlow(user) + } } else if err == nil { return nil, fmt.Errorf("email already registered") } else if err != nil { diff --git a/server/api/user_handler.go b/server/api/user_handler.go index fcd4eb3442..e1b1c8ffa2 100644 --- a/server/api/user_handler.go +++ b/server/api/user_handler.go @@ -55,6 +55,10 @@ func (app *App) HandleCreateUser(w http.ResponseWriter, r *http.Request) { app.analyticsClient.Track(analytics.CreateSegmentNewUserTrack(user)) app.Logger.Info().Msgf("New user created: %d", user.ID) + + // non-fatal email verification flow + app.startEmailVerificationFlow(user) + var redirect string if valR := session.Values["redirect"]; valR != nil { @@ -382,46 +386,7 @@ func (app *App) InitiateEmailVerifyUser(w http.ResponseWriter, r *http.Request) return } - // error already handled by helper - if err != nil { - return - } - - form := &forms.InitiateResetUserPasswordForm{ - Email: user.Email, - } - - // convert the form to a pw reset token model - pwReset, rawToken, err := form.ToPWResetToken() - - if err != nil { - app.handleErrorFormDecoding(err, ErrProjectDecode, w) - return - } - - // handle write to the database - pwReset, err = app.Repo.PWResetToken.CreatePWResetToken(pwReset) - - if err != nil { - app.handleErrorDataWrite(err, w) - return - } - - queryVals := url.Values{ - "token": []string{rawToken}, - "token_id": []string{fmt.Sprintf("%d", pwReset.ID)}, - } - - sgClient := email.SendgridClient{ - APIKey: app.ServerConf.SendgridAPIKey, - VerifyEmailTemplateID: app.ServerConf.SendgridVerifyEmailTemplateID, - SenderEmail: app.ServerConf.SendgridSenderEmail, - } - - err = sgClient.SendEmailVerification( - fmt.Sprintf("%s/api/email/verify/finalize?%s", app.ServerConf.ServerURL, queryVals.Encode()), - form.Email, - ) + err = app.startEmailVerificationFlow(user) if err != nil { app.handleErrorInternal(err, w) @@ -429,7 +394,6 @@ func (app *App) InitiateEmailVerifyUser(w http.ResponseWriter, r *http.Request) } w.WriteHeader(http.StatusOK) - return } // FinalizEmailVerifyUser completes the email verification flow for a user. @@ -888,3 +852,39 @@ func (app *App) getUserIDFromRequest(r *http.Request) (uint, error) { return userID, nil } + +func (app *App) startEmailVerificationFlow(user *models.User) error { + form := &forms.InitiateResetUserPasswordForm{ + Email: user.Email, + } + + // convert the form to a pw reset token model + pwReset, rawToken, err := form.ToPWResetToken() + + if err != nil { + return err + } + + // handle write to the database + pwReset, err = app.Repo.PWResetToken.CreatePWResetToken(pwReset) + + if err != nil { + return err + } + + queryVals := url.Values{ + "token": []string{rawToken}, + "token_id": []string{fmt.Sprintf("%d", pwReset.ID)}, + } + + sgClient := email.SendgridClient{ + APIKey: app.ServerConf.SendgridAPIKey, + VerifyEmailTemplateID: app.ServerConf.SendgridVerifyEmailTemplateID, + SenderEmail: app.ServerConf.SendgridSenderEmail, + } + + return sgClient.SendEmailVerification( + fmt.Sprintf("%s/api/email/verify/finalize?%s", app.ServerConf.ServerURL, queryVals.Encode()), + form.Email, + ) +}