firebase

package
v0.0.0-...-94a378c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2022 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package firebase wraps the firebase Admin API for use in a client

Index

Constants

View Source
const (
	// DefaultUser represents the default user to be loaded and stored in the
	// `UserCache`.
	DefaultUser = "default"
)

Variables

This section is empty.

Functions

func NewOAuthConfig

func NewOAuthConfig(provider AuthProvider) *oauth2.Config

NewOAuthConfig creates a new `oauth.Config` for the provided `AuthProvider` based on the built in auth provider configs.

Types

type App

type App struct {
	APIKey            string
	AuthDomain        string
	DatabaseURL       string
	ProjectID         string
	StorageBucket     string
	MessagingSenderID string
	AppID             string
	MeasurementID     string
	ClientID          string
	ClientSecret      string
}

App represents a firebase application

var DefaultApp App

DefaultApp is the default, built in, firebase application

func (*App) NewAuth

func (app *App) NewAuth(opts ...AuthOption) *Auth

NewAuth creates a new authentication client based on the `APIKey`, `ClientID`, and `ClientSecret` in the `App`. You can specify any number of additional AuthOptions when creating the new client.

func (*App) NewFirestore

func (app *App) NewFirestore(ctx context.Context, auth *Auth) (*firestore.Client, error)

NewFirestore creates a new Firestore client based on the `ProjectID` in the App. It uses `auth` for authenticating with `auth.CurrentUser()`.

type Auth

type Auth struct {
	// contains filtered or unexported fields
}

Auth handles authenticating users

func (*Auth) CurrentUser

func (auth *Auth) CurrentUser() *User

CurrentUser returns the current authenticated user or nil if no user is authenticated.

func (*Auth) SignInWithConsole

func (auth *Auth) SignInWithConsole(
	ctx context.Context, cfg *oauth2.Config) (*User, *oauth2.Token, error)

SignInWithConsole authenticates the current user by outputting an authentication URL to stdout. The user should copy that URL in to their browser and paste the authentication code back in to the command line.

func (*Auth) SignInWithConsoleWithIO

func (auth *Auth) SignInWithConsoleWithIO(
	ctx context.Context, cfg *oauth2.Config,
	reader io.Reader, writer io.Writer) (*User, *oauth2.Token, error)

SignInWithConsoleWithIO performs the same operation as SignInWithConsole, but allows for specifying an input `reader` and output `writer.

func (*Auth) SignInWithToken

func (auth *Auth) SignInWithToken(
	ctx context.Context, token *oauth2.Token) (*User, *oauth2.Token, error)

SignInWithToken takes an auth token generated by firebase and exchanges it for a refresh token and a user.

func (*Auth) SignInWithUser

func (auth *Auth) SignInWithUser(ctx context.Context,
	user *User) (token *oauth2.Token, err error)

SignInWithUser authenticates with a firebase user with a refresh token.

func (*Auth) SignOut

func (auth *Auth) SignOut()

SignOut deletes the current user from the cache and then resets the current user and token.

func (*Auth) Token

func (auth *Auth) Token() (token *oauth2.Token, err error)

Token implements the TokenSource interface by providing authenticated tokens for the current user.

type AuthOption

type AuthOption interface {
	Apply(auth *Auth)
}

AuthOption is a generic interface used to apply options when creating a new authentication client.

func WithEmulatorHost

func WithEmulatorHost(emulatorHost string) AuthOption

WithEmulatorHost specifies the authentication emulator host to use in testing. Loading from environment variables is not supported.

func WithUserCache

func WithUserCache(uc UserCache) AuthOption

WithUserCache provides a user cache for the authentication client. This can be used to preserve users across a session.

type AuthProvider

type AuthProvider int

AuthProvider is an enumeration of supported authentication providers.

const (
	// GoogleAuthProvider provides support for authenticating with Google
	GoogleAuthProvider AuthProvider = iota
)

type MemoryUserCache

type MemoryUserCache map[string]*User

MemoryUserCache implements the `UserCache` interface with an in-memory map.

func (*MemoryUserCache) Delete

func (muc *MemoryUserCache) Delete(userID string)

Delete deletes a user from the cache.

func (MemoryUserCache) Get

func (muc MemoryUserCache) Get(userID string) (*User, error)

Get fetches a user from the cache.

func (*MemoryUserCache) Set

func (muc *MemoryUserCache) Set(userID string, user *User) error

Set sets a user in the cache.

type User

type User struct {
	UserID        string `json:"userId"`
	EmailVerified bool   `json:"emailVerified"`
	Email         string `json:"email"`
	DisplayName   string `json:"displayName"`
	PhotoURL      string `json:"photoUrl"`
	RefreshToken  string `json:"refreshToken"`
}

User contains details of a user from Firebase

type UserCache

type UserCache interface {
	// Get fetches a user from the cache. If no user is found, nil is returned
	// with a nil error. If a failure occurs an error is returned.
	Get(string) (*User, error)
	// Set sets a user in the cache.
	Set(string, *User) error
	// Delete deletes a user from the cache.
	Delete(string)
}

UserCache provides an interface for caching authenticated users and their `RefreshToken`. You should avoid backing this with insecure storage like a plaintext file.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL