Skip to content

Commit

Permalink
finish register formular
Browse files Browse the repository at this point in the history
  • Loading branch information
EnricoSchw committed Oct 10, 2024
1 parent d6dd970 commit d7d7fcd
Show file tree
Hide file tree
Showing 18 changed files with 274 additions and 127 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ config.prod.toml
# App ignores
TestResults.json
/cmd/main.go
config-remote.toml
/config/app.config.*.toml
/config/ctl.config.*.toml
/docs/img/pipeline-i.png
/docs/img/pipeline-ii.png
/docs/img/pipeline.png
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ build: go_init
go build -race -o ./bin/$(SERVER_NAME) ./cmd/server

run: build
./bin/$(SERVER_NAME) -config=config.toml
./bin/$(SERVER_NAME) -config=config/app.config.dev.toml

run-remote: build
./bin/$(SERVER_NAME) -config=config-remote.toml
./bin/$(SERVER_NAME) -config=config/app.config.remote.toml

race:
go run -race ./cmd/server -config=config.toml
go run -race ./cmd/server -config=config/app.config.dev.toml

build-linux: go_init
GOOS=linux GOARCH=amd64 go build -o bin/$(SERVER_NAME).linux.amd64 $(GO_LDFLAGS) ./cmd/server
Expand Down
73 changes: 0 additions & 73 deletions config.docker.toml

This file was deleted.

7 changes: 7 additions & 0 deletions config.toml → config/app.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,10 @@ name = "shig"
[web]
enable = true
dir = "./web"

[mail]
enable = false
from = "[email protected]"
password = ""
smtpHost = "smtp.gmail.com"
smtpPort = "587"
File renamed without changes.
11 changes: 7 additions & 4 deletions internal/auth/account/account_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,25 @@ func (s *AccountService) CreateAccount(ctx context.Context, account *Account) er

actor, err := models.NewPersonActor(s.instanceUrl, account.User)
if err != nil {
return fmt.Errorf("failed create actor: %w", err)
return fmt.Errorf("creating actor: %w", err)
}

account.Actor = actor
_, err = s.repo.Add(ctx, account)
if err != nil {
return fmt.Errorf("failed create account: %w", err)
return fmt.Errorf("adding account: %w", err)
}

token := NewEmailVerificationToken(account)

if err = s.repo.AddVerificationToken(ctx, token); err != nil {
return fmt.Errorf("failed create token: %w", err)
return fmt.Errorf("creating verify token: %w", err)
}

if err = s.mailSender.SendActivateAccountMail(account.User, account.Email, token.UUID); err != nil {
return fmt.Errorf("sending verify email: %w", err)
}

s.mailSender.SendRegisterMail(account.User, account.Email, token.UUID)
return nil
}

Expand Down
8 changes: 4 additions & 4 deletions internal/auth/handler/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/shigde/sfu/internal/auth/account"
"github.com/shigde/sfu/internal/rest"
"golang.org/x/exp/slog"
)

func Register(accountService *account.AccountService) http.HandlerFunc {
Expand All @@ -17,11 +18,10 @@ func Register(accountService *account.AccountService) http.HandlerFunc {
return
}

err = accountService.CreateAccount(r.Context(), acc)
if err != nil {
rest.HttpError(w, "registration error", http.StatusBadRequest, err)
return
if err = accountService.CreateAccount(r.Context(), acc); err != nil {
slog.Error("auth.Register:", "err", err)
}

w.WriteHeader(http.StatusCreated)
}
}
Expand Down
5 changes: 5 additions & 0 deletions internal/config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/shigde/sfu/internal/activitypub/instance"
"github.com/shigde/sfu/internal/auth/session"
"github.com/shigde/sfu/internal/mail"
"github.com/shigde/sfu/internal/metric"
"github.com/shigde/sfu/internal/rtp"
"github.com/shigde/sfu/internal/storage"
Expand Down Expand Up @@ -63,5 +64,9 @@ func ParseConfig(file string, env *Environment) (*SFU, error) {
return nil, err
}

if err := mail.ValidateEmailConfig(config.MailConfig); err != nil {
return nil, err
}

return config, nil
}
2 changes: 2 additions & 0 deletions internal/config/sfu.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/shigde/sfu/internal/activitypub/instance"
"github.com/shigde/sfu/internal/auth/session"
"github.com/shigde/sfu/internal/logging"
"github.com/shigde/sfu/internal/mail"
"github.com/shigde/sfu/internal/metric"
"github.com/shigde/sfu/internal/rtp"
"github.com/shigde/sfu/internal/storage"
Expand All @@ -21,6 +22,7 @@ type SFU struct {
*telemetry.TelemetryConfig `mapstructure:"telemetry"`
*rtp.RtpConfig `mapstructure:"rtp"`
*instance.FederationConfig `mapstructure:"federation"`
*mail.MailConfig `mapstructure:"mail"`
}

type Environment struct {
Expand Down
13 changes: 13 additions & 0 deletions internal/mail/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package mail

type MailConfig struct {
Enable bool `mapstructure:"enable"`
From string `mapstructure:"from"`
Pass string `mapstructure:"password"`
SmtpHost string `mapstructure:"smtpHost"`
SmtpPort string `mapstructure:"smtpPort"`
}

func ValidateEmailConfig(config *MailConfig) error {
return nil
}
8 changes: 0 additions & 8 deletions internal/mail/register.html

This file was deleted.

56 changes: 31 additions & 25 deletions internal/mail/sender_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,59 @@ import (
"fmt"
"html/template"
"net/smtp"
"net/url"
)

type SenderService struct {
cfg *MailConfig
instanceUrl *url.URL
}

func NewSenderService() *SenderService {
return &SenderService{}
func NewSenderService(config *MailConfig, instanceUrl *url.URL) *SenderService {
return &SenderService{
cfg: config,
instanceUrl: instanceUrl,
}
}

func (s *SenderService) SendRegisterMail(name string, email string, activateToken string) {

// Sender data.
from := "[email protected]"
password := "Faderef661$##"
func (s *SenderService) SendActivateAccountMail(name string, email string, activateToken string) error {

// Receiver email address.
to := []string{
"[email protected]",
if !s.cfg.Enable {
return nil
}

// smtp server configuration.
smtpHost := "smtp.gmail.com"
smtpPort := "587"
// Receiver email address.
to := []string{email}
link := s.instanceUrl.String() + "/activateAccount/" + activateToken

// Authentication.
auth := smtp.PlainAuth("", from, password, smtpHost)
auth := smtp.PlainAuth("", s.cfg.From, s.cfg.Pass, s.cfg.SmtpHost)

t, _ := template.ParseFiles("register.html")
t, err := template.ParseFiles("internal/mail/template/activate_account.html")
if err != nil {
return fmt.Errorf("parsing email template: %w", err)
}

var body bytes.Buffer

mimeHeaders := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
body.Write([]byte(fmt.Sprintf("Subject: This is a test subject \n%s\n\n", mimeHeaders)))
if _, err = body.Write([]byte(fmt.Sprintf("Subject: Activate Your Account \n%s\n\n", mimeHeaders))); err != nil {
return fmt.Errorf("writing email boddy: %w", err)
}

t.Execute(&body, struct {
Name string
Message string
User string
Link string
Instance string
}{
Name: "Puneet Singh",
Message: "This is a test message in a HTML template",
User: name,
Link: link,
Instance: s.instanceUrl.String(),
})

// Sending email.
err := smtp.SendMail(smtpHost+":"+smtpPort, auth, from, to, body.Bytes())
if err != nil {
fmt.Println(err)
return
if err = smtp.SendMail(s.cfg.SmtpHost+":"+s.cfg.SmtpPort, auth, s.cfg.From, to, body.Bytes()); err != nil {
return fmt.Errorf("sendingx email boddy: %w", err)
}
fmt.Println("Email Sent!")
return nil
}
Loading

0 comments on commit d7d7fcd

Please sign in to comment.