ios集成firebase_如何使用Firebase將Google Login集成到Ionic應用程序中

ios集成firebase

by Ryan Gordon

通過瑞安·戈登(Ryan Gordon)

如何使用Firebase將Google Login集成到Ionic應用程序中 (How to integrate Google Login into an Ionic app with Firebase)

A lot of apps these days need to maintain some form of user authentication. This helps users manage their accounts and store their info securely. But account creation can be a bad experience for some users. Some do not want to have another account on some website where they have to remember yet another password, because their uncle Mick recommended never using the same password.

如今,許多應用程序都需要維護某種形式的用戶身份驗證。 這可以幫助用戶管理其帳戶并安全地存儲其信息。 但是,創建帳戶對于某些用戶而言可能是不好的體驗。 有些人不想在某個網站上擁有另一個帳戶,而在該網站上他們必須記住另一個密碼,因為他們的叔叔米克建議不要使用相同的密碼。

In the interest of improving the UX for these users, it’s helpful implement a way to sign in with accounts they already have such as Google, Facebook, or Github.

為了改善這些用戶的UX,實現一種方法來登錄他們已經擁有的帳戶(例如Google,Facebook或Github)會很有幫助。

OAuth is how we can achieve this. Each of these providers and many others provide OAuth Authentication which we can use to sign these users in with their existing information. Furthermore, if a user likes to sign in with multiple providers, we can link one user account to one or more providers. This means that a user can sign in with either Google or Facebook and still access the same account in our system.

OAuth是我們可以實現這一目標的方法。 這些提供程序中的每一個以及許多其他提供程序都提供OAuth身份驗證,我們可以使用這些身份驗證將這些用戶與他們的現有信息一起登錄。 此外,如果用戶喜歡使用多個提供商登錄,我們可以將一個用戶帳戶鏈接到一個或多個提供商。 這意味著用戶可以使用Google或Facebook登錄并仍然訪問我們系統中的同一帳戶。

Do you just want the code instead of following the post? Check out the repo (and give it a star in you find it helpful)!

您只是想要代碼,而不是關注帖子嗎? 查看回購協議 (并給它加星號,對您有幫助)!

To follow along with this tutorial , youll need both Node.js and Ionic installed.

要繼續本教程,您需要同時安裝Node.js和Ionic。

入門 (Getting started)

To install Ionic and Cordova (which for the moment is needed for plugins), run the following in terminal after installing Node:

要安裝Ionic和Cordova(目前需要插件),請在安裝Node之后在終端中運行以下命令:

npm install -g ionic cordova

If you get EACCES: permission denied, you may need to run the command with sudo ?

如果獲得EACCES:權限被拒絕,則可能需要使用sudo運行命令?

To create an app with Ionic, start <appname> <template>. For this, we’ll use a blank template as a starting point .

要使用Ionic創建應用,請啟動<appname> <template>。 為此,我們將使用空白模板作為起點。

The code for Google signin will be put into a provider class which will be called by whichever page needs to use that sign in method.

Google登錄代碼將被放置到提供程序類中,該類將由需要使用該登錄方法的任何頁面調用。

ionic g provider auth

使用Firebase設置應用并獲取憑據 (Setup app with Firebase and get credentials)

In order for firebase to work with both the native Android and iOS platforms, we’ll need to do two configurations for the app, each a little bit different.

為了使Firebase能夠與本機Android和iOS平臺一起使用,我們需要為該應用程序進行兩項配置,每項配置都稍有不同。

For each device, we will need to configure the Google Sign In API.

對于每臺設備,我們都需要配置Google登錄API。

的iOS (iOS)

For iOS setup, you need to supply the bundle ID. This is the value in config.xml which is usually set to io.ionic.starter. Change that to whatever you like, and then that will be your bundle ID.

對于iOS設置,您需要提供捆綁包ID。 這是config.xml中的值,通常將其設置為io.ionic.starter。 將其更改為您喜歡的任何內容,然后將其作為您的捆綁包ID。

Make sure you don’t leave it as the default value.

確保不要將其保留為默認值。

After setup, you will get a GoogleService-Info.plist. Save this file to the root of the Ionic project folder. After performing these steps, your done! For iOS at least..

設置完成后,您將獲得一個GoogleService-Info.plist。 將此文件保存到Ionic項目文件夾的根目錄。 完成這些步驟后,您就完成了! 至少適用于iOS。

You should now see the following UI and have your Plist file downloaded and ready to use.

現在,您應該看到以下UI,并下載了Plist文件并可以使用。

安卓系統 (Android)

The process for adding Google sign into the Android platform is almost identical, with one extra requirement. To get started, head to this link, and start creating your Android app.

將Google標牌添加到Android平臺的過程幾乎相同,但有一個額外的要求。 首先,請轉到此鏈接 ,然后開始創建您的Android應用。

It’s important to note that for building to Android (and also for using this plugin) you will need to install the Android build tools version 19.1.0 or higher.

請務必注意,要構建到Android(以及使用此插件),您將需要安裝Android構建工具版本19.1.0或更高版本。

取得鑰匙? (Acquiring the key ?)

Once the build tools are installed and you’ve confirmed this, the keystore should be available to get the SHA-1 value we need for the Android part of the process. The keystore is used to hold the signing keys you use for the apps you build.

一旦安裝了構建工具并確認了這一點,就可以使用密鑰庫來獲取過程中Android部分所需的SHA-1值。 密鑰庫用于保存用于構建的應用程序的簽名密鑰。

Run this command in the terminal to acquire the SHA-1 value need:

在終端中運行以下命令以獲取所需的SHA-1值:

keytool -exportcert -list -v -alias androiddebugkey -keystore ~/.android/debug.keystore

Important: This will be different from the keystore you would use if you are taking the app into production. Stop at this point and reflect — will you release this? It may be wise to use the release keystore if so.

重要提示 :這與將應用程序投入生產時將使用的密鑰庫不同。 到此為止停止思考,您會釋放嗎? 如果這樣的話,使用發布密鑰庫可能是明智的。

After this step, head to this link, and add your Android project.

完成此步驟后,請轉到此鏈接 ,然后添加您的Android項目。

Make sure again that you have changed the config.xml ‘id’ value for your project so that it is no longer io.ionic.starter .

再次確保更改了項目的config.xml'id'值,以使其不再是io.ionic.starter

It will ask for the SHA-1 value we got from terminal. Input it and you’ll be given the option to download a google-services.json file. Download this file and save it to the root of your project. Keep it safe!

它將要求我們從終端獲得的SHA-1值。 輸入它,您將可以選擇下載google-services.json文件。 下載此文件并將其保存到項目的根目錄。 保持安全!

將Google登錄安裝到您的代碼庫 (Installing Google Sign-in to your codebase)

One of the good things about Ionic is its documentation. It has docs available for most of the plugins and components. The Google Sign-in docs for Ionic can be found here.

Ionic的優點之一是其文檔。 它具有可用于大多數插件和組件的文檔。 可在此處找到 Ionic的Google登錄文檔。

Run these commands in the terminal to add the plugin to the project.

在終端中運行這些命令以將插件添加到項目中。

$ ionic cordova plugin add cordova-plugin-googleplus --variable REVERSED_CLIENT_ID=myreversedclientid
$ npm install --save @ionic-native/google-plus

At this stage, you have the iOS and Android Google Sign-in API configured. You have two config files (one for each platform) and two plugins installed and ready to use.

在此階段,您已經配置了iOS和Android Google登錄API。 您有兩個配置文件(每個平臺一個)和兩個已安裝并可以使用的插件。

配置Firebase (Configuring Firebase)

Firebase will be the host for the OAuth sign-ins like Google plus. Before it can be used in the project, you need to set up the project in Firebase. If you have never done this before, head to this post on how to setup a Firebase config and initialize Firebase itself.

Firebase將成為OAuth登錄(如Google plus)的主機。 您必須先在Firebase中設置項目,然后才能在項目中使用它。 如果您以前從未這樣做過,請轉至這篇文章,了解如何設置Firebase配置和初始化Firebase本身。

At the very minimum, you need to have these packages installed:

至少,您需要安裝以下軟件包:

npm install angularfire2 firebase

使用插件 (Using the plugin)

The first step will be to setup a listener which will react to events, such as when a user logs in or out. If a sign in event occurs, the user object will contain credentials for that user such as their name and avatar picture.

第一步將是設置一個偵聽器,該偵聽器將對事件做出React,例如用戶登錄或注銷時。 如果發生登錄事件,則用戶對象將包含該用戶的憑據,例如其名稱和頭像圖片。

This piece of code should be in the constructor of either the home.ts in this project or any page where you want to track the status of AuthState.

這段代碼應位于該項目中的home.ts或您要跟蹤AuthState狀態的任何頁面的構造函數中。

Google登錄代碼 (Google Login Code)

You’ve made it this far. Doot doot! ?.

到目前為止,您已經做到了。 ot! ?

The final part of getting users signed in is the actual Google auth flow itself.

使用戶登錄的最后一部分是實際的Google身份驗證流程本身。

You need to test this part on a device, as the Ionic Native plugin uses Cordova which requires building onto a device.

您需要在設備上測試此部分,因為Ionic Native插件使用Cordova,這需要在設備上構建。

The pattern we are aiming for is setting up the functions to return promises. Then, if we need to, we can perform some action with the result.

我們旨在的模式是設置函數以返回承諾。 然后,如果需要,我們可以對結果執行一些操作。

If the sign-in is successful, then we will get a credential from the result and sign in the user to our Firebase.

如果登錄成功,則我們將從結果中獲取證書并將用戶登錄到我們的Firebase。

Lastly, depending on how it goes, we will either resolve or reject the promise. This will trigger either the .then or .catch clause anywhere this function is called.

最后,根據進展情況,我們將解決或拒絕承諾。 這將在調用此函數的任何地方觸發.then或.catch子句。

In the home.ts, this googleLogin function can be called with a .then and .catch clause, and the result will be passed accordingly in case we need to do anything with it.

在home.ts中,可以使用.then和.catch子句調用此googleLogin函數,并在需要進行任何處理時相應地傳遞結果。

When the sign-in is completed, the onAuthStateChanged listener will be triggered and the user information will be updated on the page.

登錄完成后,將觸發onAuthStateChanged偵聽器,并在頁面上更新用戶信息。

Google Plus sign-in is now wired up to Firebase and working on the device. If you plan to take this app into production, as noted you’ll need to redo a few things and use a different keystore for release.

Google Plus登錄現在已連接到Firebase并可以在設備上使用。 如前所述,如果您打算將此應用程序投入生產,則需要重做一些事情,并使用其他密鑰庫進行發布。

結論 (Conclusion)

In this article, we have set up the Google Sign-in API and worked through a cross platform solution for signing users into our Firebase with Google Plus.

在本文中,我們設置了Google Sign-in API,并通過跨平臺解決方案來使用Google Plus將用戶登錄到我們的Firebase。

Although there is a lot of configuration required between Google Developers Console and Firebase, the benefit is that our users can now sign into any web app we build with their existing Google accounts.

盡管Google Developers Console和Firebase之間需要進行大量配置,但好處是我們的用戶現在可以登錄到使用現有Google帳戶構建的任何Web應用程序。

This post is the first of a number I plan to write for Firebase / Ionic sign-ins.

這篇文章是我計劃為Firebase / Ionic登錄撰寫的第一篇文章。

If you want access to the code, here again is a link to the repo :

如果您想訪問代碼,這里再次是指向倉庫的鏈接:

Ryan-Gordon/Ionic-FirestarterIonic-Firestarter - Ionic Firestarter is a open source project showcasing different Firebase features implemented in…github.com

Ryan-Gordon / Ionic-Firestarter Ionic-Firestarter-Ionic Firestarter是一個開源項目,展示了在 github.com中 實現的不同Firebase功能。

Want some similar posts on Ionic ? Here is a couple other posts I’v done:

想要有關Ionic的類似帖子? 這是我完成的其他幾篇文章:

How to dynamically theme your Ionic application and make your users happyDesigning a sleek color scheme for your mobile application can be time consuming. Why not let the user choose their own…

如何為Ionic應用程序動態設置主題并使用戶感到滿意 為移動應用程序設計時尚的配色方案可能會很耗時。 為什么不讓用戶選擇自己的…

Alternative Sign in Methods for Firebase with IonicIn my other posts on Firebase sign ins, a focus has been put on Social providers. The main point of this emphasis is to…medium.com

使用Ionic進行Firebase的替代登錄方法 在我關于Firebase登錄的其他文章中,重點關注了社交提供程序。 這種強調的重點是... medium.com

翻譯自: https://www.freecodecamp.org/news/how-to-integrate-google-login-into-an-ionic-app-with-firebase-41cb69234919/

ios集成firebase

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/394096.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/394096.shtml
英文地址,請注明出處:http://en.pswp.cn/news/394096.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

面向對象三大核心特點,封裝、繼承和多態

封裝 封裝其實是一種思想&#xff0c;將事物狀態和功能裝進一個容器&#xff0c;那么這個容器在python中就是類&#xff0c;由這個類產生的對象都擁有類的屬性和功能 在面向對象的思想中&#xff0c;推崇將具有某些共同特征的事物歸為一類&#xff0c;那么這些事物就可以看做是…

java編寫某計算器控制臺程序_用java程序編寫一個計算器

點擊查看用java程序編寫一個計算器具體信息答&#xff1a;給你一個參考&#xff0c;希望不要被百度吞了當晚餐 import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.text.Decimal…

物聯網商機迸發 LPWAN芯片現身 本文轉自d1net(轉載)

聯發科技發表首款NB-IoT系統單芯片MT2625。來源&#xff1a;MediaTeK 物聯網(IoT)帶動的龐大商機吸引各方業者積極投入&#xff0c;尤其是各種聯網技術不斷現身&#xff0c;爭奪各式各樣極富發展潛力的應用領域。 根據IDC的調查報告&#xff0c;物聯網市場在2017年聲勢看漲&…

jquery之stop()的用法

工作中遇到過的實際案例&#xff1a; 1、我在項目里做的一個下拉菜單&#xff0c;當鼠標移上去的時候就菜單顯示&#xff0c;當鼠標離開的時候菜單隱藏 如果我快速不斷地將鼠標移入移出菜單&#xff08;即&#xff0c;當菜單下拉動畫未完成時&#xff0c;鼠標又移出了菜單&…

leetcode1123. 最深葉節點的最近公共祖先(dfs)

給你一個有根節點的二叉樹&#xff0c;找到它最深的葉節點的最近公共祖先。 回想一下&#xff1a; 葉節點 是二叉樹中沒有子節點的節點 樹的根節點的 深度 為 0&#xff0c;如果某一節點的深度為 d&#xff0c;那它的子節點的深度就是 d1 如果我們假定 A 是一組節點 S 的 最近…

sed空格替換成回車_【一題試水平】 利用sed命令將test.txt中所有的回車替換成空格?...

題目背景&#xff0c;這個題也很有年頭了&#xff0c;看似簡單&#xff0c;實則坑很大&#xff0c;good luck! 先不要看答案 看看自己能寫出多少方法.方法1 把每一行內容追加到Hold Space中&#xff0c;最后1行弄回到Pattern space中.然后進行替換基礎版[rootoldboyedu-show01 …

github 和git_學習編碼時如何學習Git和GitHub

github 和gitby Iago Rodrigues通過Iago Rodrigues 學習編碼時如何學習Git和GitHub (How you can learn Git and GitHub while you’re learning to code) In this article, I’ll give you some hints about how to become a Git/GitHub ninja. Also, as a bonus, I’ll show…

015_ICMP專項研究監控

一、數據demo cat /proc/net/snmp Ip: Forwarding DefaultTTL InReceives InHdrErrors InAddrErrors ForwDatagrams InUnknownProtos InDiscards InDelivers OutRequests OutDiscards OutNoRoutes ReasmTimeout ReasmReqds ReasmOKs ReasmFails FragOKs FragFails FragCreates …

leetcode129. 求根到葉子節點數字之和(dfs)

給定一個二叉樹&#xff0c;它的每個結點都存放一個 0-9 的數字&#xff0c;每條從根到葉子節點的路徑都代表一個數字。例如&#xff0c;從根到葉子節點路徑 1->2->3 代表數字 123。計算從根到葉子節點生成的所有數字之和。說明: 葉子節點是指沒有子節點的節點。示例 1:輸…

java for i i 區別,i ++amp;和i ++之間的區別是什么? ++我在for循環(Java)?

hello, Ive just started learning Java and now Im into for loop statement. I dont understand how i i works in a for loop statement.I mean how they work in mathematics operations like addition and subtraction. I hope some one will explain this to me.解決方案…

php 設置中文 cookie, js獲取

參考鏈接:http://www.nowamagic.net/librarys/veda/detail/1271 http://www.ruanyifeng.com/blog/2008/06/base64.html cookie.js 文件 var Cookies {}; /** * 設置Cookies */ Cookies.set function(name, value){ var argv arguments; var argc arguments.length; var exp…

學會這二十個正則表達式,能讓你少些1000行代碼!

正則表達式&#xff0c;是一個強大且高效的文本處理工具。通常情況下&#xff0c;通過一段表達準確的表達式&#xff0c;能夠非常簡短、快速的實現復雜業務邏輯。因此&#xff0c;正則表達式通常是一個成熟開發人員的標配&#xff0c;可以輔助實現開發效率的極強提升。在需要實…

mergesort_Mergesort算法的功能方法

mergesortby Joe Chasinga通過喬查辛加(Joe Chasinga) Mergesort算法的功能方法 (A functional approach to mergesort algorithm) Algorithms are often difficult for people to understand. I believe that this is because they are most often programmed or explained i…

循環內部異步函數處理相關問題解析

需求分析&#xff1a;根據一級標題ID篩選出所有對應的二級標題&#xff0c;返回一級標題ID&#xff0c;標題名和二級標題ID&#xff0c;標題名組成的數組 問題&#xff1a;通過forEach遍歷所有一級標題取對應的ID&#xff0c;根據ID條件查找所有的二級標題&#xff0c;遍歷符合…

nacos怎么修改服務分組_Nacos(六):多環境下如何“管理”及“隔離”配置和服務...

前言前景回顧&#xff1a;現如今&#xff0c;在微服務體系中&#xff0c;一個系統往往被拆分為多個服務&#xff0c;每個服務都有自己的配置文件&#xff0c;然后每個系統往往還會準備開發環境、測試環境、正式環境我們來說算一算&#xff0c;假設某系統有10個微服務&#xff0…

Hive_Hive的數據模型_內部表

Hive的數據模型_內部表 - 與數據庫中的Table在概念上是類似。- 每一個Table在Hive中都有一個相應的目錄存儲數據。- 所有的Table數據(不包括External Table)都保存在這個目錄中。 create table t1 (tid int, tname string, age int);create table t2 (tid int, tname string, a…

為啥JAVA虛擬機不開發系統_理解Java虛擬機體系結構

1 概述眾所周知&#xff0c;Java支持平臺無關性、安全性和網絡移動性。而Java平臺由Java虛擬機和Java核心類所構成&#xff0c;它為純Java程序提供了統一的編程接口&#xff0c;而不管下層操作系統是什么。正是得益于Java虛擬機&#xff0c;它號稱的“一次編譯&#xff0c;到處…

Android WindowManager和WindowManager.LayoutParams的使用以及實現懸浮窗口的方法

1.理清概念 我們使用過Dialog和PopupWindow,還有Toast,它們都顯示在Activity之上。那么我們首先需要理解的是android中是如何去繪制這些UI的呢&#xff1f;這里我只講我所理解的&#xff0c;首先看一層次圖&#xff08;盜用網絡&#xff09;首先我們看到左邊的Activity層&#…

leetcode面試題 04.03. 特定深度節點鏈表(bfs)

給定一棵二叉樹&#xff0c;設計一個算法&#xff0c;創建含有某一深度上所有節點的鏈表&#xff08;比如&#xff0c;若一棵樹的深度為 D&#xff0c;則會創建出 D 個鏈表&#xff09;。返回一個包含所有深度的鏈表的數組。示例&#xff1a;輸入&#xff1a;[1,2,3,4,5,null,7…

Java集合中的細節

integer數據對比 對于Integer var ? 在-128至127范圍內的賦值&#xff0c;Integer對象是在IntegerCache.cache產生&#xff0c;會復用已有對象&#xff0c;這個區間內的Integer值可以直接使用進行判斷&#xff0c;但是這個區間之外的所有數據&#xff0c;都會在堆上產生&…