Skip to content

Add example TypeScript app for Audit Logs #6

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

Merged
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions typescript-audit-logs-example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
.env
out/
tsconfig.tsbuildinfo
74 changes: 74 additions & 0 deletions typescript-audit-logs-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# TypeScript Example App with Audit Logs powered by WorkOS

An example Node.js / TypeScript application demonstrating how to use the [WorkOS Node.js SDK](https://github.com/workos/workos-node) to send and retrieve Audit Log events. This app is not meant to show a real-world example of an Audit Logs implementation, but rather to show concrete examples of how events can be sent using the Node.js SDK.

## Prerequisites

-Node.js version 10+

## TypeScript Project Setup

1. Clone the main repo and install dependencies for the app you'd like to use:

```bash
# HTTPS
git clone https://github.com/workos/typescript-example-applications.git
```

or

```bash
# SSH
git clone git@github.com:workos/typescript-example-applications.git
```

2. Navigate to Audit Logs app within the cloned repo.

```bash
$ cd typescript-example-applications/typescript-audit-logs-example
```

3. Install the dependencies.
```bash
$ npm install
```

## Configure your environment

4. Grab your API Key and Client ID from the WorkOS Dashboard. Create a `.env`
file at the root of the project, and store these like so:

```bash
WORKOS_API_KEY=sk_xxxxxxxxxxxxx
WORKOS_CLIENT_ID=project_xxxxxxxxxxxx
```

## Testing the Integration

5. Start the server and head to `http://localhost:8000/ to begin the login flow!

```sh
npm start
```

## Audit Logs setup

Follow the [Audit Logs configuration steps](https://workos.com/docs/audit-logs/emit-an-audit-log-event/sign-in-to-your-workos-dashboard-account-and-configure-audit-log-event-schemas) to set up the following 5 events that are sent with this example:

Action title: "user.signed_in" | Target type: "team"
Action title: "user.logged_out" | Target type: "team"
Action title: "user.organization_set" | Target type: "team"
Action title: "user.organization_deleted" | Target type: "team"
Action title: "user.connection_deleted" | Target type: "team"

Next, take note of the Organization ID for the Org which you will be sending the Audit Log events for. This ID gets entered into the splash page of the example application.

Once you enter the Organization ID and submit it, you will be brought to the page where you'll be able to send the audit log events that were just configured. You'll also notice that the action of setting the Organization triggered an Audit Log already. Click the buttons to send the respective events.

To obtain a CSV of the Audit Log events that were sent for the last 30 days, click the "Export Events" button. This will bring you to a new page where you can download the events. Downloading the events is a 2 step process. First you need to create the report by clicking the "Generate CSV" button. Then click the "Access CSV" button to download a CSV of the Audit Log events for the selected Organization for the past 30 days.

## Need help?

First, make sure to reference the Audit Logs docs at https://workos.com/docs/audit-logs.

If you get stuck and aren't able to resolve the issue by reading our docs or API reference, you can reach out to us at support@workos.com and we'll lend a hand.
96 changes: 96 additions & 0 deletions typescript-audit-logs-example/audit_log_events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const user_signed_in = {
"action": "user.signed_in",
"occurred_at": new Date().toISOString(),
"actor": {
"type": "user",
"id": "user_01GBNJC3MX9ZZJW1FSTF4C5938",
},
"targets": [
{
"type": "team",
"id": "team_01GBNJD4MKHVKJGEWK42JNMBGS",
},
],
"context": {
"location": "123.123.123.123",
"user_agent": "Chrome/104.0.0.0",
},
}

const user_logged_out = {
"action": "user.logged_out",
"occurred_at": new Date().toISOString(),
"actor": {
"type": "user",
"id": "user_01GBNJC3MX9ZZJW1FSTF4C5938",
},
"targets": [
{
"type": "team",
"id": "team_01GBNJD4MKHVKJGEWK42JNMBGS",
},
],
"context": {
"location": "123.123.123.123",
"user_agent": "Chrome/104.0.0.0",
},
}

const user_organization_set = {
"action": "user.organization_set",
"occurred_at": new Date().toISOString(),
"actor": {
"type": "user",
"id": "user_01GBNJC3MX9ZZJW1FSTF4C5938",
},
"targets": [
{
"type": "team",
"id": "team_01GBNJD4MKHVKJGEWK42JNMBGS",
},
],
"context": {
"location": "123.123.123.123",
"user_agent": "Chrome/104.0.0.0",
},
}

const user_organization_deleted = {
"action": "user.organization_deleted",
"occurred_at": new Date().toISOString(),
"actor": {
"type": "user",
"id": "user_01GBNJC3MX9ZZJW1FSTF4C5938",
},
"targets": [
{
"type": "team",
"id": "team_01GBNJD4MKHVKJGEWK42JNMBGS",
},
],
"context": {
"location": "123.123.123.123",
"user_agent": "Chrome/104.0.0.0",
},
}

const user_connection_deleted = {
"action": "user.connection_deleted",
"occurred_at": new Date().toISOString(),
"actor": {
"type": "user",
"id": "user_01GBNJC3MX9ZZJW1FSTF4C5938",
},
"targets": [
{
"type": "team",
"id": "team_01GBNJD4MKHVKJGEWK42JNMBGS",
},
],
"context": {
"location": "123.123.123.123",
"user_agent": "Chrome/104.0.0.0",
},
}

module.exports = { user_signed_in, user_logged_out, user_organization_set, user_organization_deleted, user_connection_deleted };
22 changes: 22 additions & 0 deletions typescript-audit-logs-example/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const express = require('express')
import 'dotenv/config.js'
const router = require('./routes/index.ts')
import morgan from 'morgan'

const app = express()

const port = process.env.PORT || 8000

app.use('/public', express.static('public'))

app.use(express.urlencoded({ extended: false }))

app.use(express.json())

app.use(morgan('dev'))

app.use('/', router)

app.listen(port, () => {
console.log(`⚡️ [server]: Server is running at https://localhost:${port}`)
})
Loading