Skip to content

Commit b9449e4

Browse files
added ssr login with email and password working with python
1 parent 2606549 commit b9449e4

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

src/routes/docs/products/auth/server-side-rendering/+page.markdoc

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ $adminClient = (new Client())
7272
->setKey('<YOUR_API_KEY>'); // Your secret API key
7373

7474

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+
7586
```
7687
{% /multicode %}
7788

@@ -105,6 +116,22 @@ if ($session) {
105116
$sessionClient->setSession($session);
106117
}
107118
```
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+
```
108135
{% /multicode %}
109136

110137
# Creating email/password sessions {% #creating-sessions %}
@@ -178,6 +205,39 @@ try {
178205
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
179206
}
180207
```
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+
```
181241
{% /multicode %}
182242

183243
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 {
242302
} catch (Exception $e) {
243303
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
244304
}
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+
245329
```
246330
{% /multicode %}
247331

0 commit comments

Comments
 (0)