Matheus Querido 6 years ago
parent
commit
849d890817
3 changed files with 39 additions and 21 deletions
  1. 4 0
      src/controller/Test/Test.go
  2. 34 20
      src/lib/auth/auth.go
  3. 1 1
      src/lib/curl/curl.go

+ 4 - 0
src/controller/Test/Test.go

@@ -6,6 +6,7 @@ import (
6 6
 
7 7
 	req "../../../core/request"
8 8
 	res "../../../core/response"
9
+	"../../lib/auth"
9 10
 	errorHandler "../../lib/error"
10 11
 )
11 12
 
@@ -16,6 +17,9 @@ type Profile struct {
16 17
 
17 18
 //Inicial : Func
18 19
 func Inicial(w http.ResponseWriter, r *http.Request, vars []string) {
20
+
21
+	auth.GetUser(r)
22
+
19 23
 	var Errors errorHandler.Errors
20 24
 	// fmt.Println(vars[0])
21 25
 	// fmt.Fprintf(w, "Hello Route, %q", html.EscapeString(r.URL.Path))

+ 34 - 20
src/lib/auth/auth.go

@@ -1,23 +1,31 @@
1
-package lib
1
+package auth
2 2
 
3 3
 import (
4 4
 	"encoding/json"
5
+	"errors"
5 6
 	"log"
6 7
 	"net/http"
7
-	"errors"
8
-	"../curl"
8
+
9 9
 	"../../config"
10
+	"../curl"
10 11
 )
11 12
 
12 13
 type UserData struct {
13
-	UserId   int    `json:"id_user"`
14
-	Email    string `json:"email"`
15
-	Name     string `json:"name"`
16
-	Username string `json:"username"`
17
-	Token    string `json:"token"`
18
-	UrlToken string `json:"url_token"`
19
-	Avatar   string `json:"avatar"`
20
-	Language string `json:"language"`
14
+	UserId   int        `json:"id_user"`
15
+	Email    string     `json:"email"`
16
+	Name     string     `json:"name"`
17
+	Username string     `json:"username"`
18
+	Token    string     `json:"token"`
19
+	UrlToken string     `json:"url_token"`
20
+	Avatar   string     `json:"avatar"`
21
+	Language string     `json:"language"`
22
+	Errors   []apiError `json:"errors"`
23
+}
24
+
25
+type apiError struct {
26
+	Type      string `json:"type"`
27
+	Parameter string `json:"parameter"`
28
+	Message   string `json:"message"`
21 29
 }
22 30
 
23 31
 // func (user UserData) getId() int {
@@ -33,7 +41,12 @@ func GetUser(httpReq *http.Request) (*UserData, bool) {
33 41
 		return nil, false
34 42
 	}
35 43
 
36
-	response := curl.Curl(curlReq)
44
+	response, err := curl.Curl(curlReq)
45
+
46
+	if err != nil {
47
+		log.Print(err)
48
+		return nil, false
49
+	}
37 50
 
38 51
 	var userData UserData
39 52
 
@@ -53,19 +66,20 @@ func GetUser(httpReq *http.Request) (*UserData, bool) {
53 66
 	return &userData, true
54 67
 }
55 68
 
56
-func curlRequest(request *http.Request) curl.Request, error {
69
+func curlRequest(request *http.Request) (*curl.Request, error) {
57 70
 
58 71
 	authToken := request.Header.Get("Auth")
59 72
 
60
-	if authToken != nil {
73
+	if authToken != "" {
74
+
75
+		headers := make(map[string]string)
61 76
 
62
-		headers map[string]string
63 77
 		headers["Auth"] = authToken
64
-	
65
-		curlReq := curl.Request{
66
-			Url: config.AUTHENTICATION_SERVICE
67
-			Method: "POST"
68
-			Headers: headers
78
+
79
+		curlReq := &curl.Request{
80
+			Url:     config.AUTHENTICATION_SERVICE,
81
+			Method:  "POST",
82
+			Headers: headers,
69 83
 		}
70 84
 
71 85
 		return curlReq, nil

+ 1 - 1
src/lib/curl/curl.go

@@ -16,7 +16,7 @@ type Request struct {
16 16
 	Parameters map[string]string
17 17
 }
18 18
 
19
-func Curl(input Request) ([]byte, error) {
19
+func Curl(input *Request) ([]byte, error) {
20 20
 
21 21
 	req, err := http.NewRequest(input.Method, input.Url, getParams(input.Parameters))
22 22