Skip to content
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

Automatic verification email -> staging #913

Merged
merged 2 commits into from
Jul 23, 2021
Merged
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
6 changes: 3 additions & 3 deletions dashboard/src/main/auth/VerifyEmail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ export default class VerifyEmail extends Component<PropsType, StateType> {
let formSection = (
<div>
<InputWrapper>
<StatusText>A verification email will be sent to</StatusText>
<StatusText>A verification email should have been sent to</StatusText>
<Email>{this.context.user?.email}</Email>
</InputWrapper>
<StatusText>
Proceed below to verify your email and finish setting up your profile
Didn't get it?
</StatusText>
<Button onClick={this.handleSendEmail}>Send Verification Email</Button>
<Button onClick={this.handleSendEmail}>Resend Verification Email</Button>
</div>
);

Expand Down
5 changes: 5 additions & 0 deletions server/api/oauth_github_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
82 changes: 41 additions & 41 deletions server/api/user_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -382,54 +386,14 @@ 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)
return
}

w.WriteHeader(http.StatusOK)
return
}

// FinalizEmailVerifyUser completes the email verification flow for a user.
Expand Down Expand Up @@ -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,
)
}