Skip to content

Commit

Permalink
Merge pull request #1237 from jerry-0824/branch-add-cors-in-example-p…
Browse files Browse the repository at this point in the history
…lugin

Add CORS in the example plugin
  • Loading branch information
windcbf authored Nov 7, 2024
2 parents ea7874e + b69467e commit b9025b7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
13 changes: 13 additions & 0 deletions server/plugins/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ func AuthWithHttp(ctx *gin.Context, req *common.HttpKnockRequest, helper *plugin
return
}

corsMiddleware(ctx)

switch {
case strings.EqualFold(action, "valid"):
ackMsg, err = authRegular(ctx, req, res, helper)
Expand Down Expand Up @@ -316,6 +318,17 @@ func AuthWithNHP(req *common.NhpAuthRequest, helper *plugins.NhpServerPluginHelp
return ackMsg, err
}

func corsMiddleware(ctx *gin.Context) {
originResource := ctx.Request.Header.Get("Origin")

if (originResource != "") {
// HTTP headers for CORS
ctx.Writer.Header().Set("Access-Control-Allow-Origin", originResource) // allow cross-origin resource sharing
}

ctx.Next()
}

func main() {

}
4 changes: 3 additions & 1 deletion server/plugins/example/templates/example_login.html
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,9 @@ <h2 id="authSuccessMessage"></h2>
"&password=" + encodeURIComponent(password);
console.log(nhpValidUrl);

fetch(nhpValidUrl)
fetch(nhpValidUrl,{
credentials: "include"
})
.then(response => response.json())
.then(result => {
console.log(result);
Expand Down

0 comments on commit b9025b7

Please sign in to comment.