Skip to content

Commit e011847

Browse files
committed
updated
1 parent adb875a commit e011847

File tree

14 files changed

+635
-126
lines changed

14 files changed

+635
-126
lines changed

controllers/auth.controller.go

Lines changed: 49 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package controllers
22

33
import (
4-
"fmt"
54
"net/http"
65
"strings"
76
"time"
87

98
"github.com/gin-gonic/gin"
9+
"github.com/thanhpk/randstr"
1010
"github.com/wpcodevo/golang-gorm-postgres/initializers"
1111
"github.com/wpcodevo/golang-gorm-postgres/models"
1212
"github.com/wpcodevo/golang-gorm-postgres/utils"
@@ -47,7 +47,7 @@ func (ac *AuthController) SignUpUser(ctx *gin.Context) {
4747
Email: strings.ToLower(payload.Email),
4848
Password: hashedPassword,
4949
Role: "user",
50-
Verified: true,
50+
Verified: false,
5151
Photo: payload.Photo,
5252
Provider: "local",
5353
CreatedAt: now,
@@ -61,17 +61,34 @@ func (ac *AuthController) SignUpUser(ctx *gin.Context) {
6161
return
6262
}
6363

64-
userResponse := &models.UserResponse{
65-
ID: newUser.ID,
66-
Name: newUser.Name,
67-
Email: newUser.Email,
68-
Photo: newUser.Photo,
69-
Role: newUser.Role,
70-
Provider: newUser.Provider,
71-
CreatedAt: newUser.CreatedAt,
72-
UpdatedAt: newUser.UpdatedAt,
64+
config, _ := initializers.LoadConfig(".")
65+
66+
// Generate Verification Code
67+
code := randstr.String(20)
68+
69+
verification_code := utils.Encode(code)
70+
71+
// Update User in Database
72+
newUser.VerificationCode = verification_code
73+
ac.DB.Save(newUser)
74+
75+
var firstName = newUser.Name
76+
77+
if strings.Contains(firstName, " ") {
78+
firstName = strings.Split(firstName, " ")[1]
79+
}
80+
81+
// 👇 Send Email
82+
emailData := utils.EmailData{
83+
URL: config.ClientOrigin + "/verifyemail/" + code,
84+
FirstName: firstName,
85+
Subject: "Your account verification code",
7386
}
74-
ctx.JSON(http.StatusCreated, gin.H{"status": "success", "data": gin.H{"user": userResponse}})
87+
88+
utils.SendEmail(&newUser, &emailData)
89+
90+
message := "We sent an email with a verification code to " + newUser.Email
91+
ctx.JSON(http.StatusCreated, gin.H{"status": "success", "message": message})
7592
}
7693

7794
func (ac *AuthController) SignInUser(ctx *gin.Context) {
@@ -96,68 +113,43 @@ func (ac *AuthController) SignInUser(ctx *gin.Context) {
96113

97114
config, _ := initializers.LoadConfig(".")
98115

99-
// Generate Tokens
100-
access_token, err := utils.CreateToken(config.AccessTokenExpiresIn, user.ID, config.AccessTokenPrivateKey)
101-
if err != nil {
102-
ctx.JSON(http.StatusBadRequest, gin.H{"status": "fail", "message": err.Error()})
103-
return
104-
}
105-
106-
refresh_token, err := utils.CreateToken(config.RefreshTokenExpiresIn, user.ID, config.RefreshTokenPrivateKey)
116+
// Generate Token
117+
token, err := utils.GenerateToken(config.TokenExpiresIn, user.ID, config.TokenSecret)
107118
if err != nil {
108119
ctx.JSON(http.StatusBadRequest, gin.H{"status": "fail", "message": err.Error()})
109120
return
110121
}
111122

112-
ctx.SetCookie("access_token", access_token, config.AccessTokenMaxAge*60, "/", "localhost", false, true)
113-
ctx.SetCookie("refresh_token", refresh_token, config.RefreshTokenMaxAge*60, "/", "localhost", false, true)
114-
ctx.SetCookie("logged_in", "true", config.AccessTokenMaxAge*60, "/", "localhost", false, false)
123+
ctx.SetCookie("token", token, config.TokenMaxAge*60, "/", "localhost", false, true)
115124

116-
ctx.JSON(http.StatusOK, gin.H{"status": "success", "access_token": access_token})
125+
ctx.JSON(http.StatusOK, gin.H{"status": "success", "token": token})
117126
}
118127

119-
// Refresh Access Token
120-
func (ac *AuthController) RefreshAccessToken(ctx *gin.Context) {
121-
message := "could not refresh access token"
122-
123-
cookie, err := ctx.Cookie("refresh_token")
124-
125-
if err != nil {
126-
ctx.AbortWithStatusJSON(http.StatusForbidden, gin.H{"status": "fail", "message": message})
127-
return
128-
}
128+
func (ac *AuthController) LogoutUser(ctx *gin.Context) {
129+
ctx.SetCookie("token", "", -1, "/", "localhost", false, true)
130+
ctx.JSON(http.StatusOK, gin.H{"status": "success"})
131+
}
129132

130-
config, _ := initializers.LoadConfig(".")
133+
func (ac *AuthController) VerifyEmail(ctx *gin.Context) {
131134

132-
sub, err := utils.ValidateToken(cookie, config.RefreshTokenPublicKey)
133-
if err != nil {
134-
ctx.AbortWithStatusJSON(http.StatusForbidden, gin.H{"status": "fail", "message": err.Error()})
135-
return
136-
}
135+
code := ctx.Params.ByName("verificationCode")
136+
verification_code := utils.Encode(code)
137137

138-
var user models.User
139-
result := ac.DB.First(&user, "id = ?", fmt.Sprint(sub))
138+
var updatedUser models.User
139+
result := ac.DB.First(&updatedUser, "verification_code = ?", verification_code)
140140
if result.Error != nil {
141-
ctx.AbortWithStatusJSON(http.StatusForbidden, gin.H{"status": "fail", "message": "the user belonging to this token no logger exists"})
141+
ctx.JSON(http.StatusBadRequest, gin.H{"status": "fail", "message": "Invalid verification code or user doesn't exists"})
142142
return
143143
}
144144

145-
access_token, err := utils.CreateToken(config.AccessTokenExpiresIn, user.ID, config.AccessTokenPrivateKey)
146-
if err != nil {
147-
ctx.AbortWithStatusJSON(http.StatusForbidden, gin.H{"status": "fail", "message": err.Error()})
145+
if updatedUser.Verified {
146+
ctx.JSON(http.StatusConflict, gin.H{"status": "fail", "message": "User already verified"})
148147
return
149148
}
150149

151-
ctx.SetCookie("access_token", access_token, config.AccessTokenMaxAge*60, "/", "localhost", false, true)
152-
ctx.SetCookie("logged_in", "true", config.AccessTokenMaxAge*60, "/", "localhost", false, false)
153-
154-
ctx.JSON(http.StatusOK, gin.H{"status": "success", "access_token": access_token})
155-
}
156-
157-
func (ac *AuthController) LogoutUser(ctx *gin.Context) {
158-
ctx.SetCookie("access_token", "", -1, "/", "localhost", false, true)
159-
ctx.SetCookie("refresh_token", "", -1, "/", "localhost", false, true)
160-
ctx.SetCookie("logged_in", "", -1, "/", "localhost", false, false)
150+
updatedUser.VerificationCode = ""
151+
updatedUser.Verified = true
152+
ac.DB.Save(&updatedUser)
161153

162-
ctx.JSON(http.StatusOK, gin.H{"status": "success"})
154+
ctx.JSON(http.StatusOK, gin.H{"status": "success", "message": "Email verified successfully"})
163155
}

example.env

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ POSTGRES_PORT=6500
77
PORT=8000
88
CLIENT_ORIGIN=http://localhost:3000
99

10-
ACCESS_TOKEN_PRIVATE_KEY=LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpNSUlCUEFJQkFBSkJBTzVIKytVM0xrWC91SlRvRHhWN01CUURXSTdGU0l0VXNjbGFFKzlaUUg5Q2VpOGIxcUVmCnJxR0hSVDVWUis4c3UxVWtCUVpZTER3MnN3RTVWbjg5c0ZVQ0F3RUFBUUpCQUw4ZjRBMUlDSWEvQ2ZmdWR3TGMKNzRCdCtwOXg0TEZaZXMwdHdtV3Vha3hub3NaV0w4eVpSTUJpRmI4a25VL0hwb3piTnNxMmN1ZU9wKzVWdGRXNApiTlVDSVFENm9JdWxqcHdrZTFGY1VPaldnaXRQSjNnbFBma3NHVFBhdFYwYnJJVVI5d0loQVBOanJ1enB4ckhsCkUxRmJxeGtUNFZ5bWhCOU1HazU0Wk1jWnVjSmZOcjBUQWlFQWhML3UxOVZPdlVBWVd6Wjc3Y3JxMTdWSFBTcXoKUlhsZjd2TnJpdEg1ZGdjQ0lRRHR5QmFPdUxuNDlIOFIvZ2ZEZ1V1cjg3YWl5UHZ1YStxeEpXMzQrb0tFNXdJZwpQbG1KYXZsbW9jUG4rTkVRdGhLcTZuZFVYRGpXTTlTbktQQTVlUDZSUEs0PQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQ==
11-
ACCESS_TOKEN_PUBLIC_KEY=LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUZ3d0RRWUpLb1pJaHZjTkFRRUJCUUFEU3dBd1NBSkJBTzVIKytVM0xrWC91SlRvRHhWN01CUURXSTdGU0l0VQpzY2xhRSs5WlFIOUNlaThiMXFFZnJxR0hSVDVWUis4c3UxVWtCUVpZTER3MnN3RTVWbjg5c0ZVQ0F3RUFBUT09Ci0tLS0tRU5EIFBVQkxJQyBLRVktLS0tLQ==
12-
ACCESS_TOKEN_EXPIRED_IN=15m
13-
ACCESS_TOKEN_MAXAGE=15
10+
EMAIL_FROM=admin@admin.com
11+
SMTP_HOST=smtp.mailtrap.io
12+
SMTP_USER=
13+
SMTP_PASS=
14+
SMTP_PORT=587
1415

16+
TOKEN_EXPIRED_IN=60m
17+
TOKEN_MAXAGE=60
1518

16-
REFRESH_TOKEN_PRIVATE_KEY=LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpNSUlCT1FJQkFBSkJBSWFJcXZXeldCSndnYjR1SEhFQ01RdHFZMTI5b2F5RzVZMGlGcG51a0J1VHpRZVlQWkE4Cmx4OC9lTUh3Rys1MlJGR3VxMmE2N084d2s3TDR5dnY5dVY4Q0F3RUFBUUpBRUZ6aEJqOUk3LzAxR285N01CZUgKSlk5TUJLUEMzVHdQQVdwcSswL3p3UmE2ZkZtbXQ5NXNrN21qT3czRzNEZ3M5T2RTeWdsbTlVdndNWXh6SXFERAplUUloQVA5UStrMTBQbGxNd2ZJbDZtdjdTMFRYOGJDUlRaZVI1ZFZZb3FTeW40YmpBaUVBaHVUa2JtZ1NobFlZCnRyclNWZjN0QWZJcWNVUjZ3aDdMOXR5MVlvalZVRlVDSUhzOENlVHkwOWxrbkVTV0dvV09ZUEZVemhyc3Q2Z08KU3dKa2F2VFdKdndEQWlBdWhnVU8yeEFBaXZNdEdwUHVtb3hDam8zNjBMNXg4d012bWdGcEFYNW9uUUlnQzEvSwpNWG1heWtsaFRDeWtXRnpHMHBMWVdkNGRGdTI5M1M2ZUxJUlNIS009Ci0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0t
17-
REFRESH_TOKEN_PUBLIC_KEY=LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUZ3d0RRWUpLb1pJaHZjTkFRRUJCUUFEU3dBd1NBSkJBSWFJcXZXeldCSndnYjR1SEhFQ01RdHFZMTI5b2F5Rwo1WTBpRnBudWtCdVR6UWVZUFpBOGx4OC9lTUh3Rys1MlJGR3VxMmE2N084d2s3TDR5dnY5dVY4Q0F3RUFBUT09Ci0tLS0tRU5EIFBVQkxJQyBLRVktLS0tLQ==
18-
REFRESH_TOKEN_EXPIRED_IN=60m
19-
REFRESH_TOKEN_MAXAGE=60
19+
TOKEN_SECRET=my-ultra-secure-json-web-token-string

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ require (
77
github.com/gin-gonic/gin v1.8.1
88
github.com/golang-jwt/jwt v3.2.2+incompatible
99
github.com/google/uuid v1.1.2
10+
github.com/k3a/html2text v1.0.8
1011
github.com/spf13/viper v1.12.0
12+
github.com/thanhpk/randstr v1.0.4
1113
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa
14+
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
1215
gorm.io/driver/postgres v1.3.8
1316
gorm.io/gorm v1.23.8
1417
)
@@ -50,6 +53,7 @@ require (
5053
golang.org/x/sys v0.0.0-20220804214406-8e32c043e418 // indirect
5154
golang.org/x/text v0.3.7 // indirect
5255
google.golang.org/protobuf v1.28.1 // indirect
56+
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
5357
gopkg.in/ini.v1 v1.66.4 // indirect
5458
gopkg.in/yaml.v2 v2.4.0 // indirect
5559
gopkg.in/yaml.v3 v3.0.1 // indirect

go.sum

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
153153
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
154154
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
155155
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
156+
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
157+
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
156158
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
157159
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
158160
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
@@ -214,6 +216,10 @@ github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnr
214216
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
215217
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
216218
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
219+
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
220+
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
221+
github.com/k3a/html2text v1.0.8 h1:rVanLhKilpnJUJs/CNKWzMC4YaQINGxK0rSG8ssmnV0=
222+
github.com/k3a/html2text v1.0.8/go.mod h1:ieEXykM67iT8lTvEWBh6fhpH4B23kB9OMKPdIBmgUqA=
217223
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
218224
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
219225
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
@@ -276,6 +282,10 @@ github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXY
276282
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
277283
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
278284
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
285+
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
286+
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
287+
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
288+
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
279289
github.com/spf13/afero v1.8.2 h1:xehSyVa0YnHWsJ49JFljMpg1HX19V6NDZ1fkm1Xznbo=
280290
github.com/spf13/afero v1.8.2/go.mod h1:CtAatgMJh6bJEIs48Ay/FOnkljP3WeGUG0MC1RfAqwo=
281291
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
@@ -302,6 +312,8 @@ github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PK
302312
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
303313
github.com/subosito/gotenv v1.3.0 h1:mjC+YW8QpAdXibNi+vNWgzmgBH4+5l5dCXv8cNysBLI=
304314
github.com/subosito/gotenv v1.3.0/go.mod h1:YzJjq/33h7nrwdY+iHMhEOEEbW0ovIz0tB6t6PwAXzs=
315+
github.com/thanhpk/randstr v1.0.4 h1:IN78qu/bR+My+gHCvMEXhR/i5oriVHcTB/BJJIRTsNo=
316+
github.com/thanhpk/randstr v1.0.4/go.mod h1:M/H2P1eNLZzlDwAzpkkkUvoyNNMbzRGhESZuEQk3r0U=
305317
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
306318
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
307319
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
@@ -497,6 +509,7 @@ golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3
497509
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
498510
golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
499511
golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
512+
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
500513
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
501514
golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
502515
golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
@@ -641,11 +654,15 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
641654
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
642655
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
643656
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
657+
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk=
658+
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
644659
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
645660
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
646661
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
647662
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
648663
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
664+
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE=
665+
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=
649666
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s=
650667
gopkg.in/ini.v1 v1.66.4 h1:SsAcf+mM7mRZo2nJNGt8mZCjG8ZRaNGMURJw7BsIST4=
651668
gopkg.in/ini.v1 v1.66.4/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=

initializers/loadEnv.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ type Config struct {
1616

1717
ClientOrigin string `mapstructure:"CLIENT_ORIGIN"`
1818

19-
AccessTokenPrivateKey string `mapstructure:"ACCESS_TOKEN_PRIVATE_KEY"`
20-
AccessTokenPublicKey string `mapstructure:"ACCESS_TOKEN_PUBLIC_KEY"`
21-
RefreshTokenPrivateKey string `mapstructure:"REFRESH_TOKEN_PRIVATE_KEY"`
22-
RefreshTokenPublicKey string `mapstructure:"REFRESH_TOKEN_PUBLIC_KEY"`
23-
AccessTokenExpiresIn time.Duration `mapstructure:"ACCESS_TOKEN_EXPIRED_IN"`
24-
RefreshTokenExpiresIn time.Duration `mapstructure:"REFRESH_TOKEN_EXPIRED_IN"`
25-
AccessTokenMaxAge int `mapstructure:"ACCESS_TOKEN_MAXAGE"`
26-
RefreshTokenMaxAge int `mapstructure:"REFRESH_TOKEN_MAXAGE"`
19+
TokenSecret string `mapstructure:"TOKEN_SECRET"`
20+
TokenExpiresIn time.Duration `mapstructure:"TOKEN_EXPIRED_IN"`
21+
TokenMaxAge int `mapstructure:"TOKEN_MAXAGE"`
22+
23+
EmailFrom string `mapstructure:"EMAIL_FROM"`
24+
SMTPHost string `mapstructure:"SMTP_HOST"`
25+
SMTPPass string `mapstructure:"SMTP_PASS"`
26+
SMTPPort int `mapstructure:"SMTP_PORT"`
27+
SMTPUser string `mapstructure:"SMTP_USER"`
2728
}
2829

2930
func LoadConfig(path string) (config Config, err error) {

middleware/deserialize-user.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@ import (
1313

1414
func DeserializeUser() gin.HandlerFunc {
1515
return func(ctx *gin.Context) {
16-
var access_token string
17-
cookie, err := ctx.Cookie("access_token")
16+
var token string
17+
cookie, err := ctx.Cookie("token")
1818

1919
authorizationHeader := ctx.Request.Header.Get("Authorization")
2020
fields := strings.Fields(authorizationHeader)
2121

2222
if len(fields) != 0 && fields[0] == "Bearer" {
23-
access_token = fields[1]
23+
token = fields[1]
2424
} else if err == nil {
25-
access_token = cookie
25+
token = cookie
2626
}
2727

28-
if access_token == "" {
28+
if token == "" {
2929
ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"status": "fail", "message": "You are not logged in"})
3030
return
3131
}
3232

3333
config, _ := initializers.LoadConfig(".")
34-
sub, err := utils.ValidateToken(access_token, config.AccessTokenPublicKey)
34+
sub, err := utils.ValidateToken(token, config.TokenSecret)
3535
if err != nil {
3636
ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"status": "fail", "message": err.Error()})
3737
return

models/user.model.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ import (
77
)
88

99
type User struct {
10-
ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primary_key"`
11-
Name string `gorm:"type:varchar(255);not null"`
12-
Email string `gorm:"uniqueIndex;not null"`
13-
Password string `gorm:"not null"`
14-
Role string `gorm:"type:varchar(255);not null"`
15-
Provider string `gorm:"not null"`
16-
Photo string `gorm:"not null"`
17-
Verified bool `gorm:"not null"`
18-
CreatedAt time.Time `gorm:"not null"`
19-
UpdatedAt time.Time `gorm:"not null"`
10+
ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primary_key"`
11+
Name string `gorm:"type:varchar(255);not null"`
12+
Email string `gorm:"uniqueIndex;not null"`
13+
Password string `gorm:"not null"`
14+
Role string `gorm:"type:varchar(255);not null"`
15+
Provider string `gorm:"not null"`
16+
Photo string `gorm:"not null"`
17+
VerificationCode string
18+
Verified bool `gorm:"not null"`
19+
CreatedAt time.Time `gorm:"not null"`
20+
UpdatedAt time.Time `gorm:"not null"`
2021
}
2122

2223
type SignUpInput struct {

routes/auth.routes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ func (rc *AuthRouteController) AuthRoute(rg *gin.RouterGroup) {
1919

2020
router.POST("/register", rc.authController.SignUpUser)
2121
router.POST("/login", rc.authController.SignInUser)
22-
router.GET("/refresh", rc.authController.RefreshAccessToken)
2322
router.GET("/logout", middleware.DeserializeUser(), rc.authController.LogoutUser)
23+
router.GET("/verifyemail/:verificationCode", rc.authController.VerifyEmail)
2424
}

templates/base.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{{define "base"}}
2+
<!DOCTYPE html>
3+
<html>
4+
<head>
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
7+
{{template "styles" .}}
8+
<title>{{ .Subject}}</title>
9+
</head>
10+
<body>
11+
<table
12+
role="presentation"
13+
border="0"
14+
cellpadding="0"
15+
cellspacing="0"
16+
class="body"
17+
>
18+
<tr>
19+
<td>&nbsp;</td>
20+
<td class="container">
21+
<div class="content">
22+
<!-- START CENTERED WHITE CONTAINER -->
23+
{{block "content" .}}{{end}}
24+
<!-- END CENTERED WHITE CONTAINER -->
25+
</div>
26+
</td>
27+
<td>&nbsp;</td>
28+
</tr>
29+
</table>
30+
</body>
31+
</html>
32+
{{end}}

0 commit comments

Comments
 (0)