Skip to content

Commit 44c9292

Browse files
authored
Initial implementation (#1)
This is a first-pass implementation for the NodeJS SDK: - Core API calls to register and authenticate - Error handling - Type exports - API key auto-detection - License
1 parent e3f64a5 commit 44c9292

File tree

8 files changed

+1976
-0
lines changed

8 files changed

+1976
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist/
2+
node_modules/

LICENSE

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2023-2034, SnapAuth
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
8+
1. Redistributions of source code must retain the above copyright notice, this
9+
list of conditions and the following disclaimer.
10+
11+
2. Redistributions in binary form must reproduce the above copyright notice,
12+
this list of conditions and the following disclaimer in the documentation
13+
and/or other materials provided with the distribution.
14+
15+
3. Neither the name of the copyright holder nor the names of its
16+
contributors may be used to endorse or promote products derived from
17+
this software without specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,109 @@
11
# 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

Comments
 (0)