Skip to content

Commit 1ea230d

Browse files
author
raul-brainattica
committed
Login - Auth Services added.
1 parent dfa2a93 commit 1ea230d

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

controllers/auth_controller.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package controllers
33
import (
44
"api.jwt.auth/api/parameters"
55
"api.jwt.auth/core/authentication"
6+
"api.jwt.auth/services"
67
"api.jwt.auth/services/models"
78
"encoding/json"
89
"fmt"
@@ -11,24 +12,14 @@ import (
1112
)
1213

1314
func Login(w http.ResponseWriter, r *http.Request) {
14-
request_user := new(models.User)
15+
requestUser := new(models.User)
1516
decoder := json.NewDecoder(r.Body)
16-
decoder.Decode(&request_user)
17+
decoder.Decode(&requestUser)
1718

18-
authBackend := authentication.InitJWTAuthenticationBackend()
19-
20-
if authBackend.Authenticate(request_user) {
21-
token := parameters.TokenAuthentication{authBackend.GenerateToken()}
22-
response, _ := json.Marshal(token)
23-
w.Header().Set("Content-Type", "application/json")
24-
w.WriteHeader(http.StatusOK)
25-
w.Write(response)
26-
27-
} else {
28-
w.Header().Set("Content-Type", "application/json")
29-
w.WriteHeader(http.StatusUnauthorized)
30-
w.Write([]byte("Unauthorized"))
31-
}
19+
responseStatus, token := services.Login(requestUser)
20+
w.Header().Set("Content-Type", "application/json")
21+
w.WriteHeader(responseStatus)
22+
w.Write(token)
3223
}
3324

3425
func RefresfhToken(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {

services/auth_service.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package services
2+
3+
import (
4+
"api.jwt.auth/api/parameters"
5+
"api.jwt.auth/core/authentication"
6+
"api.jwt.auth/services/models"
7+
"encoding/json"
8+
"net/http"
9+
)
10+
11+
func Login(requestUser *models.User) (int, []byte) {
12+
authBackend := authentication.InitJWTAuthenticationBackend()
13+
14+
if authBackend.Authenticate(requestUser) {
15+
token := parameters.TokenAuthentication{authBackend.GenerateToken()}
16+
response, _ := json.Marshal(token)
17+
return http.StatusOK, response
18+
}
19+
20+
return http.StatusUnauthorized, []byte("")
21+
}
22+
23+
/*func RefreshToken() {
24+
25+
}
26+
27+
func Logout() {
28+
29+
}*/

0 commit comments

Comments
 (0)