Skip to content

Commit ee683ba

Browse files
authored
Merge pull request #21 from alexisluque/add-cors-support
Add CORS support
2 parents 2dedeb7 + 6ad86cd commit ee683ba

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

01-Authorization-RS256/main.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/dgrijalva/jwt-go"
1515
"github.com/gorilla/mux"
1616
"github.com/joho/godotenv"
17+
"github.com/rs/cors"
1718
)
1819

1920
type Response struct {
@@ -66,6 +67,12 @@ func main() {
6667
SigningMethod: jwt.SigningMethodRS256,
6768
})
6869

70+
c := cors.New(cors.Options{
71+
AllowedOrigins: []string{"http://localhost:3000"},
72+
AllowCredentials: true,
73+
AllowedHeaders: []string{"Authorization"},
74+
})
75+
6976
r := mux.NewRouter()
7077

7178
// This route is always accessible
@@ -104,9 +111,10 @@ func main() {
104111
responseJSON(message, w, http.StatusOK)
105112
}))))
106113

114+
handler := c.Handler(r)
107115
http.Handle("/", r)
108116
fmt.Println("Listening on http://localhost:3010")
109-
http.ListenAndServe("0.0.0.0:3010", nil)
117+
http.ListenAndServe("0.0.0.0:3010", handler)
110118
}
111119

112120

0 commit comments

Comments
 (0)