@@ -72,6 +72,17 @@ $adminClient = (new Client())
72
72
->setKey('<YOUR_API_KEY>'); // Your secret API key
73
73
74
74
75
+ ```
76
+ ```python
77
+ from appwrite.client import Client
78
+
79
+ admin_client = (Client()
80
+ .set_endpoint('https://<REGION>.cloud.appwrite.io/v1') # Your API Endpoint \
81
+ .set_project('<PROJECT_ID>') # Your project ID
82
+ .set_key('<YOUR_API_KEY>') # Your secret API key
83
+ )
84
+
85
+
75
86
```
76
87
{% /multicode %}
77
88
@@ -105,6 +116,22 @@ if ($session) {
105
116
$sessionClient->setSession($session);
106
117
}
107
118
```
119
+
120
+ ```python
121
+ from flask import request
122
+ from appwrite.client import Client
123
+
124
+ session_client = (Client()
125
+ .set_endpoint('https://<REGION>.cloud.appwrite.io/v1') # Your API Endpoint
126
+ .set_project('<PROJECT_ID>') # Your project ID
127
+ )
128
+
129
+ # Get the session cookie from the request
130
+ session = request.cookies.get('session')
131
+ if session:
132
+ session_client.set_session(session)
133
+
134
+ ```
108
135
{% /multicode %}
109
136
110
137
# Creating email/password sessions {% #creating-sessions %}
@@ -178,6 +205,39 @@ try {
178
205
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
179
206
}
180
207
```
208
+ ```python
209
+ from flask import Flask, request, jsonify, make_response
210
+
211
+ # Initialize admin client here
212
+ # ...
213
+
214
+ @app.post('/login')
215
+ def login():
216
+ body = request.json
217
+ # Get email and password from request
218
+ email = body['email']
219
+ password = body['password']
220
+
221
+ try:
222
+ account = Account(admin_client)
223
+
224
+ # Create the session using the Appwrite client
225
+ session = account.create_email_password_session(email, password)
226
+ resp = make_response(jsonify({'success': True}))
227
+
228
+ # Set the session cookie
229
+ resp.set_cookie('session',
230
+ session['secret'],
231
+ httponly=True,
232
+ secure=True,
233
+ samesite='Strict',
234
+ expires=session['expire'],
235
+ path='/'
236
+ )
237
+ return resp
238
+ except Exception as e:
239
+ return jsonify({'success': False, 'error': str(e)}), 400
240
+ ```
181
241
{% /multicode %}
182
242
183
243
We also recommend using the `httpOnly`, `secure`, and `sameSite` cookie options to ensure that the cookie is only sent over HTTPS,
@@ -242,6 +302,30 @@ try {
242
302
} catch (Exception $e) {
243
303
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
244
304
}
305
+ ```
306
+ ```python
307
+ # Initialize the session client here
308
+
309
+ @app.get('/user')
310
+ def get_user():
311
+ # First, read the session cookie from the request
312
+ session = request.cookies.get('session')
313
+
314
+ # If the session cookie is not present, return an error
315
+ if not session:
316
+ return jsonify({'success': False, 'error': 'Unauthorized'}), 401
317
+
318
+ # pass the session cookie to the Appwrite client
319
+ session_client.set_session(session)
320
+ account = Account(session_client)
321
+
322
+ # Now, you can make authenticated requests to the Appwrite API
323
+ try:
324
+ user = account.get()
325
+ return jsonify({'success': True, 'user': user})
326
+ except Exception as e:
327
+ return jsonify({'success': False, 'error': str(e)}), 400
328
+
245
329
```
246
330
{% /multicode %}
247
331
0 commit comments