Skip to content

Commit 234e0fc

Browse files
docs: update authentication guide
1 parent 75049e9 commit 234e0fc

File tree

1 file changed

+194
-53
lines changed

1 file changed

+194
-53
lines changed

docs/guide/authentication.rst

Lines changed: 194 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,277 @@
11
Authentication
22
==============
33

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.
79

810
.. code-block:: python
911
1012
# Get a reference to the auth service
1113
auth = firebaseApp.auth()
14+
..
1215
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.
1819

19-
# Add user info
20-
user = auth.update_profile(display_name, photo_url, delete_attribute)
2120

22-
# Get user info
23-
user = auth.get_account_info()
21+
create_user_with_email_and_password
22+
-----------------------------------
2423

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.
2726

28-
# data to save
29-
data = {
30-
"name": "Mortimer 'Morty' Smith"
31-
}
27+
.. code-block:: python
3228
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)
3531
..
3632
33+
.. note::
34+
Make sure you have the Email/Password provider enabled in your
35+
Firebase dashboard under Authentication -> Sign In Method.
3736

3837

39-
Token expiry
40-
------------
38+
sign_in_with_email_and_password
39+
-------------------------------
4140

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.
4244

4345
.. code-block:: python
4446
47+
# Log the user in
4548
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']
5049
..
5150
5251

53-
Custom tokens
54-
-------------
52+
create_custom_token
53+
-------------------
5554

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:
5957
6058
.. code-block:: python
6159
60+
# Create custom token
6261
token = auth.create_custom_token("your_custom_id")
6362
..
6463
6564
You can also pass in additional claims.
6665

6766
.. code-block:: python
6867
68+
# Create custom token with claims
6969
token_with_additional_claims = auth.create_custom_token("your_custom_id", {"premium_account": True})
7070
..
7171
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.
7485

7586
.. code-block:: python
7687
88+
# log in user using custom token
7789
user = auth.sign_in_with_custom_token(token)
7890
..
7991
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.
80108

81109

82-
Manage Users
83-
------------
110+
create_authentication_uri
111+
-------------------------
84112

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.
85117

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>`.
88129

89130
.. code-block:: python
90131
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')
92134
..
93135
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
96147
Firebase dashboard under Authentication -> Sign In Method.
97148

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+
100156

101157
.. code-block:: python
102158
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())
104163
..
105164
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+
108179

109180
.. code-block:: python
110181
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'])
112204
..
113205
114-
Get account information
115-
^^^^^^^^^^^^^^^^^^^^^^^
206+
207+
update_profile
208+
--------------
209+
210+
Update stored information or add information into the user's account.
116211

117212
.. code-block:: python
118213
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')
120219
..
121220
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.
124230

125231
.. code-block:: python
126232
233+
# before the 1 hour expiry:
127234
user = auth.refresh(user['refreshToken'])
235+
236+
# now we have a fresh token
237+
user['idToken']
128238
..
129239
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.
132247

133248
.. code-block:: python
134249
135250
auth.delete_user_account(user['idToken'])
136251
..
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

Comments
 (0)