working signup wizard.

This commit is contained in:
Jason Kulatunga 2024-02-14 17:42:21 -08:00
parent db893f66e1
commit f8292c300f
No known key found for this signature in database
5 changed files with 71 additions and 15 deletions

View File

@ -6,6 +6,11 @@ import (
"strings"
)
type UserWizard struct {
*User `json:",inline"`
JoinMailingList bool `json:"join_mailing_list"`
}
type User struct {
ModelBase
FullName string `json:"full_name"`

View File

@ -0,0 +1,45 @@
package utils
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
)
func JoinNewsletter(name string, email string, message string, wantsToChat string) error {
mailingListRequestData := map[string]string{
"email": email,
"name": name,
"message": message,
"11_chat": wantsToChat,
}
payloadBytes, err := json.Marshal(mailingListRequestData)
if err != nil {
return fmt.Errorf("an error occurred while marshalling join newsletter request: %w", err)
}
req, err := http.NewRequest(http.MethodPost, "https://api.platform.fastenhealth.com/v1/newsletter/join", bytes.NewBuffer(payloadBytes))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
newsletterResp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("an error occurred while sending newsletter join request: %w", err)
}
defer newsletterResp.Body.Close()
if newsletterResp.StatusCode >= 300 || newsletterResp.StatusCode < 200 {
b, err := io.ReadAll(newsletterResp.Body)
if err == nil {
log.Printf("Error Response body: %s", string(b))
}
return fmt.Errorf("an error occurred while attempting to join newsletter: %d", newsletterResp.StatusCode)
}
return nil
}

View File

@ -7,6 +7,7 @@ import (
"github.com/fastenhealth/fasten-onprem/backend/pkg/config"
"github.com/fastenhealth/fasten-onprem/backend/pkg/database"
"github.com/fastenhealth/fasten-onprem/backend/pkg/models"
"github.com/fastenhealth/fasten-onprem/backend/pkg/utils"
"github.com/gin-gonic/gin"
"net/http"
)
@ -15,19 +16,25 @@ func AuthSignup(c *gin.Context) {
databaseRepo := c.MustGet(pkg.ContextKeyTypeDatabase).(database.DatabaseRepository)
appConfig := c.MustGet(pkg.ContextKeyTypeConfig).(config.Interface)
var user models.User
if err := c.ShouldBindJSON(&user); err != nil {
var userWizard models.UserWizard
if err := c.ShouldBindJSON(&userWizard); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
return
}
err := databaseRepo.CreateUser(c, &user)
err := databaseRepo.CreateUser(c, userWizard.User)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
return
}
//TODO: we can derive the encryption key and the hash'ed user from the responseData sub. For now the Sub will be the user id prepended with hello.
userFastenToken, err := auth.JwtGenerateFastenTokenFromUser(user, appConfig.GetString("jwt.issuer.key"))
userFastenToken, err := auth.JwtGenerateFastenTokenFromUser(*userWizard.User, appConfig.GetString("jwt.issuer.key"))
//check if the user wants to join the mailing list
if userWizard.JoinMailingList {
//ignore error messages, we don't want to block the user from signing up
utils.JoinNewsletter(userWizard.FullName, userWizard.Email, "", "")
}
c.JSON(http.StatusOK, gin.H{"success": true, "data": userFastenToken})
}

View File

@ -69,14 +69,14 @@
</div><!-- form-group -->
<div class="form-group form-check">
<input [(ngModel)]="newUser.agreeTerms" name="agreeTerms" #agreeTerms="ngModel" type="checkbox" class="form-check-input" id="agreeTermsCheck" required>
<input [(ngModel)]="newUser.agree_terms" name="agree_terms" #agree_terms="ngModel" type="checkbox" class="form-check-input" id="agreeTermsCheck" required>
<label class="form-check-label" for="agreeTermsCheck">
I have read and agree to the Fasten Health <br/> <a href="https://policy.fastenhealth.com/privacy_policy.html">Privacy Policy</a>
and <a href="https://policy.fastenhealth.com/terms.html">Terms of Service</a>
</label>
<div *ngIf="agreeTerms.invalid && (agreeTerms.dirty || agreeTerms.touched)" class="alert alert-danger">
<div *ngIf="agreeTerms.errors?.['required']">
<div *ngIf="agree_terms.invalid && (agree_terms.dirty || agree_terms.touched)" class="alert alert-danger">
<div *ngIf="agree_terms.errors?.['required']">
You must agree to the Privacy Policy and Terms of Use.
</div>
</div>
@ -84,22 +84,20 @@
<div class="form-group form-check">
<input [(ngModel)]="newUser.joinMailingList" name="joinMailingList" #joinMailingList="ngModel" type="checkbox" class="form-check-input" id="joinMailingListCheck">
<input [(ngModel)]="newUser.join_mailing_list" name="join_mailing_list" #join_mailing_list="ngModel" type="checkbox" class="form-check-input" id="joinMailingListCheck">
<label class="form-check-label" for="joinMailingListCheck">
Join Mailing List
</label>
</div>
<button [disabled]="!userForm.form.valid || loading" type="submit" class="btn btn-az-primary btn-block">Create Account</button>
<div *ngIf="errorMsg" class="alert alert-danger mt-3" role="alert">
<strong>Error</strong> {{errorMsg}}
</div>
</form>
</div><!-- az-signin-header -->
<div class="az-signin-footer">
<button [disabled]="!userForm.form.valid || loading" type="submit" class="btn btn-az-primary btn-block">Create Account</button>
</div><!-- az-signin-footer -->
</div><!-- az-card-signin -->
</div><!-- az-signin-wrapper -->

View File

@ -7,9 +7,9 @@ import {ToastService} from '../../services/toast.service';
import {ToastNotification, ToastType} from '../../models/fasten/toast';
class UserWizard extends User {
passwordConfirm: string = ""
agreeTerms: boolean = false
joinMailingList: boolean = true
password_confirm: string = ""
agree_terms: boolean = false
join_mailing_list: boolean = true
}
@ -126,6 +126,7 @@ export class AuthSignupWizardComponent implements OnInit {
this.loading = true
this.submitted = true;
console.log("starting signup process...",this.newUser)
this.authService.Signup(this.newUser).then((tokenResp: any) => {
this.loading = false
console.log(tokenResp);