|
| 1 | +Authentication |
| 2 | +============== |
| 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 | + |
| 7 | + |
| 8 | +.. code-block:: python |
| 9 | +
|
| 10 | + # Get a reference to the auth service |
| 11 | + auth = firebaseApp.auth() |
| 12 | +
|
| 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() |
| 18 | +
|
| 19 | + # Add user info |
| 20 | + user = auth.update_profile(display_name, photo_url, delete_attribute) |
| 21 | +
|
| 22 | + # Get user info |
| 23 | + user = auth.get_account_info() |
| 24 | +
|
| 25 | + # Get a reference to the database service |
| 26 | + db = firebaseApp.database() |
| 27 | +
|
| 28 | + # data to save |
| 29 | + data = { |
| 30 | + "name": "Mortimer 'Morty' Smith" |
| 31 | + } |
| 32 | +
|
| 33 | + # Pass the user's idToken to the push method |
| 34 | + results = db.child("users").push(data, user['idToken']) |
| 35 | +.. |
| 36 | +
|
| 37 | + |
| 38 | + |
| 39 | +Token expiry |
| 40 | +------------ |
| 41 | + |
| 42 | + |
| 43 | +.. code-block:: python |
| 44 | +
|
| 45 | + 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 | +.. |
| 51 | +
|
| 52 | + |
| 53 | +Custom tokens |
| 54 | +------------- |
| 55 | + |
| 56 | +You can also create users using `custom |
| 57 | +tokens <https://firebase.google.com/docs/auth/server/create-custom-tokens>`__, |
| 58 | +for example: |
| 59 | + |
| 60 | +.. code-block:: python |
| 61 | +
|
| 62 | + token = auth.create_custom_token("your_custom_id") |
| 63 | +.. |
| 64 | +
|
| 65 | +You can also pass in additional claims. |
| 66 | + |
| 67 | +.. code-block:: python |
| 68 | +
|
| 69 | + token_with_additional_claims = auth.create_custom_token("your_custom_id", {"premium_account": True}) |
| 70 | +.. |
| 71 | +
|
| 72 | +You can then send these tokens to the client to sign in, or sign in as |
| 73 | +the user on the server. |
| 74 | + |
| 75 | +.. code-block:: python |
| 76 | +
|
| 77 | + user = auth.sign_in_with_custom_token(token) |
| 78 | +.. |
| 79 | +
|
| 80 | + |
| 81 | + |
| 82 | +Manage Users |
| 83 | +------------ |
| 84 | + |
| 85 | + |
| 86 | +Creating users |
| 87 | +^^^^^^^^^^^^^^ |
| 88 | + |
| 89 | +.. code-block:: python |
| 90 | +
|
| 91 | + auth.create_user_with_email_and_password(email, password) |
| 92 | +.. |
| 93 | +
|
| 94 | + .. note:: |
| 95 | + Make sure you have the Email/password provider enabled in your |
| 96 | + Firebase dashboard under Authentication -> Sign In Method. |
| 97 | + |
| 98 | +Verifying emails |
| 99 | +^^^^^^^^^^^^^^^^ |
| 100 | + |
| 101 | +.. code-block:: python |
| 102 | +
|
| 103 | + auth.send_email_verification(user['idToken']) |
| 104 | +.. |
| 105 | +
|
| 106 | +Sending password reset emails |
| 107 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 108 | + |
| 109 | +.. code-block:: python |
| 110 | +
|
| 111 | + auth.send_password_reset_email("email") |
| 112 | +.. |
| 113 | +
|
| 114 | +Get account information |
| 115 | +^^^^^^^^^^^^^^^^^^^^^^^ |
| 116 | + |
| 117 | +.. code-block:: python |
| 118 | +
|
| 119 | + auth.get_account_info(user['idToken']) |
| 120 | +.. |
| 121 | +
|
| 122 | +Refreshing tokens |
| 123 | +^^^^^^^^^^^^^^^^^ |
| 124 | + |
| 125 | +.. code-block:: python |
| 126 | +
|
| 127 | + user = auth.refresh(user['refreshToken']) |
| 128 | +.. |
| 129 | +
|
| 130 | +Delete account |
| 131 | +^^^^^^^^^^^^^^ |
| 132 | + |
| 133 | +.. code-block:: python |
| 134 | +
|
| 135 | + auth.delete_user_account(user['idToken']) |
| 136 | +.. |
0 commit comments