|
| 1 | +# Authentication |
| 2 | + |
| 3 | +Firestack handles authentication for us out of the box, both with email/password-based authentication and through oauth providers (with a separate library to handle oauth providers). |
| 4 | + |
| 5 | +> Android requires the Google Play services to installed for authentication to function. |
| 6 | +
|
| 7 | +## Local Auth |
| 8 | + |
| 9 | +#### [onAuthStateChanged(event: Function)](https://firebase.google.com/docs/reference/js/firebase.auth.Auth#onAuthStateChanged) |
| 10 | + |
| 11 | +Listen for changes in the users auth state (logging in and out). |
| 12 | + |
| 13 | +```javascript |
| 14 | +firestack.auth().onAuthStateChanged((evt) => { |
| 15 | + // evt is the authentication event, it contains an `error` key for carrying the |
| 16 | + // error message in case of an error and a `user` key upon successful authentication |
| 17 | + if (!evt.authenticated) { |
| 18 | + // There was an error or there is no user |
| 19 | + console.error(evt.error) |
| 20 | + } else { |
| 21 | + // evt.user contains the user details |
| 22 | + console.log('User details', evt.user); |
| 23 | + } |
| 24 | +}) |
| 25 | +.then(() => console.log('Listening for authentication changes')) |
| 26 | +``` |
| 27 | + |
| 28 | +#### offAuthStateChanged() |
| 29 | + |
| 30 | +Remove the `onAuthStateChanged` listener. |
| 31 | +This is important to release resources from our app when we don't need to hold on to the listener any longer. |
| 32 | + |
| 33 | +```javascript |
| 34 | +firestack.auth().offAuthStateChanged() |
| 35 | +``` |
| 36 | + |
| 37 | +#### [createUserWithEmailAndPassword(email: string, password: string)](https://firebase.google.com/docs/reference/js/firebase.auth.Auth#createUserWithEmailAndPassword) |
| 38 | + |
| 39 | +We can create a user by calling the `createUserWithEmailAndPassword()` function. |
| 40 | +The method accepts two parameters, an email and a password. |
| 41 | + |
| 42 | +```javascript |
| 43 | +firestack. auth(). createUserWithEmailAndPassword( '[email protected]', '123456') |
| 44 | + .then((user) => { |
| 45 | + console.log('user created', user) |
| 46 | + }) |
| 47 | + .catch((err) => { |
| 48 | + console.error('An error occurred', err); |
| 49 | + }) |
| 50 | +``` |
| 51 | + |
| 52 | +#### [signInWithEmailAndPassword(email: string, password: string)](https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInWithEmailAndPassword) |
| 53 | + |
| 54 | +To sign a user in with their email and password, use the `signInWithEmailAndPassword()` function. |
| 55 | +It accepts two parameters, the user's email and password: |
| 56 | + |
| 57 | +```javascript |
| 58 | +firestack. auth(). signInWithEmailAndPassword( '[email protected]', '123456') |
| 59 | + .then((user) => { |
| 60 | + console.log('User successfully logged in', user) |
| 61 | + }) |
| 62 | + .catch((err) => { |
| 63 | + console.error('User signin error', err); |
| 64 | + }) |
| 65 | +``` |
| 66 | + |
| 67 | +#### [signInAnonymously()](https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInAnonymously) |
| 68 | + |
| 69 | +Sign an anonymous user. If the user has already signed in, that user will be returned. |
| 70 | + |
| 71 | +```javascript |
| 72 | +firestack.auth().signInAnonymously() |
| 73 | + .then((user) => { |
| 74 | + console.log('Anonymous user successfully logged in', user) |
| 75 | + }) |
| 76 | + .catch((err) => { |
| 77 | + console.error('Anonymous user signin error', err); |
| 78 | + }) |
| 79 | +``` |
| 80 | + |
| 81 | +#### signInWithProvider() |
| 82 | + |
| 83 | +We can use an external authentication provider, such as twitter/facebook for authentication. In order to use an external provider, we need to include another library to handle authentication. |
| 84 | + |
| 85 | +> By using a separate library, we can keep our dependencies a little lower and the size of the application down. |
| 86 | +
|
| 87 | +#### signInWithCustomToken() |
| 88 | + |
| 89 | +To sign a user using a self-signed custom token, use the `signInWithCustomToken()` function. It accepts one parameter, the custom token: |
| 90 | + |
| 91 | +```javascript |
| 92 | +firestack.auth().signInWithCustomToken(TOKEN) |
| 93 | + .then((user) => { |
| 94 | + console.log('User successfully logged in', user) |
| 95 | + }) |
| 96 | + .catch((err) => { |
| 97 | + console.error('User signin error', err); |
| 98 | + }) |
| 99 | +``` |
| 100 | + |
| 101 | +#### [updateUserEmail()](https://firebase.google.com/docs/reference/js/firebase.User#updateEmail) |
| 102 | + |
| 103 | +We can update the current user's email by using the command: `updateUserEmail()`. |
| 104 | +It accepts a single argument: the user's new email: |
| 105 | + |
| 106 | +```javascript |
| 107 | +firestack. auth(). updateUserEmail( '[email protected]') |
| 108 | + .then((res) => console.log('Updated user email')) |
| 109 | + .catch(err => console.error('There was an error updating user email')) |
| 110 | +``` |
| 111 | + |
| 112 | +#### [updateUserPassword()](https://firebase.google.com/docs/reference/js/firebase.User#updatePassword) |
| 113 | + |
| 114 | +We can update the current user's password using the `updateUserPassword()` method. |
| 115 | +It accepts a single parameter: the new password for the current user |
| 116 | + |
| 117 | +```javascript |
| 118 | +firestack.auth().updateUserPassword('somethingReallyS3cr3t733t') |
| 119 | + .then(res => console.log('Updated user password')) |
| 120 | + .catch(err => console.error('There was an error updating your password')) |
| 121 | +``` |
| 122 | + |
| 123 | +#### [updateUserProfile()](https://firebase.google.com/docs/auth/web/manage-users#update_a_users_profile) |
| 124 | + |
| 125 | +To update the current user's profile, we can call the `updateUserProfile()` method. |
| 126 | +It accepts a single parameter: |
| 127 | + |
| 128 | +* object which contains updated key/values for the user's profile. |
| 129 | +Possible keys are listed [here](https://firebase.google.com/docs/auth/ios/manage-users#update_a_users_profile). |
| 130 | + |
| 131 | +```javascript |
| 132 | +firestack.auth() |
| 133 | + .updateUserProfile({ |
| 134 | + displayName: 'Ari Lerner' |
| 135 | + }) |
| 136 | + .then(res => console.log('Your profile has been updated')) |
| 137 | + .catch(err => console.error('There was an error :(')) |
| 138 | +``` |
| 139 | + |
| 140 | +#### [sendPasswordResetWithEmail()](https://firebase.google.com/docs/auth/web/manage-users#send_a_password_reset_email) |
| 141 | + |
| 142 | +To send a password reset for a user based upon their email, we can call the `sendPasswordResetWithEmail()` method. |
| 143 | +It accepts a single parameter: the email of the user to send a reset email. |
| 144 | + |
| 145 | +```javascript |
| 146 | +firestack. auth(). sendPasswordResetWithEmail( '[email protected]') |
| 147 | + .then(res => console.log('Check your inbox for further instructions')) |
| 148 | + .catch(err => console.error('There was an error :(')) |
| 149 | +``` |
| 150 | +#### [deleteUser()](https://firebase.google.com/docs/auth/web/manage-users#delete_a_user) |
| 151 | + |
| 152 | +It's possible to delete a user completely from your account on Firebase. |
| 153 | +Calling the `deleteUser()` method will take care of this for you. |
| 154 | + |
| 155 | +```javascript |
| 156 | +firestack.auth() |
| 157 | + .deleteUser() |
| 158 | + .then(res => console.log('Sad to see you go')) |
| 159 | + .catch(err => console.error('There was an error - Now you are trapped!')) |
| 160 | +``` |
| 161 | + |
| 162 | +#### getToken() |
| 163 | + |
| 164 | +If you want user's token, use `getToken()` method. |
| 165 | + |
| 166 | +```javascript |
| 167 | +firestack.auth() |
| 168 | + .getToken() |
| 169 | + .then(res => console.log(res.token)) |
| 170 | + .catch(err => console.error('error')) |
| 171 | +``` |
| 172 | + |
| 173 | +#### [signOut()](https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signOut) |
| 174 | + |
| 175 | +To sign the current user out, use the `signOut()` method. |
| 176 | +It accepts no parameters |
| 177 | + |
| 178 | +```javascript |
| 179 | +firestack.auth() |
| 180 | + .signOut() |
| 181 | + .then(res => console.log('You have been signed out')) |
| 182 | + .catch(err => console.error('Uh oh... something weird happened')) |
| 183 | +``` |
| 184 | + |
| 185 | + |
| 186 | +#### getCurrentUser() |
| 187 | + |
| 188 | +Although you _can_ get the current user using the `getCurrentUser()` method, it's better to use this from within the callback function provided by `listenForAuth()`. |
| 189 | +However, if you need to get the current user, call the `getCurrentUser()` method: |
| 190 | + |
| 191 | +```javascript |
| 192 | +firestack.auth() |
| 193 | + .getCurrentUser() |
| 194 | + .then(user => console.log('The currently logged in user', user)) |
| 195 | + .catch(err => console.error('An error occurred')) |
| 196 | +``` |
| 197 | + |
| 198 | +## Social Auth |
| 199 | + |
| 200 | +TODO |
0 commit comments