Skip to content

Commit 5ef550a

Browse files
committed
added confirm email endpoint
1 parent c49de36 commit 5ef550a

File tree

18 files changed

+250
-34
lines changed

18 files changed

+250
-34
lines changed

api/errors/errors.go

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var (
3535
EmailOrPassword = APIError{400, "EMAILPASSMISS", "Email or password missing", "The Email or password missing"}
3636
EmailNotFound = APIError{400, "EMAILNOTFND", "Email Not Found", "No matching email address was found"}
3737
UserNotFound = APIError{400, "USERNOTFND", "User Not Found", "No matching user found for the supplied ID"}
38+
EmailTokenNotFound = APIError{400, "TOKNOTFOUND", "Token Not Found", "No matching token found"}
3839
WrongPassword = APIError{401, "WRONGPASS", "Wrong Password", "password is incorrect"}
3940
EmailTaken = APIError{400, "EMAILTAKEN", "Email Taken", "email already exists"}
4041
AuthCookieMissing = APIError{403, "AUTHCOOKMISS", "Auth Cookie Missing", "Auth Cookie is missing"}

api/integrations/stripe/stripe.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ type WebhookEvent struct {
3333
}
3434

3535
func Init() {
36-
conf = config.GetConfig()
37-
stripe.Key = conf.Stripe.SecretKey
36+
stripe.Key = config.Load().Stripe.SecretKey
3837
}
3938

4039
func CreatePlan(amount, intervalCount int64, interval, name, currency string) (*string, *string, error) {

api/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main
22

33
import (
4-
"eirevpn/api/config"
4+
cfg "eirevpn/api/config"
55
"eirevpn/api/integrations"
66
"eirevpn/api/logger"
77
"eirevpn/api/models"
@@ -18,8 +18,8 @@ func main() {
1818

1919
appPath, _ := os.Getwd()
2020
filename, _ := filepath.Abs(appPath + "/config.yaml")
21-
config.Init(filename)
22-
conf := config.GetConfig()
21+
cfg.Init(filename)
22+
conf := cfg.Load()
2323

2424
integrations.Init()
2525

api/models/cart.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ func (c *Cart) BeforeCreate(scope *gorm.Scope) error {
4141
return nil
4242
}
4343

44-
// Beforecdate sets the cdatedAt column to the current time
45-
func (c *Cart) Beforecdate(scope *gorm.Scope) error {
46-
scope.SetColumn("cdatedAt", "check")
44+
// BeforeUpdate sets the UpdatedAt column to the current time
45+
func (c *Cart) BeforeUpdate(scope *gorm.Scope) error {
46+
scope.SetColumn("UpdatedAt", time.Now())
4747
return nil
4848
}

api/models/emailtoken.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package models
2+
3+
import (
4+
"time"
5+
6+
"github.com/satori/go.uuid"
7+
"github.com/jinzhu/gorm"
8+
)
9+
10+
// EmailConfirm contains the email confirmation tokens with a one to one mapping
11+
// to the user
12+
type EmailToken struct {
13+
BaseModel
14+
UserID uint
15+
Token string `json:"token"`
16+
}
17+
18+
func (et *EmailToken) Find() error {
19+
if err := db().Where(&et).First(&et).Error; err != nil {
20+
return err
21+
}
22+
return nil
23+
}
24+
25+
func (et *EmailToken) GetUser() (*User, error) {
26+
var user User
27+
if err := db().Model(&user).Related(&et).Error; err != nil {
28+
return nil, err
29+
}
30+
return &user, nil
31+
}
32+
33+
func (et *EmailToken) Create() error {
34+
if err := db().Create(&et).Error; err != nil {
35+
return err
36+
}
37+
return nil
38+
}
39+
40+
func (et *EmailToken) Save() error {
41+
if err := db().Save(&et).Error; err != nil {
42+
return err
43+
}
44+
return nil
45+
}
46+
47+
func (et *EmailToken) Delete() error {
48+
if err := db().Delete(&et).Error; err != nil {
49+
return err
50+
}
51+
return nil
52+
}
53+
54+
// BeforeCreate sets the CreatedAt column to the current time
55+
func (et *EmailToken) BeforeCreate(scope *gorm.Scope) error {
56+
scope.SetColumn("CreatedAt", time.Now())
57+
token := uuid.NewV4()
58+
scope.SetColumn("Token", token.String())
59+
return nil
60+
}
61+
62+
// BeforeUpdate sets the UpdatedAt column to the current time
63+
func (et *EmailToken) BeforeUpdate(scope *gorm.Scope) error {
64+
scope.SetColumn("UpdatedAt", time.Now())
65+
return nil
66+
}

api/models/models.go

+1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ func Get() []interface{} {
2424
&UserAppSession{},
2525
&Cart{},
2626
&Server{},
27+
&EmailToken{},
2728
}
2829
}

api/models/user.go

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type User struct {
2727
Password string `json:"password" binding:"required"`
2828
StripeCustomerID string `json:"stripe_customer_id"`
2929
Type UserType `json:"type"`
30+
EmailConfirmed bool `json:"email_confirmed"`
3031
}
3132

3233
func (u *User) Find() error {

api/router/router.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const secretkey = "verysecretkey1995"
2626

2727
func Init(logging bool) *gin.Engine {
2828

29-
conf := config.GetConfig()
29+
conf := config.Load()
3030

3131
var router *gin.Engine
3232

@@ -71,6 +71,8 @@ func Init(logging bool) *gin.Engine {
7171
private.GET("/user/cancel", user.CancelSubscription)
7272
public.GET("/user/logout", user.Logout) //public so this router can skip auth middleware
7373

74+
public.GET("/user/confirm_email/:token", user.ConfirmEmail)
75+
7476
protected.GET("/plans/:id", plan.Plan)
7577
protected.POST("/plans/create", plan.CreatePlan)
7678
protected.PUT("/plans/update/:id", plan.UpdatePlan)
@@ -100,7 +102,7 @@ func Init(logging bool) *gin.Engine {
100102

101103
func auth(secret string, protected bool) gin.HandlerFunc {
102104
return func(c *gin.Context) {
103-
conf := config.GetConfig()
105+
conf := config.Load()
104106
if conf.App.EnableAuth {
105107
var usersession models.UserAppSession
106108

@@ -149,7 +151,7 @@ func auth(secret string, protected bool) gin.HandlerFunc {
149151
if err != nil {
150152
logger.Log(logger.Fields{
151153
Loc: "router.go - auth()",
152-
Code: errors.TokenInvalid.Code,
154+
Code: errors.RefresCookieMissing.Code,
153155
Err: err.Error(),
154156
})
155157
clearCookies(c)
@@ -271,7 +273,7 @@ func auth(secret string, protected bool) gin.HandlerFunc {
271273
}
272274

273275
func clearCookies(c *gin.Context) {
274-
conf := config.GetConfig()
276+
conf := config.Load()
275277
c.SetCookie(conf.App.AuthCookieName, "", -1, "/", conf.App.Domain, false, true)
276278
c.SetCookie(conf.App.RefreshCookieName, "", -1, "/", conf.App.Domain, false, true)
277279
c.SetCookie("uid", "", -1, "/", conf.App.Domain, false, false)

api/server/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func Connect(c *gin.Context) {
213213
return
214214
}
215215

216-
conf := config.GetConfig()
216+
conf := config.Load()
217217
if conf.App.EnableSubscriptions {
218218
var userplan models.UserPlan
219219
userplan.UserID = userID.(uint)

0 commit comments

Comments
 (0)