http://localhost:3000/callback
.http://localhost:3000
.go.mod
file to list all the dependencies in your application.To integrate Auth0 in a Go application, add the coreos/go-oidc/v3
and x/oauth2
packages.In addition to the OIDC and OAuth2 packages, add joho/godotenv
, gin-gonic/gin
, and
gin-contrib/sessions
.gin
for routing, but you can use whichever router you want.go.mod
file with the necessary dependencies and install them using the following command in
your terminal:.env
within the root of your project directory:router.go
in the platform/router
folder. In this package, create a
method to configure and return our routes using github.com/gin-gonic/gin. You will be passing an instance of
Authenticator
to the method, for use with the login
and callback
handlers./login
route.Create a file called login.go
in the web/app/login
folder, and add a
Handler
function. Upon executing the handler, the user will be redirected to Auth0 where they can
enter their credentials.To call the /login
route, add a link to /login
in the home.html
template
located in the web/template
directory./callback
route.Create a file called callback.go
in the web/app/callback
folder, and add a
Handler
function.This handler will take the code
query string, provided by Auth0, and exchange it for an ID token and
an access token.If the ID token is valid, it will store the profile information and access token in the session. The profile
information is based on the claims contained in the ID token. Session storage allows the application to access
that information as needed.profile
that was stored in the session previously.Create a handler for the /user
endpoint in web/app/user/user.go
and return the
corresponding HTML file. As the profile
is being passed to ctx.HTML()
, you can access
the profile information such as picture
and nickname
inside that same HTML file.An example of such an HTML file could look like the example below, but you can retrieve any profile information,
including custom claims.logout.go
in the folder web/app/logout
, and add the function
Handler
to redirect the user to Auth0’s logout endpoint.The returnTo
URL needs to be in the list of Allowed Logout URLs in the settings section of the
application, For more information, see Redirect Users After Logout.Create a file called user.js
in the folder web/static/js
, and add the code to remove
the cookie from a logged-in user.isAuthenticated.go
in platform/middleware
and add a function that
checks if the user is authenticated or not based on the profile
session key. If the user is not
authenticated, the middleware will redirect the user to the root of the application.With the middleware created, we can set it up for any route that needs authentication by adding it to the router.main.go
, create an instance of the authenticator and the router, which gets passed the
authenticator instance.If you are using a .env
file, you must call godotenv.Load()
at the very beginning of
the main()
function.Serve your application by using the following command in your terminal: