44 "log"
55 "os"
66
7- // "fmt"
87 "github.com/joho/godotenv"
98 "golang.org/x/oauth2"
109 "golang.org/x/oauth2/github"
@@ -18,15 +17,23 @@ type Config struct {
1817
1918var AppConfig Config
2019
20+ // backendURL returns the BACKEND_URL env var, or localhost for dev.
21+ // OAuth providers will redirect back to <backendURL>/<provider>_callback.
22+ func backendURL () string {
23+ if u := os .Getenv ("BACKEND_URL" ); u != "" {
24+ return u
25+ }
26+ return "http://localhost:8080"
27+ }
28+
2129func GoogleConfig () oauth2.Config {
22- err := godotenv . Load ( ".env" )
23- if err != nil {
24- log .Fatalf ( "Some error occured. Err : %s " , err )
30+ // godotenv is a dev convenience only — log a warning if missing.
31+ if err := godotenv . Load ( ".env" ); err != nil {
32+ log .Printf ( "[config] no .env file found (expected in production) : %v " , err )
2533 }
2634
2735 AppConfig .GoogleLoginConfig = oauth2.Config {
28- RedirectURL : "http://localhost:8080/google_callback" ,
29- // RedirectURL: "http://localhost:3000/",
36+ RedirectURL : backendURL () + "/google_callback" ,
3037 ClientID : os .Getenv ("GOOGLE_CLIENT_ID" ),
3138 ClientSecret : os .Getenv ("GOOGLE_CLIENT_SECRET" ),
3239 Scopes : []string {
@@ -40,17 +47,13 @@ func GoogleConfig() oauth2.Config {
4047}
4148
4249func GithubConfig () oauth2.Config {
43- err := godotenv .Load (".env" )
44- if err != nil {
45- log .Fatalf ("Some error occured. Err: %s" , err )
50+ if err := godotenv .Load (".env" ); err != nil {
51+ log .Printf ("[config] no .env file found (expected in production): %v" , err )
4652 }
4753
4854 AppConfig .GitHubLoginConfig = oauth2.Config {
49- RedirectURL : "http://localhost:8080/github_callback" ,
50- // RedirectURL: "http://localhost:3000/",
51- ClientID : os .Getenv ("GITHUB_CLIENT_ID" ),
52- // RedirectURL: fmt.Sprintf(
53- // "https://github.com/login/oauth/authorize?scope=user:repo&client_id=%s&redirect_uri=%s", os.Getenv("GITHUB_CLIENT_ID"), "http://localhost:8080/github_callback"),
55+ RedirectURL : backendURL () + "/github_callback" ,
56+ ClientID : os .Getenv ("GITHUB_CLIENT_ID" ),
5457 ClientSecret : os .Getenv ("GITHUB_CLIENT_SECRET" ),
5558 Scopes : []string {"user" , "repo" },
5659 Endpoint : github .Endpoint ,
0 commit comments