admin

package module
v0.0.0-...-fbe8b33 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2017 License: MIT Imports: 18 Imported by: 0

README

go-firebase-admin

Go Report Card GoDoc

Work in Progress

Firebase Admin SDK for Golang

Usage

Init
package main

import (
  "io/ioutil"
  "log"

  "github.com/acoshift/go-firebase-admin"
)

func main() {
  // Init App
  serviceAccount, _ := ioutil.ReadFile("service_account.json")
  firApp, err := admin.InitializeApp(admin.ProjectID("YOUR_PROJECT_ID"), admin.ServiceAccount(serviceAccount))
  if err != nil {
    panic(err)
  }
  firAuth := firApp.Auth()

  // ...
}
CreateCustomToken
userID := "12345678"
claims := map[string]interface{}{"isAdmin": true}
token, err := firAuth.CreateCustomToken(userID, claims)
VerifyIDToken
idToken := "ID_TOKEN_FROM_CLIENT"
claims, err := firAuth.VerifyIDToken(idToken)
if err != nil {
  panic(err)
}

userID := claims.UserID
log.Println(userID)
GetAccountInfoByUID
user, err := firAuth.GetAccountInfoByUID("123312121")
GetAccountInfoByUIDs
users, err := firAuth.GetAccountInfoByUIDs([]string{"123312121", "2433232", "12121211"})
GetAccountInfoByEmail
user, err := firAuth.GetAccountInfoByEmail("abc@gmail.com")
GetAccountInfoByEmails
users, err := firAuth.GetAccountInfoByEmails([]string{"abc@gmail.com", "qqq@hotmail.com", "aaaqaq@aaa.com"})
DeleteAccount
err := firAuth.DeleteAccount("USER_ID")
CreateAccount
userID, err := firApp.CreateAccount(&admin.Account{
  Email:         "aaa@bbb.com",
  EmailVerified: true,
  Password:      "12345678",
  DisplayName:   "AAA BBB",
})
ListAccount
cursor := firApp.ListAccount(100)
for {
  users, err := cursor.Next()
  if users == nil || err != nil {
    break
  }
  log.Println(len(users))
}
UpdateAccount
err := firApp.UpdateAccount(&UpdateAccount{
  LocalID: "12121212",
  Email: "new_email@email.com",
  Password: "new_password",
  DisplayName: "new name",
})

Documentation

Overview

Package admin is the unofficial firebase admin sdk: https://firebase.google.com/docs/auth/admin/

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRequireServiceAccount = errors.New("firebase: requires service account")
	ErrRequireUID            = errors.New("firebaseauth: require user id")
)

Error constants

View Source
var (
	ServerValueTimestamp interface{} = map[string]string{".sv": "timestamp"}
)

ServerValue

Functions

This section is empty.

Types

type App

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

App holds information about application configuration

func InitializeApp

func InitializeApp(options AppOptions) (*App, error)

InitializeApp initializes firebase application with options

func (*App) Auth

func (app *App) Auth() (*Auth, error)

Auth creates new Auth instance each instance has the save firebase app instance but difference public keys instance better create only one instance

func (*App) Database

func (app *App) Database() *Database

Database creates new Database instance

type AppOptions

type AppOptions struct {
	ProjectID      string
	ServiceAccount []byte
	DatabaseURL    string
}

AppOptions is the firebase app options for initialize app

type Auth

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

Auth type

func (*Auth) CreateCustomToken

func (auth *Auth) CreateCustomToken(userID string, claims interface{}) (string, error)

CreateCustomToken creates a custom token used for client to authenticate with firebase server using signInWithCustomToken https://firebase.google.com/docs/auth/admin/create-custom-tokens

func (*Auth) CreateUser

func (auth *Auth) CreateUser(user *User) (*UserRecord, error)

CreateUser creates an user if not provides UserID, firebase server will auto generate

func (*Auth) DeleteUser

func (auth *Auth) DeleteUser(userID string) error

DeleteUser deletes an user by user id

func (*Auth) GetUser

func (auth *Auth) GetUser(uid string) (*UserRecord, error)

GetUser retrieves an user by user id

func (*Auth) GetUserByEmail

func (auth *Auth) GetUserByEmail(email string) (*UserRecord, error)

GetUserByEmail retrieves user by email

func (*Auth) GetUsers

func (auth *Auth) GetUsers(userIDs []string) ([]*UserRecord, error)

GetUsers retrieves users by user ids

func (*Auth) GetUsersByEmail

func (auth *Auth) GetUsersByEmail(emails []string) ([]*UserRecord, error)

GetUsersByEmail retrieves users by emails

func (*Auth) ListUsers

func (auth *Auth) ListUsers(maxResults int64) *ListAccountCursor

ListUser creates list account cursor for retrieves accounts MaxResults can change later after create cursor

func (*Auth) UpdateUser

func (auth *Auth) UpdateUser(user *User) (*UserRecord, error)

UpdateUser updates an existing user

func (*Auth) VerifyIDToken

func (auth *Auth) VerifyIDToken(idToken string) (*Claims, error)

VerifyIDToken validates given idToken return Claims for that token only valid token

type CancelFunc

type CancelFunc func()

CancelFunc is the function for cancel "On"

type ChildSnapshot

type ChildSnapshot struct {
	PrevChildKey string
}

ChildSnapshot type

type Claims

type Claims struct {
	Issuer    string      `json:"iss,omitempty"`
	Subject   string      `json:"sub,omitempty"`
	Audience  string      `json:"aud,omitempty"`
	IssuedAt  int64       `json:"iat,omitempty"`
	ExpiresAt int64       `json:"exp,omitempty"`
	UserID    string      `json:"uid,omitempty"`
	Claims    interface{} `json:"claims,omitempty"`
}

Claims is the firebase authentication token claims

func (*Claims) Valid

func (c *Claims) Valid() error

Valid implements jwt-go Claims interface for validates time based claims, such as IssuedAt, and ExpiresAt But not verify token signature and header

type DataSnapshot

type DataSnapshot struct {
}

DataSnapshot type

type Database

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

Database type

func (*Database) Ref

func (database *Database) Ref(path string) *Reference

Ref returns a Reference for a path

type ErrTokenInvalid

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

ErrTokenInvalid is the invalid token error

func (*ErrTokenInvalid) Error

func (err *ErrTokenInvalid) Error() string

Error implements error interface

type ListAccountCursor

type ListAccountCursor struct {
	MaxResults int64
	// contains filtered or unexported fields
}

ListAccountCursor type

func (*ListAccountCursor) Next

func (cursor *ListAccountCursor) Next() ([]*UserRecord, error)

Next retrieves next users from cursor which limit to MaxResults then move cursor to the next users

type OldChildSnapshot

type OldChildSnapshot struct {
}

OldChildSnapshot type

type Query

type Query interface {
	Ref() *Reference
	EndAt(value interface{}, key string) Query
	EqualTo(value interface{}, key string) Query
	IsEqual(other interface{}) Query
	LimitToFirst(limit int) Query
	LimitToLast(limit int) Query
	OrderByChild(path string) Query
	OrderByKey() Query
	OrderByPriority() Query
	OrderByValue() Query
	StartAt(value interface{}, key string) Query
	OnValue(event chan *DataSnapshot) CancelFunc
	OnChildAdded(event chan *ChildSnapshot) CancelFunc
	OnChildRemoved(event chan *OldChildSnapshot) CancelFunc
	OnChildChanged(event chan *ChildSnapshot) CancelFunc
	OnChildMoved(event chan *ChildSnapshot) CancelFunc
	OnceValue() *DataSnapshot
	OnceChildAdded() *ChildSnapshot
	OnceChildRemove() *OldChildSnapshot
	OnceChildChanged() *ChildSnapshot
	OnceChildMoved() *ChildSnapshot
	String() string
}

Query is the query interface

type Reference

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

Reference represents a specific location in Database

func (*Reference) Push

func (ref *Reference) Push(value interface{}) error

Push pushs data to current location

func (*Reference) Remove

func (ref *Reference) Remove() error

Remove removes data from current location

func (*Reference) Set

func (ref *Reference) Set(value interface{}) error

Set writes data to current location

type UpdateAccount

type UpdateAccount struct {
	// UserID is the existing user id to update
	UserID        string `json:"localId,omitempty"`
	Email         string `json:"email,omitempty"`
	EmailVerified bool   `json:"emailVerified,omitempty"`
	Password      string `json:"password,omitempty"`
	DisplayName   string `json:"displayName,omitempty"`
	PhotoURL      string `json:"photoUrl,omitempty"`
	Disabled      bool   `json:"disableUser,omitempty"`
}

UpdateAccount use for update existing account

type User

type User struct {
	UserID        string
	Email         string
	EmailVerified bool
	Password      string
	DisplayName   string
	PhotoURL      string
	Disabled      bool
}

User use for create new user use Password when create user (plain text) use RawPassword when update user (hashed password)

type UserInfo

type UserInfo struct {
	UserID      string
	Email       string
	DisplayName string
	PhotoURL    string
	ProviderID  string
}

UserInfo is the user provider information

type UserMetadata

type UserMetadata struct {
	CreatedAt      time.Time
	LastSignedInAt time.Time
}

type UserRecord

type UserRecord struct {
	UserID        string
	Email         string
	EmailVerified bool
	DisplayName   string
	PhotoURL      string
	Disabled      bool
	Metadata      UserMetadata
	ProviderData  []*UserInfo
}

UserRecord is the firebase authentication user

Jump to

Keyboard shortcuts

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