|
8 | 8 | responses "golang-gin-boilerplate/services/apiresponse" |
9 | 9 | "golang-gin-boilerplate/services/logservice" |
10 | 10 | "net/http" |
| 11 | +"time" |
11 | 12 |
|
| 13 | +"github.com/dgrijalva/jwt-go" |
12 | 14 | "github.com/gin-gonic/gin" |
13 | 15 | "github.com/sirupsen/logrus" |
14 | 16 | "golang.org/x/crypto/bcrypt" |
@@ -44,8 +46,43 @@ func Login() gin.HandlerFunc { |
44 | 46 | c.JSON(http.StatusBadRequest, responses.ErrorResponse{Status: http.StatusBadRequest, Message: "Error: Invalid email or password", Data: ""}) |
45 | 47 | return |
46 | 48 | } |
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(), |
49 | 70 | }) |
| 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 | + |
50 | 87 | } |
51 | 88 | } |
0 commit comments