|
| 1 | +<table> |
| 2 | +<tr style="border:none"> |
| 3 | +<td style="border:none; padding:0"><img src="images/firebase.png" width="154px" height="43px" alt="Firebase"/></td> |
| 4 | +<td style="border:none; padding:4">Authentication</td> |
| 5 | +</tr> |
| 6 | +</table> |
| 7 | + |
| 8 | +## Enabling Authentication |
| 9 | +Version 1.1.0 of this plugin added the capability to log your users in, either |
| 10 | + |
| 11 | +* anonymously, |
| 12 | +* by email and password, |
| 13 | +* using a custom token, |
| 14 | +* using Facebook (iOS: solid, Android: experimental). |
| 15 | + |
| 16 | +Each of these login mechanisms need to be enabled in your Firebase console at the 'Login & Auth' tab. |
| 17 | + |
| 18 | +## Functions |
| 19 | + |
| 20 | +### Listening to auth state changes |
| 21 | +As stated [here](https://firebase.google.com/docs/auth/ios/manage-users#get_the_currently_signed-in_user): |
| 22 | + |
| 23 | +> The recommended way to get the current user is by setting a listener on the Auth object |
| 24 | +
|
| 25 | +To listen to auth state changes you can register a listener during `init`: |
| 26 | + |
| 27 | +```js |
| 28 | + firebase.init({ |
| 29 | + onAuthStateChanged: function(data) { // optional but useful to immediately re-logon the user when he re-visits your app |
| 30 | + console.log(data.loggedIn ? "Logged in to firebase" : "Logged out from firebase"); |
| 31 | + if (data.loggedIn) { |
| 32 | + console.log("user's email address: " + (data.user.email ? data.user.email : "N/A")); |
| 33 | + } |
| 34 | + } |
| 35 | + }); |
| 36 | +``` |
| 37 | + |
| 38 | +If - for some reason - you want more control over the listener you can use these methods after you ran `init`: |
| 39 | + |
| 40 | +```js |
| 41 | + // configure a listener: |
| 42 | + var listener = { |
| 43 | + onAuthStateChanged: function(data) { |
| 44 | + console.log(data.loggedIn ? "Logged in to firebase" : "Logged out from firebase"); |
| 45 | + if (data.loggedIn) { |
| 46 | + console.log("User info", data.user); |
| 47 | + } |
| 48 | + }, |
| 49 | + thisArg: this |
| 50 | + }; |
| 51 | + |
| 52 | + // add the listener: |
| 53 | + firebase.addAuthStateListener(listener); |
| 54 | + |
| 55 | + // stop listening to auth state changes: |
| 56 | + firebase.removeAuthStateListener(listener); |
| 57 | + |
| 58 | + // check if already listening to auth state changes |
| 59 | + firebase.hasAuthStateListener(listener); |
| 60 | +``` |
| 61 | + |
| 62 | + |
| 63 | +### Anonymous login |
| 64 | +Don't forget to enable anonymous login in your firebase instance. |
| 65 | + |
| 66 | +```js |
| 67 | + firebase.login({ |
| 68 | + type: firebase.LoginType.ANONYMOUS |
| 69 | + }).then( |
| 70 | + function (result) { |
| 71 | + // the result object has these properties: uid, provider, expiresAtUnixEpochSeconds, profileImageURL, token |
| 72 | + JSON.stringify(result); |
| 73 | + }, |
| 74 | + function (errorMessage) { |
| 75 | + console.log(errorMessage); |
| 76 | + } |
| 77 | + ) |
| 78 | +``` |
| 79 | + |
| 80 | +### Email-Password login |
| 81 | +Don't forget to enable email-password login in your firebase instance. |
| 82 | + |
| 83 | +```js |
| 84 | + firebase.login({ |
| 85 | + type: firebase.LoginType.PASSWORD, |
| 86 | + |
| 87 | + password: 'theirpassword' |
| 88 | + }).then( |
| 89 | + function (result) { |
| 90 | + // the result object has these properties: uid, provider, expiresAtUnixEpochSeconds, profileImageURL, token |
| 91 | + JSON.stringify(result); |
| 92 | + }, |
| 93 | + function (errorMessage) { |
| 94 | + console.log(errorMessage); |
| 95 | + } |
| 96 | + ) |
| 97 | +``` |
| 98 | + |
| 99 | +#### Managing email-password accounts |
| 100 | +##### Creating a Password account |
| 101 | +```js |
| 102 | + firebase.createUser({ |
| 103 | + |
| 104 | + password: 'firebase' |
| 105 | + }).then( |
| 106 | + function (result) { |
| 107 | + dialogs.alert({ |
| 108 | + title: "User created", |
| 109 | + message: "userid: " + result.key, |
| 110 | + okButtonText: "Nice!" |
| 111 | + }) |
| 112 | + }, |
| 113 | + function (errorMessage) { |
| 114 | + dialogs.alert({ |
| 115 | + title: "No user created", |
| 116 | + message: errorMessage, |
| 117 | + okButtonText: "OK, got it" |
| 118 | + }) |
| 119 | + } |
| 120 | + ) |
| 121 | +``` |
| 122 | + |
| 123 | +#### Resetting a password |
| 124 | +```js |
| 125 | + firebase.resetPassword({ |
| 126 | + |
| 127 | + }).then( |
| 128 | + function () { |
| 129 | + // called when password reset was successful, |
| 130 | + // you could now prompt the user to check his email |
| 131 | + }, |
| 132 | + function (errorMessage) { |
| 133 | + console.log(errorMessage); |
| 134 | + } |
| 135 | + ) |
| 136 | +``` |
| 137 | + |
| 138 | +#### Changing a password |
| 139 | +```js |
| 140 | + firebase.changePassword({ |
| 141 | + |
| 142 | + oldPassword: 'myOldPassword', |
| 143 | + newPassword: 'myNewPassword' |
| 144 | + }).then( |
| 145 | + function () { |
| 146 | + // called when password change was successful |
| 147 | + }, |
| 148 | + function (errorMessage) { |
| 149 | + console.log(errorMessage); |
| 150 | + } |
| 151 | + ) |
| 152 | +``` |
| 153 | + |
| 154 | +### Custom login |
| 155 | +Use this login type to authenticate against firebase using a token generated by your own backend server. See these [instructions on how to generate the authentication token](https://firebase.google.com/docs/auth/server). |
| 156 | + |
| 157 | +```js |
| 158 | + var token = "myBackendToken"; |
| 159 | + |
| 160 | + firebase.login({ |
| 161 | + type: firebase.LoginType.CUSTOM, |
| 162 | + token: token |
| 163 | + }).then( |
| 164 | + function (result) { |
| 165 | + // the result object has these properties: uid, provider, expiresAtUnixEpochSeconds, profileImageURL, token |
| 166 | + JSON.stringify(result); |
| 167 | + }, |
| 168 | + function (errorMessage) { |
| 169 | + console.log(errorMessage); |
| 170 | + } |
| 171 | + ) |
| 172 | +``` |
| 173 | + |
| 174 | +### Facebook login |
| 175 | +On iOS this is rock solid, but on Android it's work in progress. If you want to use it for iOS open the `Podfile` in the plugin's `platforms/ios` folder and uncomment the Facebook line (you can't miss it). |
| 176 | + |
| 177 | +Don't forget to enable Facebook login in your firebase instance. |
| 178 | + |
| 179 | +```js |
| 180 | + firebase.login({ |
| 181 | + type: firebase.LoginType.FACEBOOK |
| 182 | + }).then( |
| 183 | + function (result) { |
| 184 | + JSON.stringify(result); |
| 185 | + }, |
| 186 | + function (errorMessage) { |
| 187 | + console.log(errorMessage); |
| 188 | + } |
| 189 | + ) |
| 190 | +``` |
| 191 | + |
| 192 | +And also, add [this bit to `app.js`](https://github.com/EddyVerbruggen/nativescript-plugin-firebase-demo/blob/master/Firebase/app/app.js#L5-L34) to give control back to your app after Facebook's auth UI finished. |
| 193 | + |
| 194 | + |
| 195 | +### logout |
| 196 | +Shouldn't be more complicated than: |
| 197 | + |
| 198 | +```js |
| 199 | + firebase.logout(); |
| 200 | +``` |
| 201 | + |
| 202 | + |
0 commit comments