Skip to content

Commit d8303da

Browse files
jwt token
1 parent c8e44cb commit d8303da

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

controllers/auth/login.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88
responses "golang-gin-boilerplate/services/apiresponse"
99
"golang-gin-boilerplate/services/logservice"
1010
"net/http"
11+
"time"
1112

13+
"github.com/dgrijalva/jwt-go"
1214
"github.com/gin-gonic/gin"
1315
"github.com/sirupsen/logrus"
1416
"golang.org/x/crypto/bcrypt"
@@ -44,8 +46,43 @@ func Login() gin.HandlerFunc {
4446
c.JSON(http.StatusBadRequest, responses.ErrorResponse{Status: http.StatusBadRequest, Message: "Error: Invalid email or password", Data: ""})
4547
return
4648
}
47-
c.JSON(http.StatusOK, gin.H{
48-
"message": "Ok",
49+
50+
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
51+
"user": existingUser.Email,
52+
"id": existingUser.Id,
53+
"sub": existingUser.Id,
54+
"exp": time.Now().Add(time.Hour * 24 * 30).Unix(),
55+
})
56+
57+
tokenStr, err := token.SignedString([]byte("supersaucysecret"))
58+
if err != nil {
59+
c.JSON(http.StatusBadRequest, gin.H{
60+
"error": "Failed to create token",
61+
})
62+
return
63+
}
64+
65+
Refreshtoken := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
66+
"user": existingUser.Email,
67+
"id": existingUser.Id,
68+
"sub": existingUser.Id,
69+
"exp": time.Now().Add(time.Hour * 24 * 30).Unix(),
4970
})
71+
72+
refreshtoken, err := Refreshtoken.SignedString([]byte("supersaucysecret"))
73+
if err != nil {
74+
c.JSON(http.StatusBadRequest, gin.H{
75+
"error": "Failed to create token",
76+
})
77+
return
78+
}
79+
var res models.SignedResponse
80+
c.SetSameSite(http.SameSiteLaxMode)
81+
c.SetCookie("access_token", tokenStr, 3600*24*30, "", "", false, true)
82+
c.SetCookie("refresh_token", refreshtoken, 3600*24*30, "", "", false, true)
83+
res.Access_token = tokenStr
84+
res.Refresh_token = refreshtoken
85+
c.JSON(http.StatusOK, responses.SuccesResponse{Status: http.StatusOK, Message: "success", Data: map[string]interface{}{"data": res}})
86+
5087
}
5188
}

file-log02-22-2024.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{"level":"info","msg":"golang app started","time":"2024-02-22T13:58:28+05:30"}
2+
{"controller":"logincontroller","functionName":"Login","level":"info","msg":"Login endpoint called","status":"200","time":"2024-02-22T13:59:05+05:30"}
3+
{"level":"info","msg":"golang app started","time":"2024-02-22T14:01:00+05:30"}
4+
{"controller":"logincontroller","functionName":"Login","level":"info","msg":"Login endpoint called","status":"200","time":"2024-02-22T14:01:13+05:30"}
5+
{"level":"info","msg":"golang app started","time":"2024-02-22T14:30:22+05:30"}
6+
{"controller":"logincontroller","functionName":"Login","level":"info","msg":"Login endpoint called","status":"200","time":"2024-02-22T14:30:31+05:30"}
7+
{"level":"info","msg":"golang app started","time":"2024-02-22T14:34:14+05:30"}
8+
{"controller":"logincontroller","functionName":"Login","level":"info","msg":"Login endpoint called","status":"200","time":"2024-02-22T14:34:23+05:30"}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
1111
github.com/chenzhuoyu/iasm v0.9.1 // indirect
1212
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
13+
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
1314
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
1415
github.com/gin-contrib/sse v0.1.0 // indirect
1516
github.com/gin-gonic/gin v1.9.1 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0q
1919
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
2020
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2121
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
22+
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
23+
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
2224
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
2325
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
2426
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=

models/user.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ type UserLogin struct {
1818
Email string `json:"email"`
1919
Password string `json:"password"`
2020
}
21+
22+
type SignedResponse struct {
23+
Access_token string `json:"access_token"`
24+
Refresh_token string `json:"refresh_token"`
25+
}

0 commit comments

Comments
 (0)