-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f630bf8
commit 4185e24
Showing
2 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,74 @@ | ||
# cardboard.js | ||
JS/TS Wrapper for the CardBoard API | ||
|
||
# Examples | ||
## Initial Setup | ||
```typescript | ||
import {config} from "dotenv" | ||
|
||
import { Cardboard } from "cardboard.js" | ||
|
||
config() | ||
|
||
const clientSecret = process.env.CLIENT_SECRET | ||
const clientId = process.env.CLIENT_ID | ||
|
||
if (!clientSecret) { | ||
throw new Error("Missing client secret") | ||
} | ||
|
||
if (!clientId) { | ||
throw new Error("Missing client id") | ||
} | ||
|
||
const cb = new Cardboard(clientId, clientSecret) | ||
``` | ||
## Usage | ||
```typescript | ||
import express from "express" | ||
|
||
const app = express() | ||
|
||
app.use(express.json()) | ||
|
||
// single user mock db, you would have something way more complicated here for multiple users and your app's own data | ||
let at="" | ||
|
||
// route setup in redirect uri on cardboard.ink | ||
app.get("/login", async (req, res) => { | ||
const code = req.query.code as string | ||
|
||
// initializes long term session on cardboard's end (30 days) | ||
const loginData = await cb.exchangeInitialToken(code) | ||
|
||
// your logic to set session in your app | ||
at = loginData.access_token | ||
res.send(loginData) | ||
return | ||
}) | ||
|
||
app.get("/logout", async (req, res) => { | ||
|
||
// remove session on cardboard's end | ||
const logout = await cb.revokeToken(at) | ||
|
||
// your logic to remove session in your app | ||
at = "" | ||
res.send(logout) | ||
return | ||
}) | ||
|
||
app.get("/user", async (req, res) => { | ||
if (at === "") { | ||
res.send(`<h1>Not logged in</h1> <a href='https://cardboard.ink/auth?client_id=${clientId}'>Login</a>`) | ||
return | ||
} | ||
const user = await cb.getUserInfo(at) | ||
res.json(user) | ||
return | ||
}) | ||
|
||
app.listen(3000, () => { | ||
console.log("Listening on port 3000") | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters