Skip to content

Commit a402859

Browse files
added oauth working with python
1 parent d13acc1 commit a402859

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,29 @@ $redirectUrl = $account->createOAuth2Token(
483483

484484
header('Location' . $redirectUrl);
485485
```
486+
```python
487+
from appwrite.client import Client
488+
from appwrite.services.account import Account, OAuthProvider
489+
from flask import Flask, request ,redirect, make_response, jsonify
490+
491+
admin_client = (Client()
492+
.set_endpoint('https://<REGION>.cloud.appwrite.io/v1')
493+
.set_project('<PROJECT_ID>')
494+
.set_key('<API_KEY>')
495+
)
496+
497+
@app.get('/oauth')
498+
def oauth():
499+
account = Account(admin_client)
500+
501+
redirect_url = account.create_o_auth2_token(
502+
OAuthProvider.Github, # Provider
503+
'https://example.com/oauth/success', # Success URL
504+
'https://example.com/oauth/failure', # Failure URL
505+
)
506+
507+
return redirect(redirect_url)
508+
```
486509
{% /multicode %}
487510

488511
Next, create a success callback endpoint that receives the `userId` and `secret` URL parameters, and then calls `createSession` on the server side. This endpoint returns a session object, which you can store in a cookie.
@@ -548,6 +571,38 @@ try {
548571
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
549572
}
550573
```
574+
```python
575+
@app.get('/oauth/success')
576+
def oauth_success():
577+
account = Account(admin_client)
578+
579+
# Get the userId and secret from the URL parameters
580+
user_id = request.args.get('userId')
581+
secret = request.args.get('secret')
582+
583+
try:
584+
# Create the session using the Appwrite client
585+
session = account.createSession(user_id, secret)
586+
587+
# Set the session cookie
588+
res = make_response(jsonify({'success': True}))
589+
590+
# Set session cookie
591+
res.set_cookie(
592+
'session',
593+
session['secret'],
594+
httponly=True,
595+
secure=True,
596+
samesite='Strict',
597+
max_age=session['expire'],
598+
path='/'
599+
)
600+
601+
return res
602+
603+
except Exception as e:
604+
return jsonify({'success': False, 'error': str(e)}), 400
605+
```
551606
{% /multicode %}
552607

553608
Now the cookie is set, it will be passed to the server with subsequent requests, and you can use it to make authenticated requests to the Appwrite API on behalf of the end-user.

0 commit comments

Comments
 (0)