update: use env file to config server

This commit is contained in:
Kenta420-Poom 2023-09-20 10:39:11 +07:00
parent 59407840cd
commit d8f9858ea0
8 changed files with 541 additions and 12 deletions

View file

@ -7,17 +7,19 @@ import (
"net/http"
"net/url"
"os"
"recipe-manager/config"
"github.com/go-chi/chi/v5"
"golang.org/x/oauth2"
)
type AuthRouter struct {
cfg *config.ServerConfig
gConfig *oauth2.Config
nonce map[string]map[string]string
}
func NewAuthRouter() *AuthRouter {
func NewAuthRouter(cfg *config.ServerConfig) *AuthRouter {
file, err := os.Open("client_secret.json")
if err != nil {
@ -29,10 +31,11 @@ func NewAuthRouter() *AuthRouter {
json.NewDecoder(file).Decode(&clientSecret)
return &AuthRouter{
cfg: cfg,
gConfig: &oauth2.Config{
ClientID: clientSecret["web"].(map[string]interface{})["client_id"].(string),
ClientSecret: clientSecret["web"].(map[string]interface{})["client_secret"].(string),
RedirectURL: "http://localhost:8080/auth/google/callback",
RedirectURL: cfg.ServerDomain + "/auth/google/callback",
Scopes: []string{"https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile"},
Endpoint: oauth2.Endpoint{
AuthURL: clientSecret["web"].(map[string]interface{})["auth_uri"].(string),
@ -116,7 +119,7 @@ func (ar *AuthRouter) Route(r chi.Router) {
// redirect to frontend with token and refresh token
w.Header().Add("set-cookie", "access_token="+token.AccessToken+"; Path=/; HttpOnly; SameSite=None; Secure")
w.Header().Add("set-cookie", "refresh_token="+token.RefreshToken+"; Path=/; HttpOnly; SameSite=None; Secure")
http.Redirect(w, r, "http://localhost:4200/callback?"+value.Encode(), http.StatusTemporaryRedirect)
http.Redirect(w, r, ar.cfg.ClientRedirectURL+"/callback?"+value.Encode(), http.StatusTemporaryRedirect)
})
r.Get("/refresh", func(w http.ResponseWriter, r *http.Request) {
@ -136,7 +139,7 @@ func (ar *AuthRouter) Route(r chi.Router) {
}
// redirect to frontend with token and refresh token
http.Redirect(w, r, "http://localhost:4200/callback?token="+token.AccessToken+"&redirect_to="+redirectTo, http.StatusTemporaryRedirect)
http.Redirect(w, r, ar.cfg.ClientRedirectURL+"/callback?token="+token.AccessToken+"&redirect_to="+redirectTo, http.StatusTemporaryRedirect)
})
r.Get("/revoke", func(w http.ResponseWriter, r *http.Request) {