Skip to content

Commit bda8503

Browse files
awolfdenAdam Wolfman
andauthored
Add Get Directory Route and Refactor UI/UX (#3)
* Add Get Directory route and refactor UI * Update README to reflect webhooks secret env variable * Update README to reflect that directory_id is no longer a required env variable * Update README to remove reference to /users and /groups endpoints Co-authored-by: Adam Wolfman <[email protected]>
1 parent ab9dd74 commit bda8503

File tree

7 files changed

+84
-12
lines changed

7 files changed

+84
-12
lines changed

python-flask-directory-sync-example/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ If you get stuck, please reach out to us at [email protected] so we can help.
5454

5555
6. Obtain and make note of the following values. In the next step, these will be set as environment variables.
5656
- Your [WorkOS API key](https://dashboard.workos.com/api-keys)
57-
- Your `DIRECTORY_ID`, in the format `directory_<random-alphanumeric-string>`, retrievable from the URL in the Directory Sync area of the WorkOS dashboard:
58-
![](directory_id_location.png)
57+
- Your `WEBHOOKS_SECRET`, retrievable from the URL in the WEBHOOKS area of the WorkOS dashboard. This is only required if you are utilizing the webhooks route of this application to receive and validate webhook events.
58+
5959

6060
7. Ensure you're in the root directory for the example app, `python-flask-directory-sync-example/`. Create a `.env` file to securely store the environment variables. Open this file with the Nano text editor. (This file is listed in this repo's `.gitignore` file, so your sensitive information will not be checked into version control.)
6161
```bash
@@ -66,7 +66,7 @@ If you get stuck, please reach out to us at [email protected] so we can help.
6666
8. Once the Nano text editor opens, you can directly edit the `.env` file by listing the environment variables:
6767
```bash
6868
export WORKOS_API_KEY=<value found in step 6>
69-
export DIRECTORY_ID=<value found in step 6>
69+
export WEBHOOKS_SECRET=<value found in step 6>
7070
```
7171

7272
To exit the Nano text editor, type `CTRL + x`. When prompted to "Save modified buffer", type `Y`, then press the `Enter` or `Return` key.
@@ -79,7 +79,7 @@ If you get stuck, please reach out to us at [email protected] so we can help.
7979
You can ensure the environment variables were set correctly by running the following commands. The output should match the corresponding values.
8080
```bash
8181
(env) $ echo $WORKOS_API_KEY
82-
(env) $ echo $DIRECTORY_ID
82+
(env) $ echo $WEBHOOKS_SECRET
8383
```
8484

8585
## Start the server
@@ -89,7 +89,7 @@ If you get stuck, please reach out to us at [email protected] so we can help.
8989
flask run
9090
```
9191

92-
2. Once the server is running, navigate to http://localhost:5000 to view the home page of the app where you can then select the view for users or groups. Alternatively you could also view these directly using the following url's: http://localhost:5000/users and http://localhost:5000/groups.
92+
2. Once the server is running, navigate to http://localhost:5000 to view the home page of the app where you can then select the view for users or groups.
9393

9494
- The `/users` URL corresponds to the WorkOS API's [List Directory Users endpoint](https://workos.com/docs/reference/directory-sync/user/list)
9595
- The `/groups` URL corresponds to the WorkOS API's [List Directory Groups endpoint](https://workos.com/docs/reference/directory-sync/group/list)

python-flask-directory-sync-example/app.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,39 @@
1212

1313
@app.route('/')
1414
def home():
15-
return render_template('home.html')
15+
directories = workos.client.directory_sync.list_directories()
16+
print(directories)
17+
directoryNames = []
18+
for i in directories['data']:
19+
directoryNames.append(i['name'])
20+
print(directoryNames)
21+
return render_template('home.html', directories=directories)
22+
23+
@app.route('/directory')
24+
def directory():
25+
directory_id = request.args.get('id')
26+
print(directory_id)
27+
directory = workos.client.directory_sync.get_directory(directory_id)
28+
print(directory)
29+
return render_template('directory.html', directory=directory, id=directory['id'])
1630

1731
@app.route('/users')
1832
def directory_users():
33+
directory_id = request.args.get('id')
1934
users = workos_client.directory_sync.list_users(directory=directory_id)
2035
return render_template('users.html', users=users)
2136

2237

2338
@app.route('/groups')
2439
def directory_groups():
40+
directory_id = request.args.get('id')
2541
groups = workos_client.directory_sync.list_groups(directory=directory_id)
2642

2743
return render_template('groups.html', groups=groups)
2844

2945
@app.route('/webhooks', methods=['POST'])
3046
def webhooks():
47+
print(request)
3148
payload = request.get_data()
3249
sig_header = request.headers['WorkOS-Signature']
3350

python-flask-directory-sync-example/static/home.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ body {
8484
width: 95%;
8585
}
8686

87+
.directory_button {
88+
width: 40%;
89+
margin-top: 20px;
90+
}
91+
8792
h2, h1 {
8893
text-align: center;
8994
color: #555555;
@@ -143,6 +148,8 @@ div.text_box {
143148
border-radius: 5px;
144149
border: 2px solid #6363f1;
145150
width: 75%;
151+
max-height: 400px;
152+
overflow: scroll;
146153
padding: 10px;
147154
word-wrap: break-word;
148155
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<html>
2+
<head>
3+
<link rel="stylesheet" href="../static/home.css">
4+
</head>
5+
<body class="container_success">
6+
<div class="logged_in_nav">
7+
<div class="flex">
8+
<p>You're logged in, here is your users object!</p>
9+
</div>
10+
<div>
11+
<img src="../static/images/workos_logo_new.png" alt="workos logo">
12+
</div>
13+
</div>
14+
<div class='flex'>
15+
<div class="logged_in_div_left">
16+
<div>
17+
<h1>Your app,</h1>
18+
<h2>Enterprise Ready</h2>
19+
</div>
20+
<div>
21+
<a href="https://workos.com/" target="_blank"><button class='button'>WorkOS</button></a>
22+
<a href="https://workos.com/docs" target="_blank"><button class='button'>Documentation</button></a>
23+
<a href="https://workos.com/docs/reference" target="_blank"><button class='button'>API Reference</button></a>
24+
<a href="https://workos.com/blog" target="_blank"><button class='button'>Blog</button></a>
25+
<a href="javascript:history.back()"><button class='button'>Home</button></a>
26+
27+
</div>
28+
</div>
29+
<div class="logged_in_div_right">
30+
<div class="flex_column">
31+
<h2>Raw Directory Object Details</h2>
32+
<div class="text_box">
33+
<p>{{ directory }}</p>
34+
</div>
35+
</div>
36+
<div class="flex">
37+
<a class="button directory_button" href="/users?id={{id}}">Users</a>
38+
<br>
39+
<a class="button directory_button" href="/groups?id={{id}}">Groups</a>
40+
</div>
41+
</div>
42+
</div>
43+
</body>
44+
</html>

python-flask-directory-sync-example/templates/groups.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ <h2>Enterprise Ready</h2>
2222
<a href="https://workos.com/docs" target="_blank"><button class='button'>Documentation</button></a>
2323
<a href="https://workos.com/docs/reference" target="_blank"><button class='button'>API Reference</button></a>
2424
<a href="https://workos.com/blog" target="_blank"><button class='button'>Blog</button></a>
25-
<a href="javascript:history.back()"><button class='button'>Home</button></a>
25+
<a href="{{ url_for('home') }}"><button class='button'>Home</button></a>
2626
</div>
2727
</div>
2828
<div class="logged_in_div_right">
2929
<div class="flex_column">
30-
<h2>Raw Profile Response Details</h2>
30+
<h2>Raw Groups Response Details</h2>
3131
<div class="text_box">
3232
<p>{{groups}}</p>
3333
</div>

python-flask-directory-sync-example/templates/home.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ <h1>WorkOS</h1>
1313
</div>
1414

1515
<h2>Python Flask Directory Sync Example App</h2>
16-
<a class="button login_button" href="{{ url_for('directory_users') }}">Users</a>
17-
<br>
18-
<a class="button login_button" href="{{ url_for('directory_groups') }}">Groups</a>
16+
17+
{% for i in directories.data %}
18+
<a class="button login_button" href="/directory?id={{i['id']}}">{{ i['name'] }}</a>
19+
{% endfor %}
20+
1921
</div>
2022
</body>
2123
</html>
24+
25+

python-flask-directory-sync-example/templates/users.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ <h2>Enterprise Ready</h2>
2222
<a href="https://workos.com/docs" target="_blank"><button class='button'>Documentation</button></a>
2323
<a href="https://workos.com/docs/reference" target="_blank"><button class='button'>API Reference</button></a>
2424
<a href="https://workos.com/blog" target="_blank"><button class='button'>Blog</button></a>
25-
<a href="javascript:history.back()"><button class='button'>Home</button></a>
25+
<a href="{{ url_for('home') }}"><button class='button'>Home</button></a>
2626

2727
</div>
2828
</div>

0 commit comments

Comments
 (0)