Skip to content

Commit

Permalink
random big update
Browse files Browse the repository at this point in the history
  • Loading branch information
mytja committed Dec 23, 2023
1 parent 3862292 commit 72553db
Show file tree
Hide file tree
Showing 27 changed files with 1,616 additions and 542 deletions.
61 changes: 61 additions & 0 deletions backend/internal/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,67 @@ var CARDS = []Card{
{File: "/taroki/skis", Worth: 5, WorthOver: 32, Alt: "Škis"},
}

var CARDS_MAP = map[string]Card{
"/kara/1": CARDS[0],
"/kara/2": CARDS[1],
"/kara/3": CARDS[2],
"/kara/4": CARDS[3],
"/kara/pob": CARDS[4],
"/kara/kaval": CARDS[5],
"/kara/dama": CARDS[6],
"/kara/kralj": CARDS[7],

"/kriz/7": CARDS[8],
"/kriz/8": CARDS[9],
"/kriz/9": CARDS[10],
"/kriz/10": CARDS[11],
"/kriz/pob": CARDS[12],
"/kriz/kaval": CARDS[13],
"/kriz/dama": CARDS[14],
"/kriz/kralj": CARDS[15],

"/pik/7": CARDS[16],
"/pik/8": CARDS[17],
"/pik/9": CARDS[18],
"/pik/10": CARDS[19],
"/pik/pob": CARDS[20],
"/pik/kaval": CARDS[21],
"/pik/dama": CARDS[22],
"/pik/kralj": CARDS[23],

"/src/1": CARDS[24],
"/src/2": CARDS[25],
"/src/3": CARDS[26],
"/src/4": CARDS[27],
"/src/pob": CARDS[28],
"/src/kaval": CARDS[29],
"/src/dama": CARDS[30],
"/src/kralj": CARDS[31],

"/taroki/pagat": CARDS[32],
"/taroki/2": CARDS[33],
"/taroki/3": CARDS[34],
"/taroki/4": CARDS[35],
"/taroki/5": CARDS[36],
"/taroki/6": CARDS[37],
"/taroki/7": CARDS[38],
"/taroki/8": CARDS[39],
"/taroki/9": CARDS[40],
"/taroki/10": CARDS[41],
"/taroki/11": CARDS[42],
"/taroki/12": CARDS[43],
"/taroki/13": CARDS[44],
"/taroki/14": CARDS[45],
"/taroki/15": CARDS[46],
"/taroki/16": CARDS[47],
"/taroki/17": CARDS[48],
"/taroki/18": CARDS[49],
"/taroki/19": CARDS[50],
"/taroki/20": CARDS[51],
"/taroki/mond": CARDS[52],
"/taroki/skis": CARDS[53],
}

func GetCardByID(id string) (Card, error) {
for _, v := range CARDS {
if v.File == id {
Expand Down
145 changes: 85 additions & 60 deletions backend/internal/httphandlers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,79 @@ type TokenResponse struct {
Name string `json:"name"`
}

func (s *httpImpl) SendRegistrationMail(w http.ResponseWriter, email string) {
user, err := s.db.GetUserByEmail(email)
if err != nil {
s.sugared.Errorw("failed while retrieving the user from the database", "err", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
user.EmailSentOn = int(time.Now().Unix())
err = s.db.UpdateUser(user)
if err != nil {
s.sugared.Errorw("failed while updating the user", "err", err)
w.WriteHeader(http.StatusInternalServerError)
return
}

smtp := mail.NewSMTPClient()
smtp.Host = s.config.EmailServer
smtp.Port = s.config.EmailPort
smtp.Username = s.config.EmailAccount
smtp.Password = s.config.EmailPassword
smtp.Encryption = mail.EncryptionTLS

smtpClient, err := smtp.Connect()
if err != nil {
s.sugared.Error(err)
}

// Create email
msg := mail.NewMSG()
msg.SetFrom("Palčka <registracija@palcka.si>")
msg.AddTo(email)
msg.SetSubject("Registracija na tarok strežniku palcka.si")

emailConfirmationText := fmt.Sprintf(`
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Tarok Palčka</title>
</head>
<body>
<div style="margin-left:auto;margin-right:auto;">
<h1>Registracija na Tarok strežniku Palčka</h1>
Uradna instanca <b>palcka.si</b>
<p/>
Zahvaljujemo se vam za registracijo na tarok strežniku Palčka.
Vaša registracijska koda je: <p/>
<span style="font-size: 30px;">%s</span>
</div>
<p/>
Vaš račun lahko potrdite z obiskom strani <a href="https://palcka.si/email/confirm?email=%s&regCode=%s">https://palcka.si/email/confirm?email=%s&regCode=%s</a>. Pri tem se prepričajte, da vas pelje na uradno stran (na domeni palcka.si).
<p/>
Še enkrat hvala za registracijo in obilo užitka ob igri taroka vam želi
<p/>
Ekipa palcka.si
</body>
</html>
`, user.EmailConfirmation, user.Email, user.EmailConfirmation, user.Email, user.EmailConfirmation)

msg.SetBody(mail.TextHTML, emailConfirmationText)

err = msg.Send(smtpClient)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}

func (s *httpImpl) Registration(w http.ResponseWriter, r *http.Request) {
email := r.FormValue("email")
pass := r.FormValue("pass")
Expand Down Expand Up @@ -83,7 +156,7 @@ func (s *httpImpl) Registration(w http.ResponseWriter, r *http.Request) {
n := time.Now()

user := sql.User{
Email: r.FormValue("email"),
Email: email,
Password: password,
Role: role,
Name: name,
Expand All @@ -92,6 +165,7 @@ func (s *httpImpl) Registration(w http.ResponseWriter, r *http.Request) {
Disabled: false,
PasswordResetToken: "",
PasswordResetInitiatedOn: n.Format(time.RFC3339),
EmailSentOn: 0,
}

err = s.db.InsertUser(user)
Expand All @@ -101,64 +175,7 @@ func (s *httpImpl) Registration(w http.ResponseWriter, r *http.Request) {
return
}

go func() {
smtp := mail.NewSMTPClient()
smtp.Host = s.config.EmailServer
smtp.Port = s.config.EmailPort
smtp.Username = s.config.EmailAccount
smtp.Password = s.config.EmailPassword
smtp.Encryption = mail.EncryptionTLS

smtpClient, err := smtp.Connect()
if err != nil {
s.sugared.Error(err)
}

// Create email
msg := mail.NewMSG()
msg.SetFrom("Palčka <registracija@palcka.si>")
msg.AddTo(email)
msg.SetSubject("Registracija na tarok strežniku palcka.si")

emailConfirmationText := fmt.Sprintf(`
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Tarok Palčka</title>
</head>
<body>
<div style="margin-left:auto;margin-right:auto;">
<h1>Registracija na Tarok strežniku Palčka</h1>
Uradna instanca <b>palcka.si</b>
<p/>
Zahvaljujemo se vam za registracijo na tarok strežniku Palčka.
Vaša registracijska koda je: <p/>
<span style="font-size: 30px;">%s</span>
</div>
<p/>
Vaš račun lahko potrdite z obiskom strani <a href="https://palcka.si/email/confirm?email=%s&regCode=%s">https://palcka.si/email/confirm?email=%s&regCode=%s</a>. Pri tem se prepričajte, da vas pelje na uradno stran (na domeni palcka.si).
<p/>
Še enkrat hvala za registracijo in obilo užitka ob igri taroka vam želi
<p/>
Ekipa palcka.si
</body>
</html>
`, emailConfirmationPassword, email, emailConfirmationPassword, email, emailConfirmationPassword)

msg.SetBody(mail.TextHTML, emailConfirmationText)

err = msg.Send(smtpClient)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}()
go s.SendRegistrationMail(w, email)

w.WriteHeader(http.StatusCreated)
}
Expand All @@ -179,11 +196,19 @@ func (s *httpImpl) Login(w http.ResponseWriter, r *http.Request) {
return
}

if user.Disabled || !user.EmailConfirmed {
if user.Disabled {
w.WriteHeader(http.StatusForbidden)
return
}

if !user.EmailConfirmed {
if (user.EmailSentOn + 300) < int(time.Now().Unix()) {
go s.SendRegistrationMail(w, email)
}
w.WriteHeader(http.StatusAccepted)
return
}

var token string
if user.LoginToken == "" {
token, err = s.db.GetRandomToken(user)
Expand Down
17 changes: 17 additions & 0 deletions backend/internal/httphandlers/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package httphandlers

import (
"encoding/json"
"net/http"
)

func DumpJSON(jsonstruct interface{}) []byte {
marshal, _ := json.Marshal(jsonstruct)
return marshal
}

func WriteJSON(w http.ResponseWriter, jsonstruct interface{}, statusCode int) {
w.WriteHeader(statusCode)
w.Header().Set("Content-Type", "application/json")
w.Write(DumpJSON(jsonstruct))
}
5 changes: 5 additions & 0 deletions backend/internal/httphandlers/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ type HTTPHandler interface {
Logout(w http.ResponseWriter, r *http.Request)
RequestPasswordReset(w http.ResponseWriter, r *http.Request)
PasswordResetConfirm(w http.ResponseWriter, r *http.Request)
GetAllTournaments(w http.ResponseWriter, r *http.Request)
GetUpcomingTournaments(w http.ResponseWriter, r *http.Request)
NewTournament(w http.ResponseWriter, r *http.Request)
UpdateTournament(w http.ResponseWriter, r *http.Request)
DeleteTournament(w http.ResponseWriter, r *http.Request)
}

func NewHTTPHandler(db sql.SQL, config *consts.ServerConfig, sugared *zap.SugaredLogger) HTTPHandler {
Expand Down

0 comments on commit 72553db

Please sign in to comment.