-
Notifications
You must be signed in to change notification settings - Fork 3
/
handler.go
81 lines (72 loc) · 2.01 KB
/
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
package rest
import (
"context"
"net/http"
"time"
"github.com/dgrijalva/jwt-go"
dblayer "github.com/hariboGCS/Back/src/dbconn"
"github.com/hariboGCS/Back/src/model"
"github.com/labstack/echo"
"gopkg.in/mgo.v2/bson"
)
type handlerInterface interface {
GetScore(c echo.Context)
ReceiveScore(c echo.Context)
Login(c echo.Context) error
Signup(c echo.Context) error
updateUser(c echo.Context) error
SignOut(c echo.Context)
deleteUser(c echo.Context) error
GetNotice(c echo.Context)
GetComplaints(c echo.Context)
}
func GetMainPage(c echo.Context) (err error) {
return c.String(200, "main page")
}
func Signup(c echo.Context) (err error) {
// Bind
u := &model.User{ID: bson.NewObjectId().Hex()}
if err = c.Bind(u); err != nil {
return err
}
// Validate
if u.Email == "" || u.Password == "" {
return &echo.HTTPError{Code: http.StatusBadRequest, Message: "invalid email or password"}
}
collection, err := dblayer.GetDBCollection()
collection.InsertOne(context.TODO(), u)
if err != nil {
return err
}
defer collection.Database().Client().Disconnect(context.TODO())
return c.JSON(http.StatusCreated, u)
}
func Signin(c echo.Context) (err error) {
// Bind
u := new(model.User)
if err = c.Bind(u); err != nil {
return
}
filter := bson.M{"token": u.Token}
collection, err := dblayer.GetDBCollection()
if err != nil {
return err
// return &echo.HTTPError{Code: http.StatusUnauthorized,Message:"invalid email or password"}
}
err = collection.FindOne(context.TODO(), filter).Decode(&u)
_, err = collection.UpdateOne(context.TODO(), filter, &u)
defer collection.Database().Client().Disconnect(context.TODO())
// Create token
token := jwt.New(jwt.SigningMethodHS256)
// Set claims
claims := token.Claims.(jwt.MapClaims)
claims["id"] = u.ID
claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
// Generate encoded token and send it as response
u.Token, err = token.SignedString([]byte("secret"))
if err != nil {
return err
}
u.Password = "" // Don't send password
return c.JSON(http.StatusOK, u)
}