-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
68 lines (50 loc) · 1.28 KB
/
auth.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
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func signIn(c *gin.Context) {
var userInput Auth
var storedUser Auth
if err := c.ShouldBindJSON(&userInput); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
// Find user by email
result := db.Where("email = ?", userInput.Email).First(&storedUser)
if result.Error != nil {
c.JSON(401, gin.H{"error": "Invalid email or password"})
return
}
// Verify password
if userInput.Password == storedUser.Password {
user := User{
Id: storedUser.Id,
Username: storedUser.Username,
}
token, err := GenerateToken(user.Id)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to generate token"})
return
}
c.JSON(http.StatusOK, gin.H{"token": token, "user": user})
} else {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid credentials"})
}
}
func signUp(c *gin.Context) {
var newUser Auth
if err := c.ShouldBindJSON(&newUser); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
if err := db.Create(&newUser).Error; err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
user := User{
Id: newUser.Id,
Username: newUser.Username,
}
c.JSON(200, gin.H{"message": "SignUp successful", "user": user})
}