This Spring Boot application is set up to use Google OAuth as it’s authentication scheme.
Setting this up on localhost requires the first two steps below; getting this to work on Heroku requires an additional third step.
.env
file with these values..env
values to the Heroku app’s configuration values.Each of these three steps is explained in more detail below.
.env
and .env.SAMPLE
files.The .env
file is created by copying it from .env.SAMPLE
and then editing it, e.g.
cp .env.SAMPLE .env
.env
and .env.SAMPLE
will not show up in regular directory listings; files starting with .
are considered
hidden files. Use ls -a
, or configure your Mac finder/Windows explorer to show hidden files..env
, NOT in .env.SAMPLE
.env
is never committed to the GitHub repo.env
vs. .env.SAMPLE
on this page if you are interested: docs/environment-variables.Login to the Google Developer Console at https://console.cloud.google.com/.
Create a new project on the top left (or select the project you would like to create your OAuth app in)
On the upper left, there is a hamburger menu icon that provides a left navigation menu.
APIs and Services
then Credentials
.+ CREATE CREDENTIALS
OAuth Client ID
Web Application
Authorized redirect URIs
Under Authorized redirect URIs
, you’ll need to click the + ADD URI
button twice to enter two addresses:
http://localhost:8080/login/oauth2/code/google
http
not https
https://myappname.herokuapp.com/login/oauth2/code/google
my-app-name
https
not http
Then click the blue CREATE
button.
You will now see the client id and client secret values.
Keep this window open, since you’ll need these values in the next step.
.env.SAMPLE
to .env
and enter valuesIn the frontend directory, use this command to copy .env.SAMPLE
to .env
. Recall that you
may need to use ls -a
to get the files to show up, since they are hidden files on Unix-like systems.
cp .env.SAMPLE .env
The file .env.SAMPLE
should not be edited; it is intended to
be a template for creating a file called .env
that contains
your repository secrets.
The .env
is in the .gitignore
because **a file containing secrets should NOT be committed to GitHub, not even in a private repo.
After copying, the file .env
looks like this:
GOOGLE_CLIENT_ID=see-instructions
GOOGLE_CLIENT_SECRET=see-instructions
ADMIN_EMAILS=phtcon@ucsb.edu
Replace see-instructions
with the appropriate values.
For ADMIN_EMAILS, add your own email and any teammates you are collaborating with after phtcon.ucsb.edu; you can separate multiple emails with commas, e.g.
ADMIN_EMAILS=phtcon@ucsb.edu,cgaucho@ucsb.edu,ldelplaya@ucsb.edu
With this done, you should be all set to run on localhost.
For Heroku, there is one more step.
.env
values to HerokuThe easy way, using the Heroku CLI:
(Note: if you don’t access to the Heroku CLI, scroll down to “the tedious way”)
heroku login
Use this command, with the name of your app in place of my-heroku-app
heroku config:set --app my-heroku-app `cat .env`
You should get output like this:
Setting GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, ADMIN_EMAILS and restarting ⬢ demo-spring-react-example... done, v6
You can check the values by visiting the Settings
tab
in the Heroku Dashboard, and clicking Reveal Config Vars
If the command fails with the following error:
is invalid. Must be in the format FOO=bar.
Ensure that your .env
file does not have any empty lines, then retry the command.
The slightly more tedious way:
Settings
tab
then click Reveal Config Vars
.For each variable in .env
, create a Config Var entry
with the corresponding name and value.
Be sure that you preserve case: if it’s CLIENT_SECRET
, you must use CLIENT_SECRET
not client_secret
.
Deploy
tab and clicking Deploy Branch
.If you see this:
Try clicking the little arrow to open up the additional message:
Now, you’ll see the Redirect URI that the app is expecting.
If you go back to the Google Developer Console you can see what you really entered.
For example, when I was getting this error message, it’s because I put in this for my Redirect URI:
Rookie mistake! I literally had my-heroku-app
instead of demo-spring-react-example
.
Change it to the correct URI, click save. Then go back to the URL for the home page of your app and refresh the page (you don’t need to restart the Heroku backend; just refresh your browser page.) Click login again, and you should get something like this:
Success!