-
Notifications
You must be signed in to change notification settings - Fork 2
/
token.go
83 lines (79 loc) · 2.33 KB
/
token.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
package handlers
import (
"database/sql"
"encoding/json"
"github.com/dgrijalva/jwt-go"
"github.com/jafarlihi/symposium/api/config"
"github.com/jafarlihi/symposium/api/models"
"github.com/jafarlihi/symposium/api/repositories"
"golang.org/x/crypto/bcrypt"
"io"
"net/http"
)
type tokenCreationRequest struct {
Username string `json:"username"`
Email string `json:"email"`
Password string `json:"password"`
}
type tokenCreationResponse struct {
Token string `json:"token"`
User models.User `json:"user"`
}
func CreateToken(w http.ResponseWriter, r *http.Request) {
var tcr tokenCreationRequest
err := json.NewDecoder(r.Body).Decode(&tcr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error": "Request body couldn't be parsed as JSON"}`)
return
}
if tcr.Username == "" && tcr.Email == "" {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error": "Username and email fields are missing, at least one is required"}`)
return
}
if tcr.Password == "" {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error": "Password field is missing"}`)
return
}
user, err := repositories.GetUserByUsername(tcr.Username)
if err != nil {
switch err {
case sql.ErrNoRows:
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error": "User does not exist"}`)
default:
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, `{"error": "Failed to get the user"}`)
}
return
}
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(tcr.Password))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error": "Wrong password"}`)
return
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"userID": user.ID,
})
tokenString, err := token.SignedString([]byte(config.Config.Jwt.SigningSecret))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, `{"error": "Failed to create token"}`)
return
}
var response tokenCreationResponse
response.Token = tokenString
response.User = *user
response.User.Password = ""
jsonResponse, err := json.Marshal(response)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, `{"error": "Failed to marshal the response to JSON"}`)
return
}
w.WriteHeader(http.StatusOK)
io.WriteString(w, string(jsonResponse))
}