Skip to content

Commit 92d21bb

Browse files
Add. Logic to send token through httpOnly cookies
1 parent bf8a521 commit 92d21bb

File tree

4 files changed

+57
-27
lines changed

4 files changed

+57
-27
lines changed

constants/constants.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package constants
22

33
const (
4-
Success = "success"
5-
Error = "error"
6-
Username = "username"
7-
Admin = "admin"
8-
User = "user"
9-
Authorization = "Authorization"
4+
Success = "success"
5+
Error = "error"
6+
Username = "username"
7+
Admin = "admin"
8+
User = "user"
9+
Authorization = "Authorization"
10+
AccessTokenCookie = "access_token"
11+
RefreshTokenCookie = "refresh_token"
12+
LocalHost = "localhost"
13+
HomePath = "/"
14+
)
15+
16+
const (
1017
UserAlreadyExistsErrorMessage = "user is already exists with username"
1118
UserNotFoundErrorMessage = "user is not found with username"
1219
UserIsNotAuthorisedErrorMessage = "user is not authorised to this api"

handlers/authHandler.go

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"net/http"
66

77
"github.com/gin-gonic/gin"
8+
"github.com/shaikrasheed99/golang-user-jwt-authentication/configs"
9+
"github.com/shaikrasheed99/golang-user-jwt-authentication/constants"
810
"github.com/shaikrasheed99/golang-user-jwt-authentication/helpers"
911
"github.com/shaikrasheed99/golang-user-jwt-authentication/requests"
1012
"github.com/shaikrasheed99/golang-user-jwt-authentication/services"
@@ -64,8 +66,27 @@ func (ah *authHandler) SignupHandler(c *gin.Context) {
6466
return
6567
}
6668

67-
savedUserRes := helpers.CreateAuthenticationResponse(savedUser, accessToken, refreshToken)
68-
res := helpers.CreateSuccessResponse(http.StatusOK, "successfully saved user details", savedUserRes)
69+
res := helpers.CreateSuccessResponse(http.StatusOK, "successfully saved user details", nil)
70+
71+
c.SetCookie(
72+
constants.AccessTokenCookie,
73+
accessToken,
74+
int(configs.JWT_ACCESS_TOKEN_EXPIRATION_IN_MINUTES),
75+
constants.HomePath,
76+
constants.LocalHost,
77+
true,
78+
true,
79+
)
80+
81+
c.SetCookie(
82+
constants.RefreshTokenCookie,
83+
refreshToken,
84+
int(configs.JWT_REFRESH_TOKEN_EXPIRATION_IN_MINUTES),
85+
constants.HomePath,
86+
constants.LocalHost,
87+
true,
88+
true,
89+
)
6990

7091
fmt.Println("[SignupHandler] Finished execution of signup handler")
7192
c.JSON(http.StatusCreated, res)
@@ -106,8 +127,27 @@ func (ah *authHandler) LoginHandler(c *gin.Context) {
106127
return
107128
}
108129

109-
userRes := helpers.CreateAuthenticationResponse(user, accessToken, refreshToken)
110-
res := helpers.CreateSuccessResponse(http.StatusOK, "successfully logged in", userRes)
130+
res := helpers.CreateSuccessResponse(http.StatusOK, "successfully logged in", nil)
131+
132+
c.SetCookie(
133+
constants.AccessTokenCookie,
134+
accessToken,
135+
int(configs.JWT_ACCESS_TOKEN_EXPIRATION_IN_MINUTES),
136+
constants.HomePath,
137+
constants.LocalHost,
138+
true,
139+
true,
140+
)
141+
142+
c.SetCookie(
143+
constants.RefreshTokenCookie,
144+
refreshToken,
145+
int(configs.JWT_REFRESH_TOKEN_EXPIRATION_IN_MINUTES),
146+
constants.HomePath,
147+
constants.LocalHost,
148+
true,
149+
true,
150+
)
111151

112152
fmt.Println("[LoginHandler] Finished execution of login handler")
113153
c.JSON(http.StatusOK, res)

helpers/responseHelper.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,3 @@ func CreateUserResponse(user *models.User) responses.UserResponse {
4242
Role: user.Role,
4343
}
4444
}
45-
46-
func CreateAuthenticationResponse(user *models.User, accessToken, refreshToken string) responses.AuthenticationResponse {
47-
fmt.Println("[CreateAuthenticationResponseHelper] Creating authentication response")
48-
49-
return responses.AuthenticationResponse{
50-
Username: user.Username,
51-
Token: accessToken,
52-
RefreshToken: refreshToken,
53-
}
54-
}

responses/authResponse.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)