Skip to content

Commit

Permalink
feat: separate ory public url from browser redirect url
Browse files Browse the repository at this point in the history
  • Loading branch information
ThetaSinner committed Oct 10, 2024
1 parent 1b18b08 commit d5ce63e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
12 changes: 6 additions & 6 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
)

type App struct {
Router fiber.Router
Ory *ory.APIClient
OryBase string
Router fiber.Router
Ory *ory.APIClient
OryBrowserBase string
}

func SessionUser(c *fiber.Ctx) interface{} {
Expand Down Expand Up @@ -55,7 +55,7 @@ func (a *App) Prepare(app *fiber.App) {

return c.Render("public/auth/login", fiber.Map{
"Flow": flow,
"RegisterUrl": fmt.Sprintf("%sself-service/registration/browser", a.OryBase),
"RegisterUrl": fmt.Sprintf("%sself-service/registration/browser", a.OryBrowserBase),
})
})

Expand Down Expand Up @@ -109,14 +109,14 @@ func (a *App) Prepare(app *fiber.App) {
app.Use(func(c *fiber.Ctx) error {
cookies := c.GetReqHeaders()["Cookie"]
if len(cookies) == 0 {
return c.Redirect(fmt.Sprintf("%sself-service/login/browser", a.OryBase), http.StatusSeeOther)
return c.Redirect(fmt.Sprintf("%sself-service/login/browser", a.OryBrowserBase), http.StatusSeeOther)
}

// check if we have a session
session, _, err := a.Ory.FrontendAPI.ToSession(c.Context()).Cookie(cookies[0]).Execute()
if (err != nil && session == nil) || (err == nil && !*session.Active) {
// this will redirect the user to the managed Ory Login UI
return c.Redirect(fmt.Sprintf("%sself-service/login/browser", a.OryBase), http.StatusSeeOther)
return c.Redirect(fmt.Sprintf("%sself-service/login/browser", a.OryBrowserBase), http.StatusSeeOther)
}

c.Locals("cookies", cookies[0])
Expand Down
11 changes: 8 additions & 3 deletions locus.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func main() {
}
config.Servers = ory.ServerConfigurations{{URL: oryPublicUrl}}

oryPublicBrowserUrl := os.Getenv("ORY_PUBLIC_BROWSER_URL")
if oryPublicBrowserUrl == "" {
oryPublicBrowserUrl = oryPublicUrl
}

oryClient := ory.NewAPIClient(config)

log.SetLevel(log.LevelInfo)
Expand All @@ -53,9 +58,9 @@ func main() {
} else {
log.Info("Running in production mode")
oryAuthApp := auth.App{
Router: app.Group("/auth"),
Ory: oryClient,
OryBase: oryPublicUrl,
Router: app.Group("/auth"),
Ory: oryClient,
OryBrowserBase: oryPublicBrowserUrl,
}
oryAuthApp.Prepare(app)
}
Expand Down

0 comments on commit d5ce63e

Please sign in to comment.