|
1 | 1 | Authentication |
2 | 2 | ============== |
3 | 3 |
|
4 | | -The ``sign_in_with_email_and_password()`` method returns user |
5 | | -data, including a token you can use to adhere to security rules. |
6 | | - |
| 4 | +The authentication service allows you to signup, login, |
| 5 | +edit profile, apply security to the data you might store |
| 6 | +in either :ref:`Database<guide/database:Database>` or |
| 7 | +:ref:`Storage<guide/storage:Storage>`, and of course delete |
| 8 | +your account. |
7 | 9 |
|
8 | 10 | .. code-block:: python |
9 | 11 |
|
10 | 12 | # Get a reference to the auth service |
11 | 13 | auth = firebaseApp.auth() |
| 14 | +.. |
12 | 15 |
|
13 | | - # Log the user in |
14 | | - user = auth.sign_in_with_email_and_password(email, password) |
15 | | -
|
16 | | - # Log the user in anonymously |
17 | | - user = auth.sign_in_anonymous() |
| 16 | + .. note:: |
| 17 | + All sign in methods return user data, including a token |
| 18 | + you can use to adhere the security rules. |
18 | 19 |
|
19 | | - # Add user info |
20 | | - user = auth.update_profile(display_name, photo_url, delete_attribute) |
21 | 20 |
|
22 | | - # Get user info |
23 | | - user = auth.get_account_info() |
| 21 | +create_user_with_email_and_password |
| 22 | +----------------------------------- |
24 | 23 |
|
25 | | - # Get a reference to the database service |
26 | | - db = firebaseApp.database() |
| 24 | +Users can create an account using their |
| 25 | +email address and choice of password. |
27 | 26 |
|
28 | | - # data to save |
29 | | - data = { |
30 | | - "name": "Mortimer 'Morty' Smith" |
31 | | - } |
| 27 | +.. code-block:: python |
32 | 28 |
|
33 | | - # Pass the user's idToken to the push method |
34 | | - results = db.child("users").push(data, user['idToken']) |
| 29 | + # Creating an account |
| 30 | + auth.create_user_with_email_and_password(email, password) |
35 | 31 | .. |
36 | 32 |
|
| 33 | + .. note:: |
| 34 | + Make sure you have the Email/Password provider enabled in your |
| 35 | + Firebase dashboard under Authentication -> Sign In Method. |
37 | 36 |
|
38 | 37 |
|
39 | | -Token expiry |
40 | | ------------- |
| 38 | +sign_in_with_email_and_password |
| 39 | +------------------------------- |
41 | 40 |
|
| 41 | +User can login using their email and password, provided they |
| 42 | +:ref:`created an account<guide/authentication:create_user_with_email_and_password>` |
| 43 | +first. |
42 | 44 |
|
43 | 45 | .. code-block:: python |
44 | 46 |
|
| 47 | + # Log the user in |
45 | 48 | user = auth.sign_in_with_email_and_password(email, password) |
46 | | - # before the 1 hour expiry: |
47 | | - user = auth.refresh(user['refreshToken']) |
48 | | - # now we have a fresh token |
49 | | - user['idToken'] |
50 | 49 | .. |
51 | 50 |
|
52 | 51 |
|
53 | | -Custom tokens |
54 | | -------------- |
| 52 | +create_custom_token |
| 53 | +------------------- |
55 | 54 |
|
56 | | -You can also create users using `custom |
57 | | -tokens <https://firebase.google.com/docs/auth/server/create-custom-tokens>`__, |
58 | | -for example: |
| 55 | +| You can also create users using `custom tokens`_, |
| 56 | +| For example: |
59 | 57 |
|
60 | 58 | .. code-block:: python |
61 | 59 |
|
| 60 | + # Create custom token |
62 | 61 | token = auth.create_custom_token("your_custom_id") |
63 | 62 | .. |
64 | 63 |
|
65 | 64 | You can also pass in additional claims. |
66 | 65 |
|
67 | 66 | .. code-block:: python |
68 | 67 |
|
| 68 | + # Create custom token with claims |
69 | 69 | token_with_additional_claims = auth.create_custom_token("your_custom_id", {"premium_account": True}) |
70 | 70 | .. |
71 | 71 |
|
72 | | -You can then send these tokens to the client to sign in, or sign in as |
73 | | -the user on the server. |
| 72 | + .. note:: |
| 73 | + You need admin credentials (Service Account Key) to create |
| 74 | + custom tokens. |
| 75 | + |
| 76 | +.. _custom tokens: |
| 77 | + https://firebase.google.com/docs/auth/server/create-custom-tokens |
| 78 | + |
| 79 | + |
| 80 | +sign_in_with_custom_token |
| 81 | +------------------------- |
| 82 | + |
| 83 | +You can send these custom tokens to the client to |
| 84 | +sign in, or sign in as the user on the server. |
74 | 85 |
|
75 | 86 | .. code-block:: python |
76 | 87 |
|
| 88 | + # log in user using custom token |
77 | 89 | user = auth.sign_in_with_custom_token(token) |
78 | 90 | .. |
79 | 91 |
|
| 92 | +sign_in_anonymous |
| 93 | +----------------- |
| 94 | + |
| 95 | +Allows users (who haven't signed up yet) to |
| 96 | +use your app without creating an account. |
| 97 | + |
| 98 | + |
| 99 | +.. code-block:: python |
| 100 | +
|
| 101 | + # Log the user in anonymously |
| 102 | + user = auth.sign_in_anonymous() |
| 103 | +.. |
| 104 | +
|
| 105 | + .. note:: |
| 106 | + Make sure you have the **Anonymous** provider enabled in your |
| 107 | + Firebase dashboard under Authentication -> Sign In Method. |
80 | 108 |
|
81 | 109 |
|
82 | | -Manage Users |
83 | | ------------- |
| 110 | +create_authentication_uri |
| 111 | +------------------------- |
84 | 112 |
|
| 113 | +Signing in with social providers is done through two steps. First step |
| 114 | +one is done via redirecting user to the providers' login page using |
| 115 | +:ref:`create_authentication_uri<guide/authentication:create_authentication_uri>` |
| 116 | +which is can be used dynamically for all providers. |
85 | 117 |
|
86 | | -Creating users |
87 | | -^^^^^^^^^^^^^^ |
| 118 | + |
| 119 | + .. warning:: |
| 120 | + At the moment only sign is via **Google** is supported, other |
| 121 | + ones might break or work. |
| 122 | + |
| 123 | +The method returns an link to redirect user to providers' sign in page. |
| 124 | +Once the user signs into their account, user is asked for permissions |
| 125 | +and when granted, are redirect to the uri set while creating |
| 126 | +**OAuth Client IDs**, with authorization code to which can be further |
| 127 | +used to generate tokens to sign in with social providers in |
| 128 | +:ref:`second step<guide/authentication:sign_in_with_oauth_credential>`. |
88 | 129 |
|
89 | 130 | .. code-block:: python |
90 | 131 |
|
91 | | - auth.create_user_with_email_and_password(email, password) |
| 132 | + # Get a reference to the auth service with provider secret set |
| 133 | + auth = firebaseApp.auth(client_secret='client-secret-file.json') |
92 | 134 | .. |
93 | 135 |
|
94 | | - .. note:: |
95 | | - Make sure you have the Email/password provider enabled in your |
| 136 | +.. code-block:: python |
| 137 | +
|
| 138 | + # Example usage with Flask |
| 139 | + @auth.route('/login/google') |
| 140 | + def login_google(): |
| 141 | + return redirect(auth.create_authentication_uri('google.com')) |
| 142 | +
|
| 143 | +.. |
| 144 | +
|
| 145 | + .. note:: |
| 146 | + Make sure you have the **social** provider enabled in your |
96 | 147 | Firebase dashboard under Authentication -> Sign In Method. |
97 | 148 |
|
98 | | -Verifying emails |
99 | | -^^^^^^^^^^^^^^^^ |
| 149 | +authenticate_login_with_google |
| 150 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 151 | + |
| 152 | +This method is actually an reference to |
| 153 | +:ref:`create_authentication_uri<guide/authentication:create_authentication_uri>` |
| 154 | +with **Google** preset as the provider to use. |
| 155 | + |
100 | 156 |
|
101 | 157 | .. code-block:: python |
102 | 158 |
|
103 | | - auth.send_email_verification(user['idToken']) |
| 159 | + # Example usage with Flask |
| 160 | + @auth.route('/login/google') |
| 161 | + def login_google(): |
| 162 | + return redirect(auth.authenticate_login_with_google()) |
104 | 163 | .. |
105 | 164 |
|
106 | | -Sending password reset emails |
107 | | -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 165 | + .. note:: |
| 166 | + Make sure you have the **Google Sign In** provider enabled in |
| 167 | + your Firebase dashboard under Authentication -> Sign In Method. |
| 168 | + |
| 169 | + |
| 170 | +sign_in_with_oauth_credential |
| 171 | +----------------------------- |
| 172 | + |
| 173 | +Second step to sign in using social provider is to pass the URL |
| 174 | +(containing multiple params) that the user is redirected to, into this |
| 175 | +method. This method auto generates the tokens using params from that |
| 176 | +URL, then signs the user in using those tokens to Firebase linking the |
| 177 | +specific provider. |
| 178 | + |
108 | 179 |
|
109 | 180 | .. code-block:: python |
110 | 181 |
|
111 | | - auth.send_password_reset_email("email") |
| 182 | + # Here https://example.com/oauth2callback/ is the redirect URI |
| 183 | + # that was set while creating OAuth Client ID |
| 184 | +
|
| 185 | + # Example usage with Flask |
| 186 | + @auth.route('/oauth2callback/') |
| 187 | + def oauth2callback(): |
| 188 | +
|
| 189 | + user = auth.sign_in_with_oauth_credential(request.url) |
| 190 | +
|
| 191 | + return jsonify(**user) |
| 192 | +
|
| 193 | +
|
| 194 | +get_account_info |
| 195 | +---------------- |
| 196 | + |
| 197 | +This method returns an detailed version of the user's data associated |
| 198 | +with Authentication service. |
| 199 | + |
| 200 | +.. code-block:: python |
| 201 | +
|
| 202 | + # User account info |
| 203 | + user_info = auth.get_account_info(user['idToken']) |
112 | 204 | .. |
113 | 205 |
|
114 | | -Get account information |
115 | | -^^^^^^^^^^^^^^^^^^^^^^^ |
| 206 | + |
| 207 | +update_profile |
| 208 | +-------------- |
| 209 | + |
| 210 | +Update stored information or add information into the user's account. |
116 | 211 |
|
117 | 212 | .. code-block:: python |
118 | 213 |
|
119 | | - auth.get_account_info(user['idToken']) |
| 214 | + # Update user's name |
| 215 | + auth.update_profile(user['idToken'], display_name='Iron Man') |
| 216 | +
|
| 217 | + # update user's profile picture |
| 218 | + auth.update_profile(user['idToken'], photo_url='https://i.pinimg.com/originals/c0/37/2f/c0372feb0069e6289eb938b219e0b0a1.jpg') |
120 | 219 | .. |
121 | 220 |
|
122 | | -Refreshing tokens |
123 | | -^^^^^^^^^^^^^^^^^ |
| 221 | + |
| 222 | +refresh |
| 223 | +------- |
| 224 | + |
| 225 | +Firebase Auth Tokens are granted when an user logs in, and are |
| 226 | +associated with an expiration time of an hour generally, after |
| 227 | +that they lose validation and a new set of Tokens are needed, |
| 228 | +and they can be obtained by passing the ``refreshToken`` key |
| 229 | +from the users' tokens, received when logged in. |
124 | 230 |
|
125 | 231 | .. code-block:: python |
126 | 232 |
|
| 233 | + # before the 1 hour expiry: |
127 | 234 | user = auth.refresh(user['refreshToken']) |
| 235 | +
|
| 236 | + # now we have a fresh token |
| 237 | + user['idToken'] |
128 | 238 | .. |
129 | 239 |
|
130 | | -Delete account |
131 | | -^^^^^^^^^^^^^^ |
| 240 | + |
| 241 | +delete_user_account |
| 242 | +------------------- |
| 243 | + |
| 244 | +In case any user want to delete their account, it can be done by |
| 245 | +passing ``idToken`` key from the users' tokens, received when logged |
| 246 | +in. |
132 | 247 |
|
133 | 248 | .. code-block:: python |
134 | 249 |
|
135 | 250 | auth.delete_user_account(user['idToken']) |
136 | 251 | .. |
| 252 | +
|
| 253 | + |
| 254 | +send_password_reset_email |
| 255 | +------------------------- |
| 256 | + |
| 257 | +In case any user forgot his password, it is possible to send |
| 258 | +them email containing an code or link to reset their password. |
| 259 | + |
| 260 | +.. code-block:: python |
| 261 | +
|
| 262 | + auth.send_password_reset_email(email) |
| 263 | +.. |
| 264 | +
|
| 265 | + |
| 266 | +send_email_verification |
| 267 | +----------------------- |
| 268 | + |
| 269 | +To ensure the email address belongs to the user who created the |
| 270 | +account, it is recommended to request verification of the email. |
| 271 | +Verification code/link can be sent to the user by passing ``idToken`` |
| 272 | +key from the users' tokens, to this method. |
| 273 | + |
| 274 | +.. code-block:: python |
| 275 | +
|
| 276 | + auth.send_email_verification(user['idToken']) |
| 277 | +.. |
0 commit comments