Skip to content

Commit

Permalink
Merge pull request #913 from porter-dev/master
Browse files Browse the repository at this point in the history
Automatic verification email -> staging
  • Loading branch information
abelanger5 authored Jul 23, 2021
2 parents 8f6f59f + 3e4e800 commit 5511363
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 44 deletions.
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,
)
}

0 comments on commit 5511363

Please sign in to comment.