Browse Source

Added #11

dependabot/npm_and_yarn/web/prismjs-1.21.0
Max Schmitt 8 years ago
parent
commit
6d7ec04974
  1. 26
      handlers/auth.go
  2. 6
      handlers/handlers.go
  3. 11
      static/src/App/App.js

26
handlers/auth.go

@ -60,6 +60,32 @@ func (h *Handler) handleGoogleRedirect(c *gin.Context) {
c.Redirect(http.StatusTemporaryRedirect, h.oAuthConf.AuthCodeURL(state))
}
func (h *Handler) authMiddleware(c *gin.Context) {
authHeader := c.GetHeader("Authorization")
if authHeader == "" {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
"error": "'authorization' header not set",
})
return
}
token, err := jwt.ParseWithClaims(authHeader, &jwtClaims{}, func(token *jwt.Token) (interface{}, error) {
return h.config.Secret, nil
})
if err != nil {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
"error": fmt.Sprintf("could not parse token: %v", err),
})
return
}
if !token.Valid {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
"error": "token is not valid",
})
return
}
c.Next()
}
func (h *Handler) handleGoogleCheck(c *gin.Context) {
var data struct {
Token string `binding:"required"`

6
handlers/handlers.go

@ -54,8 +54,10 @@ func (h *Handler) setHandlers() {
if !h.config.EnableGinDebugMode {
gin.SetMode(gin.ReleaseMode)
}
h.engine.POST("/api/v1/create", h.handleCreate)
h.engine.POST("/api/v1/info", h.handleInfo)
protected := h.engine.Group("/api/v1/protected")
protected.Use(h.authMiddleware)
protected.POST("/create", h.handleCreate)
protected.POST("/info", h.handleInfo)
// h.engine.Static("/static", "static/src")
h.engine.NoRoute(h.handleAccess)
h.engine.LoadHTMLGlob("templates/*")

11
static/src/App/App.js

@ -6,6 +6,17 @@ class AppComponent extends Component {
handleURLChange = (e, { value }) => this.url = value
handleURLSubmit = () => {
console.log("handle Submit", "URL:", this.url)
fetch("/api/v1/protected/create", {
method: "POST",
body: JSON.stringify({
URL: this.url
}),
headers: {
"Authorization": window.localStorage.getItem("token"),
'Content-Type': 'application/json'
}
}).then(res => res.ok ? res.json() : Promise.reject(res.json()))
.then(d => console.log(d))
}
componentWillMount() {

Loading…
Cancel
Save