/
user.go
121 lines (117 loc) · 3.12 KB
/
user.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
package controller
import (
"models"
"io/ioutil"
"encoding/json"
"fmt"
"doggo"
"config"
"redis"
"net/http"
"time"
request "github.com/DronRathore/goexpress/request"
response "github.com/DronRathore/goexpress/response"
express "github.com/DronRathore/goexpress"
)
func IsLoggedIn(res *response.Response) bool {
if res.Locals["logged_in"] != nil && res.Locals["user"] != nil && res.Locals["logged_in"].(bool) == true {
return true
}
return false
}
func UserIndex(req *request.Request, res *response.Response, next func()){
if IsLoggedIn(res) == true {
var userData = res.Locals["user"].(*models.UserRedis)
if userData == nil {
goto Send403
}
res.JSON(map[string]interface{}{
"success": true,
"email": userData.Email,
"name": userData.Name,
"avatar_url": userData.Avatar_Url,
"company": userData.Company,
"uuid": userData.UUID,
"api_key": userData.Api_key,
"location": userData.Location})
return
}
Send403:
res.Header.SetStatus(403)
res.JSON(map[string]string{"error": "Please Login first"})
}
func UserUpdate(req *request.Request, res *response.Response, next func()) {
if IsLoggedIn(res) == true {
var userData = res.Locals["user"].(*models.UserRedis)
if userData == nil {
goto Send403
}
var user = models.User{Email:userData.Email}
exists, err := user.Exists()
if err != nil {
fmt.Println(err)
goto Send500
}
if exists == false {
goto Send403
}
// read the json post
data, err := ioutil.ReadAll(req.GetRaw().Body)
if err == nil {
var updateObj = make(map[string]interface{})
err := json.Unmarshal(data, &updateObj)
if err != nil {
goto Send500
} else {
// update and populate the Object
// avoid primary key from updation
// we are like spotify, won't let you change email
delete(updateObj, "email")
delete(updateObj, "user_id")
done, _ := user.Update(updateObj)
if done == true {
res.JSON(updateObj)
return
} else {
goto Send500
}
}
} else {
goto Send500
}
}
Send403:
doggo.AddDoggoMetric("user.login.fail")
res.Header.SetStatus(403)
res.JSON(map[string]string{"error": "Please Login first"})
return
Send500:
res.Header.SetStatus(500)
res.JSON(map[string]string{"error": "Internal Server Error"})
}
/*
Logout handler
*/
func UserLogout(req *request.Request, res *response.Response, next func()){
if IsLoggedIn(res) {
redis.Redis.Del(req.Cookies.Get(config.Configuration.Session.Cookie))
res.Cookie.Add(&http.Cookie{
Name: config.Configuration.Session.Cookie,
Value: "",
Path: "/",
Domain: "safedelivr.com",
Expires: time.Unix(int64(time.Now().Unix()) - 186000, 0)})
res.Redirect("/")
res.End()
} else {
res.Redirect("/")
res.End()
}
}
var UserController = func() interface{} {
var Router = express.Router()
Router.Get("/api/user", UserIndex)
Router.Get("/api/user/logout", UserLogout)
Router.Post("/api/user", UserUpdate)
return *Router
}()