Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2270 appwrite user authentication #2293

Merged
merged 2 commits into from
Oct 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
393 changes: 393 additions & 0 deletions Appwrite/Appwrite_User_Authentication.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,393 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "6c4c63ae-5034-4150-8f7a-1541968c6913",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"<img width=\"10%\" alt=\"Naas\" src=\"https://landen.imgix.net/jtci2pxwjczr/assets/5ice39g4.png?w=160\"/>"
]
},
{
"cell_type": "markdown",
"id": "8d64ced2-e7f3-442a-a3ec-8af66a99184e",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"# Appwrite - User Authentication"
]
},
{
"cell_type": "markdown",
"id": "bf188fc8-9ea3-4853-ae06-06c3fe162226",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Tags:** #appwrite #python #sdk #authentication #user #create"
]
},
{
"cell_type": "markdown",
"id": "f122d69a-ae50-4d95-90df-fcb0d483fac7",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Author:** [SaiKiran M](www.linkedin.com/in/msaikiran9)"
]
},
{
"cell_type": "markdown",
"id": "6f8a21c1-560a-405f-8599-fd906e95673d",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Last update:** 2023-10-08 (Created: 2023-10-08)"
]
},
{
"cell_type": "markdown",
"id": "96df94c1-842e-4588-a304-7b28a281138e",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Description:** This notebook will demonstrate how to use the Appwrite Python SDK to create a authentication and have options to `create_user`, `delete_user`, `list_all_users` as individual routines. It is useful for organizations that need to manage user authentication."
]
},
{
"cell_type": "markdown",
"id": "a14df074-778c-4f39-bc9e-c53d907dc830",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**References:**\n",
"- [Appwrite Python SDK](https://github.com/appwrite/sdk-for-python)\n",
"- [Appwrite Documentation](https://appwrite.io/docs)"
]
},
{
"cell_type": "markdown",
"id": "2bfe3fda-6f51-45b4-8dfa-06dda53622b2",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Input"
]
},
{
"cell_type": "markdown",
"id": "4f245a0a-f3a4-4750-be9c-752a07e7e4b5",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Import libraries"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "81730041-2523-462e-83c7-f3c08a3ca960",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"try:\n",
" import naas\n",
" from appwrite.client import Client\n",
" from appwrite.services.users import Users\n",
"except ImportError:\n",
" !pip install appwrite\n",
" from appwrite.client import Client\n",
" from appwrite.services.users import Users"
]
},
{
"cell_type": "markdown",
"id": "54602e57-1b2f-4acb-9f32-eb5ec0561582",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Setup variables\n",
"- `endpoint`: Appwrite endpoint URL\n",
"- `project`: Appwrite project ID\n",
"- `key`: Appwrite secret key"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "69019e17-53eb-434a-8837-ab33b6a5d0ce",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"endpoint = naas.secret.get(name=\"endpoint\")\n",
"project = naas.secret.get(name=\"project\")\n",
"key = naas.secret.get(name=\"key\")"
]
},
{
"cell_type": "markdown",
"id": "57e10eba-26ba-49f5-8376-02be391a8edd",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Model"
]
},
{
"cell_type": "markdown",
"id": "a93a0eb3-c7a1-467b-bb94-18e4c331d3ca",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Create User"
]
},
{
"cell_type": "markdown",
"id": "8b6f611f-d15f-4d32-af30-9193a401fdd5",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"This routine will create a new user in the Appwrite project."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b26699a4-fa18-4658-90bc-b3c3e7fd68f4",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"# Initialize Appwrite client\n",
"client = Client()\n",
"client.set_endpoint(endpoint)\n",
"client.set_project(project)\n",
"client.set_key(key)\n",
"# Create user\n",
"user_data = {\n",
" 'userId': 'john-_doe123',\n",
" 'email': '[email protected]',\n",
" 'password': 'password',\n",
" 'name': 'John Doe',\n",
"}\n",
"users=Users(client)\n",
"try: #users.create take (username, email, mobileno, password) as arguments.\n",
" user_details = users.create('msaik12', '[email protected]','+1234456666' ,'password')\n",
" print(\"User Created!\")\n",
" print(\"User Details:\")\n",
" print(f\"User ID: {user_details['$id']}\")\n",
" print(f\"Name: {user_details['name']}\")\n",
" print(f\"Email: {user_details['email']}\")\n",
" print(f\"Phone: {user_details['phone']}\")\n",
" print(f\"Registration Date: {user_details['registration']}\")\n",
" print(f\"Status: {'Active' if user_details['status'] else 'Inactive'}\")\n",
"except: #make sure to provide different username everytime creating the user!\n",
" print(\"User not created !\")"
]
},
{
"cell_type": "markdown",
"id": "cbc58c00-2d7f-4cc7-9dc8-6e097765b402",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Delete User"
]
},
{
"cell_type": "markdown",
"id": "e6394964-2012-48e5-83e0-726b49934bb0",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"This routine will delete an existing user in the Appwrite project."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f7aad6fd-c8b4-44a0-8ff6-0d9139412fd6",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"#We already initialised the Appwrite client in the above cells\n",
"# Delete user that's created above.\n",
"try:\n",
" users.delete(\"msaik12\")\n",
" print('user deleted !')\n",
"except:\n",
" print(\"user not deleted / doesn't exist !!!\")"
]
},
{
"cell_type": "markdown",
"id": "7d069786-1b78-4507-ab15-3096ef91c7ac",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### List All Users"
]
},
{
"cell_type": "markdown",
"id": "36219a1d-932b-4ca6-8759-e279cbe46f4c",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"This routine will list all users in the Appwrite project."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "902a1c53-f8a0-490c-9104-33583714391e",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"# List all users\n",
"x=users.list()\n",
"users = x.get('users', [])\n",
"for user in users:\n",
" print('User Details:')\n",
" print(f'User ID: {user[\"$id\"]}')\n",
" print(f'Name: {user[\"name\"]}')\n",
" print(f'Email: {user[\"email\"]}')\n",
" print(f'Phone: {user[\"phone\"]}')\n",
" print(f'Registration Date: {user[\"registration\"]}')\n",
" print(f'Status: {\"Active\" if user[\"status\"] else \"Inactive\"}')\n",
" print('---')"
]
},
{
"cell_type": "markdown",
"id": "184899cb-5d16-4fca-b391-8091097faeb7",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Output"
]
},
{
"cell_type": "markdown",
"id": "1761131b-d9c4-4fa1-85ae-02ea2ff9c8ea",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Display result"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "daf7aece-d7c1-484f-9dee-a1130e5a9489",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"for user in users:\n",
" print('User Details:')\n",
" print(f'User ID: {user[\"$id\"]}')\n",
" print(f'Name: {user[\"name\"]}')\n",
" print(f'Email: {user[\"email\"]}')\n",
" print(f'Phone: {user[\"phone\"]}')\n",
" print(f'Registration Date: {user[\"registration\"]}')\n",
" print(f'Status: {\"Active\" if user[\"status\"] else \"Inactive\"}')\n",
" print('---')\n"
]
},
{
"cell_type": "markdown",
"id": "5357eaf0-db18-49ec-9548-ef58e9817eeb",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
" "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading