feat: add utility function getToken in libs

master
fbonhomm 2019-08-25 17:17:30 +02:00
parent d6c847117f
commit fde869e6d7
1 changed files with 26 additions and 0 deletions

26
source/libs/get-token.go Normal file
View File

@ -0,0 +1,26 @@
/**
* Created by fbonhomm
* Email: flo-github@outlook.fr
* Licence: MIT
*/
package libs
import (
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
// GetToken
func GetToken(c *gin.Context) string {
var reqToken = c.Request.Header.Get("Authorization")
var splitToken = strings.Split(reqToken, "Bearer")
if len(splitToken) != 2 {
c.JSON(http.StatusUnauthorized, gin.H{ "error": "Token not conform." })
c.Abort()
}
return strings.TrimSpace(splitToken[1])
}