feat: add utility function getToken in libs
parent
d6c847117f
commit
fde869e6d7
|
@ -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])
|
||||
}
|
Loading…
Reference in New Issue