Skip to content

Commit 4723377

Browse files
author
raul-brainattica
committed
Logout - Auth Services added.
1 parent 94487e0 commit 4723377

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

controllers/auth_controller.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package controllers
22

33
import (
4-
"api.jwt.auth/core/authentication"
54
"api.jwt.auth/services"
65
"api.jwt.auth/services/models"
76
"encoding/json"
8-
"fmt"
9-
jwt "github.com/dgrijalva/jwt-go"
107
"net/http"
118
)
129

@@ -27,21 +24,11 @@ func RefresfhToken(w http.ResponseWriter, r *http.Request, next http.HandlerFunc
2724
}
2825

2926
func Logout(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
30-
authBackend := authentication.InitJWTAuthenticationBackend()
31-
token, err := jwt.ParseFromRequest(r, func(token *jwt.Token) (interface{}, error) {
32-
return authBackend.PublicKey, nil
33-
})
34-
tokenString := r.Header.Get("Authorization")
35-
36-
err = authBackend.Logout(tokenString, token)
37-
27+
err := services.Logout(r)
3828
w.Header().Set("Content-Type", "application/json")
3929
if err != nil {
40-
fmt.Println(http.StatusInternalServerError)
41-
fmt.Println(err)
4230
w.WriteHeader(http.StatusInternalServerError)
4331
} else {
44-
fmt.Println(http.StatusOK)
4532
w.WriteHeader(http.StatusOK)
4633
}
4734
}

services/auth_service.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"api.jwt.auth/core/authentication"
66
"api.jwt.auth/services/models"
77
"encoding/json"
8+
jwt "github.com/dgrijalva/jwt-go"
89
"net/http"
910
)
1011

@@ -30,6 +31,14 @@ func RefreshToken() []byte {
3031
return response
3132
}
3233

33-
/*func Logout() {
34-
35-
}*/
34+
func Logout(req *http.Request) error {
35+
authBackend := authentication.InitJWTAuthenticationBackend()
36+
tokenRequest, err := jwt.ParseFromRequest(req, func(token *jwt.Token) (interface{}, error) {
37+
return authBackend.PublicKey, nil
38+
})
39+
if err != nil {
40+
return err
41+
}
42+
tokenString := req.Header.Get("Authorization")
43+
return authBackend.Logout(tokenString, tokenRequest)
44+
}

0 commit comments

Comments
 (0)