/go-auth

Github / Mail / Google auth example with Postgres, Redis and Memcached

Primary LanguageGoBoost Software License 1.0BSL-1.0

Github / Mail / Google auth example with Postgresql, Redis and Memcached

image image image

Requirements

- Go: 1.18

- PostgreSQL: 14.2

- Memcached: 1.6.15

- Redis: 6.2.6

- Google and Github keys


Setup OAuth

- Github

  1. Go to the Developer settings
  2. Create Application
  3. Enable User permissions -> Email addresses -> Read Only in the Permissions
  4. Generate secret token
  5. Enter the URIs that are allowed to be redirect-URIs (e.g. https://localhost:8080/oauth/github/callback)
  6. Paste both Client ID and Client Secret to the github_secret.json

- Google

  1. Go to the Google Cloud Console
  2. Create project (add content to the consent screen like title and logo) or use existing
  3. Credentials -> Create Credentials -> Oauth Client ID
  4. Choose the Web Application type and give it a name
  5. Enter the URIs that are allowed to be redirect-URIs (e.g. https://localhost:8080/oauth/google/callback)
  6. Paste both Client ID and Client Secret to the google_secret.json

- Mail SMTP

  1. Go to the Gmail Settings
  2. Enable IMAP in the Forwarding IMAP/POP
  3. Enable Less secure apps Access OPTIONAL
  4. Generate an App Password for mail access FROM May 30, 2022
  5. Paste the mail and the password to the config

docker-compose

Server is ready immediately after containers are up

SMTP_PASSWORD="mail_password" docker-compose up
  • Redis

    Port :6380 Password password Database 0
  • PostgreSQL

    Port :5431 User server Password pg_password Database auth_example

It is possible to additionally configure the app using environment variables

environment:
  POSTGRES_IP: 127.0.0.1 # connect to local database
  HOST_PORT: 8082 # change server port

Setup PostgreSQL

migrate -database ${POSTGRESQL_URL} -path migrate/ up

Down

migrate -database ${POSTGRESQL_URL} -path migrate/ down

Build / Run

git clone https://github.com/illiafox/go-auth.git auth
cd auth

make build
make run # /cmd/server/bin

Run arguments

HTTP mode

server -http

With non-standard config and log file paths

server -config config.toml -log log.txt

With reading from environment:

Available keys can be found in config structure tags

POSTGRES_PORT=4585 server -env

Logs

In addition to the terminal output, logs are also written to the file

# Terminal
20/05/2022 10:50:20 |   info    Initializing repository
20/05/2022 10:50:20 |   info    Done    {"time": 0.012092004}
20/05/2022 10:50:20 |   info    Server started at 0.0.0.0:8080
// File (default log.txt)
{"level":"info","ts":"Fri, 20 May 2022 10:23:52 EEST","msg":"Initializing repository"}
{"level":"info","ts":"Fri, 20 May 2022 10:23:52 EEST","msg":"Done","time":0.015016048}
{"level":"info","ts":"Fri, 20 May 2022 10:23:52 EEST","msg":"Server started at 0.0.0.0:8080"}

Endpoints

- Main Page /

- Register /register

- Login /login

- Logout /logout

- Mail verify /verify

- Github OAuth /oauth/github/login /oauth/github/callback

- Google OAuth /oauth/google/login /oauth/google/callback