Git Product home page Git Product logo

firebase-admin-ex's Introduction

Firebase Admin Elixir SDK

Build Status

Overview

The Firebase Admin Elixir SDK enables access to Firebase services from privileged environments (such as servers or cloud) in Elixir.

For more information, visit the Firebase Admin SDK setup guide.

Installation

  • Add firebase_admin_ex to your list of dependencies in mix.exs:
defmodule YourApplication.Mixfile do
  use Mix.Project

  # Run "mix help deps" to learn about dependencies.
  defp deps do
    [
      {:firebase_admin_ex, "~> 0.1.0"}
    ]
  end
end

Next, run mix deps.get to pull down the dependencies:

$ mix deps.get

Now you can make an API call by obtaining an access token and using the generated modules.

Obtaining an Access Token

Authentication is typically done through Application Default Credentials which means you do not have to change the code to authenticate as long as your environment has credentials.

Start by creating a Service Account key file. This file can be used to authenticate to Google Cloud Platform services from any environment. To use the file, set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path to the key file. Alternatively you may configure goth (the the authentication ssyas described at https://github.com/peburrows/goth#installation

For example:

$ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json

If you are deploying to App Engine, Compute Engine, or Container Engine, your credentials will be available by default.

Usage

Messaging

  • Sending a WebMessage
# Get your device registration token
registration_token = "user-device-token"

# Define message payload attributes
message = FirebaseAdminEx.Messaging.Message.new(%{
  data: %{},
  token: registration_token,
  webpush: FirebaseAdminEx.Messaging.WebMessage.Config.new(%{
    headers: %{},
    data: %{},
    title: "notification title",
    body:  "notification body",
    icon:  "https://icon.png"
  })
})

# Call the Firebase messaging V1 send API
project_id = "YOUR-FIREBASE-PROJECT-ID"
{:ok, response} = FirebaseAdminEx.Messaging.send(project_id, message)
  • Sending a AndroidMessage
# Get your device registration token
registration_token = "user-device-token"

# Define message payload attributes
message = FirebaseAdminEx.Messaging.Message.new(%{
  data: %{},
  token: registration_token,
  android: FirebaseAdminEx.Messaging.AndroidMessage.Config.new(%{
    headers: %{},
    data: %{},
    title: "notification title",
    body:  "notification body",
    icon:  "https://icon.png"
  })
})

# Call the Firebase messaging V1 send API
project_id = "YOUR-FIREBASE-PROJECT-ID"
{:ok, response} = FirebaseAdminEx.Messaging.send(project_id, message)
  • Sending a APNSMessage
# Get your device registration token
registration_token = "user-device-token"

# Define message payload attributes
message = FirebaseAdminEx.Messaging.Message.new(%{
  data: %{},
  token: registration_token,
  apns: FirebaseAdminEx.Messaging.APNSMessage.Config.new(%{
    headers: %{},
    payload: %{
      aps: %{
        alert: %{
          title: "Message Title",
          body: "Message Body"
        },
        sound: "default",
        "content-available": 1
      },
      custom_data: %{}
    }
  })
})

# Call the Firebase messaging V1 send API
project_id = "YOUR-FIREBASE-PROJECT-ID"
{:ok, response} = FirebaseAdminEx.Messaging.send(project_id, message)

Authentication Management

The FirebaseAdminEx.Auth module allows for some limited management of the Firebase Autentication system. It currently supports getting, deleting and creating users.

  • Getting a user by uid:
iex(1)> FirebaseAdminEx.Auth.get_user("hYQIfs35Rfa4UMeDaf8lhcmUeTE2")
{:ok,
 "{\n  \"kind\": \"identitytoolkit#GetAccountInfoResponse\",\n  \"users\": [\n    {\n      \"localId\": \"hYQIfs35Rfa4UMeDaf8lhcmUeTE2\",\n      \"providerUserInfo\": [\n        {\n          \"providerId\": \"phone\",\n          \"rawId\": \"+61400000111\",\n          \"phoneNumber\": \"+61400000111\"\n        }\n      ],\n      \"lastLoginAt\": \"1543976568000\",\n      \"createdAt\": \"1543976568000\",\n      \"phoneNumber\": \"+61400000111\"\n    }\n  ]\n}\n"}
  • Getting a user by phone number
iex(1)> FirebaseAdminEx.Auth.get_user_by_phone_number("+61400000111")
{:ok,
 "{\n  \"kind\": \"identitytoolkit#GetAccountInfoResponse\",\n  \"users\": [\n    {\n      \"localId\": \"hYQIfs35Rfa4UMeDaf8lhcmUeTE2\",\n      \"providerUserInfo\": [\n        {\n          \"providerId\": \"phone\",\n          \"rawId\": \"+61400000111\",\n          \"phoneNumber\": \"+61400000111\"\n        }\n      ],\n      \"lastLoginAt\": \"1543976568000\",\n      \"createdAt\": \"1543976568000\",\n      \"phoneNumber\": \"+61400000111\"\n    }\n  ]\n}\n"}
  • Getting a user by email address
iex(1)> FirebaseAdminEx.Auth.get_user_by_email("[email protected]")
{:ok,
 "{\n  \"kind\": \"identitytoolkit#GetAccountInfoResponse\",\n  \"users\": [\n    {\n      \"localId\": \"hYQIfs35Rfa4UMeDaf8lhcmUeTE2\",\n      \"providerUserInfo\": [\n        {\n          \"providerId\": \"phone\",\n          \"rawId\": \"+61400000111\",\n          \"phoneNumber\": \"+61400000111\"\n        \"email\": \"[email protected]\"\n      }\n      ],\n      \"lastLoginAt\": \"1543976568000\",\n      \"createdAt\": \"1543976568000\",\n      \"phoneNumber\": \"+61400000111\"\n    }\n  ]\n}\n"}
  • Deleting a user
iex(4)> FirebaseAdminEx.Auth.delete_user("hYQIfs35Rfa4UMeDaf8lhcmUeTE2")
{:ok, "{\n  \"kind\": \"identitytoolkit#DeleteAccountResponse\"\n}\n"}
  • Creating a user
iex(4)> FirebaseAdminEx.Auth.create_email_password_user(%{"email" => "[email protected]", "password" => "hYQIfs35Rfa4UMeDaf8lhcmUeTE2"})
{:ok,
 "{\n  \"kind\": \"identitytoolkit#SignupNewUserResponse\",\n  \"email\": \"[email protected]\",\n  \"localId\": \"s5dggHJyr3fgdgJkLe234G6h6y\"\n}\n"}
  • Generating the email action link for sign-in flows
# Define ActionCodeSettings
action_code_settings = 
  FirebaseAdminEx.Auth.ActionCodeSettings.new(
    %{
      requestType: "EMAIL_SIGNIN",
      email: "[email protected]",
      returnOobLink: true,
      continueUrl: "www.test.com/sign-in",
      canHandleCodeInApp: false,
      dynamicLinkDomain: "",
      androidPackageName: "",
      androidMinimumVersion: "",
      androidInstallApp: false,
      iOSBundleId: ""
    }
  )
client_email = "YOUR-FIREBASE-CLIENT-EMAIL"
project_id = "YOUR-FIREBASE-PROJECT-ID"
iex(4)> FirebaseAdminEx.Auth.generate_sign_in_with_email_link(action_code_settings, client_email, project_id)
{:ok,
 "{\n  \"kind\": \"identitytoolkit#GetOobConfirmationCodeResponse\",\n  \"email\": \"[email protected]\",\n  \"oobLink\": \"https://YOUR-FIREBASE-CLIENT.firebaseapp.com/__/auth/action?mode=signIn&oobCode=xcdwelFRvfbtghHjswvw2f3g46hh6j8&apiKey=Fgae35h6j78_vbsddgs34th6h6hhekj97gfj&lang=en&continueUrl=www.test.com/sign-in\"\n}\n"}

Firebase Documentation

License and Terms

Your use of Firebase is governed by the Terms of Service for Firebase Services.

Disclaimer

This is not an officially supported Google product.

firebase-admin-ex's People

Contributors

duzzifelipe avatar martinjhm271 avatar nitinstp23 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

firebase-admin-ex's Issues

Can't send a message

Thank you for this package. I'm trying to use it in order to send a notification for mobile app using Firebase, but I can't.
When I try to send a message I'm always receiving this error:

[error] #PID<0.727.0> running BckoWeb.Endpoint (connection #PID<0.725.0>, stream id 2) terminated
Server: localhost:4003 (http)
Request: GET /api/v1/news/1/push
** (exit) an exception was raised:
    ** (MatchError) no match of right hand side value: :error
        (firebase_admin_ex 0.1.0) lib/messaging.ex:17: FirebaseAdminEx.Messaging.send/2

I created a new account on Google Cloud Platform, I downloaded its json and set the path on GOOGLE_APPLICATION_CREDENTIALS. So I don't know why I'm receiving this error on Goth.Config.get(client_email, :project_id) line.

How to solve that?

Not available on hex?

Hi, I'm interested in using this library but it seems like it hasn't been published to hex yet. Why not? Or is it and I just can't find it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.