-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
58 lines (47 loc) · 1.51 KB
/
server.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
package main
import (
"fmt"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
"golang.org/x/crypto/bcrypt"
"net/http"
"time"
)
type Claims struct {
Username string `json:"username"`
jwt.StandardClaims
}
var jwtKey = []byte("my_secret_key")
func main() {
}
func Login(c *gin.Context) {
var loginCreds User
if err := c.BindJSON(&loginCreds); err != nil {
respondWithError(c, http.StatusBadRequest, fmt.Sprintf("Failed to login: %v", err))
return
}
if storedPassword, exists := Users[loginCreds.Username]; !exists || bcrypt.CompareHashAndPassword([]byte(storedPassword), []byte(loginCreds.Password)) != nil {
respondWithError(c, http.StatusUnauthorized, "Invalid username or password")
return
}
tokenString, err := generateToken(loginCreds.Username)
if err != nil {
respondWithError(c, http.StatusInternalServerError, "Failed to create token")
return
}
c.JSON(http.StatusOK, gin.H{"token": tokenString, "message": "Login successful"})
}
func respondWithError(c *gin.Context, code int, message string) {
c.JSON(code, gin.H{"error": message})
}
func generateToken(username string) (string, error) {
expirationTime := time.Now().Add(5 * time.Minute)
claims := &Claims{
Username: username,
StandardClaims: jwt.StandardClaims{
ExpiresAt: expirationTime.Unix(),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString(jwtKey)
}