diff --git a/code-examples/protect-page-login/go/README.md b/code-examples/protect-page-login/go/README.md new file mode 100644 index 0000000000..2969b21d7b --- /dev/null +++ b/code-examples/protect-page-login/go/README.md @@ -0,0 +1,38 @@ +# Ory Authentication Example with Go + +This example demonstrates protecting a page in a Go web server using the Ory SDK +and Ory Account Experience. + +For the full step-by-step guide, see: +[Integrate authentication into Go](https://www.ory.sh/docs/getting-started/integrate-auth/go) + +## Prerequisites + +- Go 1.20+ +- Node.js (to run Ory CLI via `npx`) +- An Ory Network project and the Ory CLI logged in + +## Run locally + +1. Build and run the server: + +```bash +go build -o server +./server +``` + +2. In another terminal, start the Ory Tunnel pointing to your local app: + +```bash +npx @ory/cli tunnel --dev http://localhost:3000 --project +``` + +3. Open http://localhost:3000 in your browser. You will be redirected to the Ory + Account Experience for sign-in, and then returned to the protected page. + +## Notes + +- The server reads the tunnel port from `TUNNEL_PORT` (defaults to `4000`) and + app port from `PORT` (defaults to `3000`). +- This example’s middleware uses `ToSession` to validate the user session and + redirects unauthenticated users to the login UI served via the Ory Tunnel. diff --git a/code-examples/protect-page-login/go/go.mod b/code-examples/protect-page-login/go/go.mod index 3e122d0114..e9e77f9e65 100644 --- a/code-examples/protect-page-login/go/go.mod +++ b/code-examples/protect-page-login/go/go.mod @@ -1,7 +1,7 @@ module github.com/ory/docs/code-examples/go -go 1.17 +go 1.25.3 -require github.com/ory/client-go v1.15.5 +require github.com/ory/client-go v1.22.8 -require golang.org/x/oauth2 v0.23.0 // indirect +require golang.org/x/oauth2 v0.32.0 // indirect diff --git a/code-examples/protect-page-login/go/go.sum b/code-examples/protect-page-login/go/go.sum index 725238c3a7..b5c1fecdde 100644 --- a/code-examples/protect-page-login/go/go.sum +++ b/code-examples/protect-page-login/go/go.sum @@ -1,21 +1,4 @@ -cloud.google.com/go/compute/metadata v0.3.0/go.mod h1:zFmK7XCadkQkj6TtorcaGlCW1hT1fIilQDwofLpJ20k= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/ory/client-go v1.15.5 h1:WZ2p9O2H8J6vMa6e1oj0+RA9GQt4Laa7sY4rA0/D7wk= -github.com/ory/client-go v1.15.5/go.mod h1:kF9vH8mfpLJvgJus6dOjSXZ6KoswiwwcJ25hP7w2O8k= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs= -golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +github.com/ory/client-go v1.22.8 h1:v1NCqmKIKFQFYQihWUJuksvxILujvVA8HUOCAs5Lxr0= +github.com/ory/client-go v1.22.8/go.mod h1:o/1hF5MKq3gyn9nUWZF/VVz35nitCzsGfIwl5SXVJ1Y= +golang.org/x/oauth2 v0.32.0 h1:jsCblLleRMDrxMN29H3z/k1KliIvpLgCkE6R8FXXNgY= +golang.org/x/oauth2 v0.32.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA= diff --git a/docs/getting-started/integrate-auth/01_go.mdx b/docs/getting-started/integrate-auth/01_go.mdx index 1ffff2d7d9..fc6e426919 100644 --- a/docs/getting-started/integrate-auth/01_go.mdx +++ b/docs/getting-started/integrate-auth/01_go.mdx @@ -116,10 +116,10 @@ go run . will ask you to log into your Ory Console account. ```shell -npx @ory/cli tunnel --dev http://localhost:3000 +npx @ory/cli tunnel --dev http://localhost:3000 --project ``` -3. Open [http://localhost:4000](http://localhost:4000) to access the application. Since the initial call is made by an +3. Open [http://localhost:3000](http://localhost:3000) to access the application. Since the initial call is made by an unauthenticated user, the middleware doesn't detect a valid session and redirects to the login page of the defined Ory project.

From there, you can create a new account or sign in using an existing identity. When you sign in, the session becomes valid and the application shows the `Dashboard` page with the session data.