|
1 | 1 | # SnapAuth SDK for NodeJS
|
| 2 | + |
| 3 | +The official NodeJS SDK for SnapAuth 🫰 |
| 4 | + |
| 5 | +This is for _server_ code. |
| 6 | +If you're looking for the _client_ integration, check out `@snapauth/sdk`. |
| 7 | + |
| 8 | +- [SnapAuth Homepage](https://www.snapauth.app) |
| 9 | +- [Docs](https://docs.snapauth.app) |
| 10 | +- [Dashboard](https://dashboard.snapauth.app) |
| 11 | +- [Github](https://github.com/snapauthapp/sdk-node) |
| 12 | + |
| 13 | +## Installation and Setup |
| 14 | + |
| 15 | +```bash |
| 16 | +npm i --save @snapauth/node-sdk |
| 17 | +# yarn add @snapauth/sdk |
| 18 | +# etc |
| 19 | +``` |
| 20 | + |
| 21 | +```typescript |
| 22 | +import SnapAuth from '@snapauth/node-sdk' |
| 23 | +const snapAuth = new SnapAuth(process.env.SNAPAUTH_SECRET_KEY) |
| 24 | +``` |
| 25 | +> [!TIP] |
| 26 | +> The SDK will auto-detect a `SNAPAUTH_SECRET_KEY` environment variable. |
| 27 | +> If that's where you've set up your Secret Key, you can simplify this to |
| 28 | +> `const snapAuth = new SnapAuth()`. |
| 29 | +
|
| 30 | + |
| 31 | +## Usage |
| 32 | + |
| 33 | +All examples are in TypeScript, based roughly on an ExpressJS app. |
| 34 | + |
| 35 | +General usage is as follows: |
| 36 | + |
| 37 | +```typescript |
| 38 | +const response = await snapAuth.someApiCall(param1, ...) |
| 39 | +if (response.ok) { |
| 40 | + // Got back a 2xx |
| 41 | + // console.assert(response.result !== null) |
| 42 | + useDataFrom(response.result) |
| 43 | +} else { |
| 44 | + // Any other response, or network error |
| 45 | + // console.assert(response.result === null) |
| 46 | + // console.assert(response.errors.length > 0) |
| 47 | + console.error(response.errors) |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +This is similar to `fetch()` which you're probably already familiar with. |
| 52 | + |
| 53 | +If the API call succeeded, the [response](https://docs.snapauth.app/server.html) will be in `response.result`. |
| 54 | + |
| 55 | +> [!NOTE] |
| 56 | +> Even on successful responses, `response.errors` may contain information, such as deprecation or usage warnings. |
| 57 | +> We suggest always examining this value. |
| 58 | +
|
| 59 | +### Completing credential registration |
| 60 | + |
| 61 | +```typescript |
| 62 | +app.post('/register', async (request, response) => { |
| 63 | + // You should have POSTed something like this: |
| 64 | + // { |
| 65 | + // token: string |
| 66 | + // username: string |
| 67 | + // } |
| 68 | + const token = request.body.token |
| 69 | + const username = request.body.username |
| 70 | + // Do whatever you normally do to create a new User record |
| 71 | + const user = createUserWithUsername(username) |
| 72 | + // Then save the new passkey |
| 73 | + const credentialInfo = await snapAuth.attachRegistration(token, { |
| 74 | + id: user.id, // You may need to cast this to a string first, e.g. `String(user.id)` |
| 75 | + handle: user.username, // Probably the value from above |
| 76 | + }) |
| 77 | + // That's it. Proceed as normal. |
| 78 | +}) |
| 79 | +``` |
| 80 | + |
| 81 | +> [!NOTE] |
| 82 | +> The `id` is what you should use during authentication; it can not be changed. |
| 83 | +> The `handle` is to make client code more straightforward, and is typically the value the user would type in to a username field. |
| 84 | +> |
| 85 | +> You MAY hash or obfuscate the `handle`, or omit it entirely. |
| 86 | +> If you do, you'll need to either a) repeat the procedure in client code during authentication or b) rely on the user's id instead. |
| 87 | +
|
| 88 | +### Authenticating |
| 89 | + |
| 90 | +```typescript |
| 91 | +app.post('/signin', async (request, response) => { |
| 92 | + // { token: string } |
| 93 | + const token = request.body.token |
| 94 | + const auth = await snapAuth.signIn(token) |
| 95 | + if (auth.ok) { |
| 96 | + signInUserWithId(auth.result.user.id) |
| 97 | + } else { |
| 98 | + // Look at auth.errors and decide what, if anything, to display to the user. |
| 99 | + } |
| 100 | +}) |
| 101 | +``` |
| 102 | + |
| 103 | +## Building the SDK |
| 104 | + |
| 105 | +Run `npm run watch` to keep the build running continually on file change. |
| 106 | + |
| 107 | +To make the local version available for linking, run `npm link` in this directory. |
| 108 | + |
| 109 | +In the project that should _use_ the local version, run `npm link '@snapauth/node-sdk'` which will set up the symlinking. |
0 commit comments