-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth_handler.go
210 lines (175 loc) · 6.49 KB
/
auth_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// /internal/handlers/auth_handler.go
package handlers
import (
"net/http"
"time"
"context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"encoding/json"
"strings"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
"golang.org/x/crypto/bcrypt"
"task_management_api/pkg/models"
"task_management_api/pkg/database"
)
var googleOauthConfig = &oauth2.Config{
ClientID: cfg.GOOGLE_CLIENT_ID,
ClientSecret: cfg.GOOGLE_CLIENT_SECRET,
RedirectURL: cfg.GOOGLE_REDIRECT_URL,
Scopes: []string{"https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile"},
Endpoint: google.Endpoint,
}
func RegisterUser(c *gin.Context) {
var user models.User
if err := c.ShouldBindJSON(&user); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
trimmedPassword := strings.TrimSpace(user.Password)
if trimmedPassword == "" || len(trimmedPassword) < 6 {
c.JSON(http.StatusBadRequest, gin.H{"error": "Password must be at least 6 characters long"})
return
}
if err := validate.Struct(user); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
hashedPassword, err := hashPassword(trimmedPassword)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to hash password"})
return
}
user.Password = hashedPassword
if err := database.DB.Create(&user).Error; err != nil {
c.JSON(http.StatusConflict, gin.H{"error": "User already exists"})
return
}
c.JSON(http.StatusCreated, gin.H{"message": "User registered successfully"})
}
// LoginUser handles user login
func LoginUser(c *gin.Context) {
var loginData models.User
if err := c.ShouldBindJSON(&loginData); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
var user models.User
if err := database.DB.Where("username = ?", loginData.Username).First(&user).Error; err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid credentials"})
return
}
if !checkPasswordHash(loginData.Password, user.Password) {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid credentials"})
return
}
token := generateToken(user.Username, user.Role)
c.JSON(http.StatusOK, gin.H{"token": token})
}
// GoogleLogin redirects to Google OAuth
func GoogleLogin(c *gin.Context) {
url := googleOauthConfig.AuthCodeURL("state", oauth2.AccessTypeOffline)
c.Redirect(http.StatusTemporaryRedirect, url)
}
// GoogleCallback handles the callback from Google OAuth
func GoogleCallback(c *gin.Context) {
code := c.Query("code")
if code == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "No code in the request"})
return
}
token, err := googleOauthConfig.Exchange(context.Background(), code)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to exchange token"})
return
}
resp, err := http.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + token.AccessToken)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch user info"})
return
}
defer resp.Body.Close()
var googleUser struct {
Email string `json:"email"`
Name string `json:"name"`
ID string `json:"id"`
}
if err := json.NewDecoder(resp.Body).Decode(&googleUser); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to decode user info"})
return
}
var user models.User
if err := database.DB.Where("username = ?", googleUser.Email).First(&user).Error; err != nil {
user = models.User{
Username: googleUser.Email,
Password: "",
Role: "user",
}
if err := database.DB.Create(&user).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save user to the database"})
return
}
}
tokenString := generateToken(user.Username, user.Role)
c.JSON(http.StatusOK, gin.H{"token": tokenString, "user": googleUser})
}
func generateToken(username string, role string) string {
claims := jwt.MapClaims{
"username": username,
"role": role,
"exp": time.Now().Add(time.Hour * 72).Unix(),
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
tokenString, _ := token.SignedString([]byte(cfg.JWTSecret))
return tokenString
}
// hashPassword hashes the user's password
func hashPassword(password string) (string, error) {
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return "", err
}
return string(hashedPassword), nil
}
// checkPasswordHash compares the password with the hashed password
func checkPasswordHash(password, hashedPassword string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
return err == nil
}
// AdminRegisterUser handles admin registration of new users
func AdminRegisterUser(c *gin.Context) {
var user models.User
if err := c.ShouldBindJSON(&user); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
trimmedPassword := strings.TrimSpace(user.Password)
if trimmedPassword == "" || len(trimmedPassword) < 6 {
c.JSON(http.StatusBadRequest, gin.H{"error": "Password must be at least 6 characters long"})
return
}
if user.Role == "" {
user.Role = "admin"
}
if err := validate.Struct(user); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
hashedPassword, err := hashPassword(trimmedPassword)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to hash password"})
return
}
user.Password = hashedPassword
var existingUser models.User
if err := database.DB.Where("username = ?", user.Username).First(&existingUser).Error; err == nil {
c.JSON(http.StatusConflict, gin.H{"error": "User already exists"})
return
}
if err := database.DB.Create(&user).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create user"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "User registered successfully"})
}