Repository navigation
Expand file tree
/
Copy pathauth_controller.go
More file actions
128 lines (105 loc) · 3.06 KB
/
Copy pathauth_controller.go
File metadata and controls
128 lines (105 loc) · 3.06 KB
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
122
123
124
125
126
127
128
package controllers
import (
"fmpwebserver/services"
"fmpwebserver/services/models"
"encoding/json"
"net/http"
"github.com/gorilla/mux"
"fmt"
)
func SignUp(w http.ResponseWriter, r *http.Request){
requestUser := new(models.User)
decoder := json.NewDecoder(r.Body)
decoder.Decode(&requestUser)
userExists, err:= models.CheckUserExists(requestUser)
if err != nil {
fmt.Println("Method:SignUp",err)
w.WriteHeader(http.StatusInternalServerError)
} else {
if(userExists==nil){
u, err:= models.SignUpUser(requestUser)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
} else {
code, err :=models.RequestConfirmationCode(u)
if err != nil{
w.WriteHeader(http.StatusInternalServerError)
}else{
//Sending email in goroutine
go services.SendConfirmEmail(u,code)
}
json.NewEncoder(w).Encode(u)
}
}else{
json.NewEncoder(w).Encode(userExists)
}
}
}
func SignUpKiosk(w http.ResponseWriter, r *http.Request){
requestKiosk := new(models.Kiosk)
decoder := json.NewDecoder(r.Body)
decoder.Decode(&requestKiosk)
k,confirmCode,err:= models.SignUpKiosk(requestKiosk.UserName)
if err!= nil {
fmt.Println("Method:SignUpKiosk",err)
w.WriteHeader(http.StatusInternalServerError)
}else{
go services.SendConfirmEmailKiosk(k,confirmCode)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(k)
}
}
func LinkKioskToAccount(w http.ResponseWriter, r *http.Request){
requestKiosk := new(models.Kiosk)
decoder := json.NewDecoder(r.Body)
decoder.Decode(&requestKiosk)
responseStatus, token := services.LoginKiosk(requestKiosk)
requestKiosk.Token = token
k,err:= models.LinkKioskToAccount(requestKiosk)
if err!= nil {
w.WriteHeader(responseStatus)
}else{
if k!=nil{
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(k)
} else {
w.WriteHeader(http.StatusUnauthorized)
}
}
}
func ValidateConfirmCode(w http.ResponseWriter, r *http.Request){
vars := mux.Vars(r)
code := vars["code"]
usr, err :=models.VlidateConfirmCode(code)
if err != nil{
w.WriteHeader(http.StatusInternalServerError)
}else{
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(usr)
}
}
func Login(w http.ResponseWriter, r *http.Request) {
requestUser := new(models.User)
decoder := json.NewDecoder(r.Body)
decoder.Decode(&requestUser)
responseStatus, token := services.Login(requestUser)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(responseStatus)
w.Write(token)
}
func RefreshToken(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
requestUser := new(models.User)
decoder := json.NewDecoder(r.Body)
decoder.Decode(&requestUser)
w.Header().Set("Content-Type", "application/json")
w.Write(services.RefreshToken(requestUser))
}
func Logout(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
err := services.Logout(r)
w.Header().Set("Content-Type", "application/json")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
}
}