Skip to content

A comprehensive guide and example project on setting up Firebase Authentication in a Go application.

Notifications You must be signed in to change notification settings

Cprime50/fire-go-auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fire-Go-Auth: Setting up Firebase authentication and RBAC in Go

Fire-Go

Overview

Authentication is really important for any app that wants to keep users data safe. Security and the protection of users' information is utmost priority. There are different methods for handling authentication, but for this particular case, we'll be focusing on using a third-party identity platform, Firebase. It is very quick to set up, works out of the box, and has a very generous free teir that we can leverage.

authentication flow

Article

I recommend checking out this article for a detailed walkthrough of the setup process. It has everything you need to know to understand and implement each step effectively.

Setting up Firebase authentication and RBAC in Go

Getting Started

To get started with this project, follow these steps:

  1. Clone the git repo:
git clone https://github.com/Cprime50/fire-go-auth.git
  1. Set Up Firebase Project: Create a new Firebase project or use an existing one in your firebase console

  2. Install the Go dependencys: cd into project folder and run

go mod tidy
  1. Obtain Your Firebase Private Key:

    • Navigate to the Firebase Console, under project settings, service accounts and download your project's private key.
    • For security, store this key in a .env file.
  2. Create a .env File:

    • In the root directory of the project, create a .env file.
    • Add the following details to the .env file:
ADMIN_EMAIL= youremail@mail.com

PORT=:3000

FIREBASE_KEY= your_private_key.json

Replace youremail@mail.com with your admin email, and path/to/your_private_key.json with the path to your Firebase private key.

  • Admin Email: This email will be set as the default admin when authenticated
  1. Configure Your Client Application And Make a Request To The Server: Setup your client to use firebase to authenticate users

After a user is authenticated, you send their access token to the server to authenticate each request. This is typically done by including the ID token in the Authorization header of your HTTP requests.

example:

function sendTokenToServer(accessToken) {
    fetch('localhost:3000/user', {
        method: 'GET',
        headers: {
            'Authorization': 'Bearer ' + accessToken
        }
    }).then(response => {
    }).catch(error => {
        console.error('Error:', error);
    });
}

example response

Contributing

Contributions are welcome! If you have suggestions for improvements or encounter any issues, please feel free to open an issue or submit a pull request.

About

A comprehensive guide and example project on setting up Firebase Authentication in a Go application.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages