Skip to content

Commit

Permalink
pre-alpha: Add descope headers to node sdk, fix build, lint, fix exam…
Browse files Browse the repository at this point in the history
…ples (#70)

1. Add descope headers to node sdk
2. Fix build
3. Fix lint
4. Fix format
5. Add husky to commit
6. Fix examples
  • Loading branch information
dorsha authored Oct 31, 2022
1 parent d2d0121 commit c556d16
Show file tree
Hide file tree
Showing 29 changed files with 2,703 additions and 1,093 deletions.
71 changes: 35 additions & 36 deletions .eslintrc.cjs → .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
const fs = require('fs');

module.exports = {
root: true,
extends: ['airbnb-base', 'prettier', 'plugin:jest/recommended', 'plugin:import/typescript', 'airbnb-typescript/base'],
plugins: [
{
"root": true,
"extends": [
"airbnb-base",
"prettier",
"plugin:jest/recommended",
"plugin:import/typescript",
"airbnb-typescript/base"
],
"plugins": [
"@typescript-eslint",
"prettier",
"import",
"prefer-arrow",
"jest-dom",
"jest",
"jest-formatting",
"no-only-tests"],
parser: '@typescript-eslint/parser',
parserOptions: {
project: './tsconfig.json',
"no-only-tests"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.json"
},
env: {
jest: true,
node: true,
"env": {
"jest": true,
"node": true
},
settings: {
"settings": {
"import/parsers": {
"@typescript-eslint/parser": [".ts", ".tsx", ".js"]
},
Expand All @@ -30,24 +35,20 @@ module.exports = {
}
}
},
rules: {
"rules": {
"semi": ["error", "always"],
"no-tabs": ["error", { "allowIndentationTabs": true }],
"@typescript-eslint/indent": ["off"],
"quotes": [
"error",
"single",
{ "avoidEscape": true, "allowTemplateLiterals": true }
],
"quotes": ["error", "single", { "avoidEscape": true, "allowTemplateLiterals": true }],
"@typescript-eslint/quotes": [
"error",
"single",
{ "avoidEscape": true, "allowTemplateLiterals": true }
],
"semi": ["error", "never"],
"space-before-blocks": 2,
"space-before-function-paren": 2,
"no-multi-spaces": 2,
"@typescript-eslint/semi": "off",
"@typescript-eslint/semi": "error",
"no-unexpected-multiline": "error",
"@typescript-eslint/comma-dangle": ["off"],
"comma-dangle": ["off"],
Expand All @@ -71,21 +72,19 @@ module.exports = {
"import/no-extraneous-dependencies": [
2,
{
"devDependencies": [
"!./src/**/*"
]
"devDependencies": ["!./src/**/*"]
}
],
'import/extensions': [
'error',
'ignorePackages',
"import/extensions": [
"error",
"ignorePackages",
{
js: 'never',
jsx: 'never',
ts: 'never',
tsx: 'never',
},
],
"js": "never",
"jsx": "never",
"ts": "never",
"tsx": "never"
}
]
},
ignorePatterns: ['.eslintrc.cjs', 'build/*', 'dist/*', 'coverage/*', '**/testutils/*'],
};
"ignorePatterns": [".eslintrc.cjs", "build/*", "dist/*", "coverage/*", "**/testutils/*"]
}
12 changes: 6 additions & 6 deletions .github/workflows/build_docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ jobs:
CI: true

- name: Build example
run: npm run build
run: npm run build
working-directory: examples/es6
env:
CI: true
CI: true

- name: Build example cert
run: npm run generateCerts
run: npm run generateCerts
working-directory: examples/es6
env:
CI: true
CI: true

- name: Build example docker image
working-directory: examples/es6
run: |
docker build . --tag ${{ env.EXAMPLE_IMAGE_NAME }}
docker build . --tag ${{ env.EXAMPLE_IMAGE_NAME }}
- name: Log in to the Container registry
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
Expand All @@ -62,4 +62,4 @@ jobs:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Push the tagged Docker image
run: docker push ${{ env.EXAMPLE_IMAGE_NAME }}
run: docker push ${{ env.EXAMPLE_IMAGE_NAME }}
13 changes: 6 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ jobs:
run: npm run build
env:
NODE_AUTH_TOKEN: ${{ secrets.CI_NPM_READ_ORG }}
- name: Examples
run: |
(cd examples/es6 && npm i && npm run build)
(cd examples/commonjs && npm i)
- name: Examples - es6
run: cd examples/es6 && npm i && npm run build
- name: Examples - common
run: cd examples/commonjs && npm i

eslint:
name: 🪥 ESLint
Expand All @@ -49,7 +49,7 @@ jobs:
env:
CI: true
NODE_AUTH_TOKEN: ${{ secrets.CI_NPM_READ_ORG }}
- run: npm run check-format
- run: npm run format-check
- run: npm run lint

gitleaks:
Expand All @@ -70,8 +70,7 @@ jobs:
CI: true
NODE_AUTH_TOKEN: ${{ secrets.CI_NPM_READ_ORG }}
- name: Gitleaks
run: |
npm run leaks
run: npm run leaks
shell: bash

unit-test:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: 📢 publish

on:
create:
push:
tags:
- 'release/**'

Expand Down
5 changes: 1 addition & 4 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
npm run format
npm run lint
npm run leaks
npm test
npm run format-lint
2 changes: 1 addition & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# @descope:registry=https://npm.pkg.github.com
@descope:registry=https://registry.npmjs.org
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"printWidth": 100,
"tabWidth": 2,
"useTabs": false,
"semi": false,
"semi": true,
"singleQuote": true,
"trailingComma": "all"
}
37 changes: 19 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ npm i --save @descope/node-sdk

## What do you want to implement?

Click one of the following links to open the documentation for that specific functionality.
Click one of the following links to open the documentation for that specific functionality.

- [x] [One time passwords (OTP)](./docs/otp.md)
- [x] [Magic Links](./docs/magiclink.md)
Expand All @@ -26,7 +26,7 @@ Instantly run the end-to-end ExpresSDK for NodeJS examples, as shown below. The

### Prerequisites

Run the following commands in your project. Replace any instance of `<ProjectID>` in the code below with your company's Project ID, which can be found in the [Descope console](https://app.descope.com).
Run the following commands in your project. Replace any instance of `<ProjectID>` in the code below with your company's Project ID, which can be found in the [Descope console](https://app.descope.com).

This commands will add the Descope NodeJS SDK as a project dependency, clone the SDK repository locally, and set the `DESCOPE_PROJECT_ID`.

Expand All @@ -40,27 +40,28 @@ export DESCOPE_PROJECT_ID=<ProjectID>
**TL;DR**: Run `npm run quick`

Run the following commands in the root of the project to build and run the examples.

1. Run this to start the ES6 typescript module example

```code bash
npm i && \
npm run build && \
cd examples/es6 && \
npm i && \
npm run generateCerts && \
npm start
```
```code bash
npm i && \
npm run build && \
cd examples/es6 && \
npm i && \
npm run generateCerts && \
npm start
```

2. Run this to start the commonjs example

```code bash
npm i && \
npm run build && \
cd examples/commonjs && \
npm i && \
npm run generateCerts && \
npm start
```
```code bash
npm i && \
npm run build && \
cd examples/commonjs && \
npm i && \
npm run generateCerts && \
npm start
```

## License

Expand Down
48 changes: 25 additions & 23 deletions docs/magiclink.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,52 +11,54 @@ flowchart LR

### Prerequisites

Replace any instance of `<ProjectID>` in the code below with your company's Project ID, which can be found in the [Descope console](https://app.descope.com).
Replace any instance of `<ProjectID>` in the code below with your company's Project ID, which can be found in the [Descope console](https://app.descope.com).

* Run the following commands in your project
- Run the following commands in your project

These commands will add the Descope NodeJS SDK as a project dependency.
These commands will add the Descope NodeJS SDK as a project dependency.

```bash
npm i --save @descope/node-sdk
```
```bash
npm i --save @descope/node-sdk
```

* Import and initialize the ExpresSDK for NodeJS client in your source code
- Import and initialize the ExpresSDK for NodeJS client in your source code

```javascript
import DescopeClient from '@descope/node-sdk';
const descopeClient = DescopeClient({ projectId: <ProjectID> });
```
or
```javascript
import DescopeClient from '@descope/node-sdk';
const descopeClient = DescopeClient({ projectId: <ProjectID> });
```

```javascript
const sdk = require('@descope/node-sdk');
const descopeClient = sdk({ projectId: <ProjectID> });
```
or

```javascript
const sdk = require('@descope/node-sdk');
const descopeClient = sdk({ projectId: <ProjectID> });
```

### 1. Customer Sign-up

In your sign-up route using magic link (for example, `myapp.com/signup`) generate a sign-up request and send the magic link via the selected delivery method. In the example below an email is sent to "[email protected]" containing the magic link and the link will automatically return back to the provided URL ("https://mydomain.com/verify"). In additon, optional user data (for exmaple, a custom username in the code sample below) can be gathered during the sign-up process.

```javascript
await descopeClient.magiclink.signUp.email("[email protected]", { name: "custom name" })
await descopeClient.magiclink.signUp.email('[email protected]', { name: 'custom name' });
```

### 2. Customer Sign-in
In your sign-in route using magic link (for exmaple, `myapp.com/login`) generate a sign-in request send the magic link via the selected delivery method. In the example below an email is sent to "[email protected]" containing the magic link and the link will automatically return back to the provided URL ("https://mydomain.com/verify").

In your sign-in route using magic link (for exmaple, `myapp.com/login`) generate a sign-in request send the magic link via the selected delivery method. In the example below an email is sent to "[email protected]" containing the magic link and the link will automatically return back to the provided URL ("https://mydomain.com/verify").

```javascript
await descopeClient.magiclink.signIn.email("[email protected]")
await descopeClient.magiclink.signIn.email('[email protected]');
```

### 3. Customer Verification

In your verify customer route for magic link (for example, `mydomain.com/verify`) verify the token from either a customer sign-up or sign-in.

```javascript
const out = await descopeClient.magiclink.verify(token)
const out = await descopeClient.magiclink.verify(token);
if (out.data.cookies) {
res.set('Set-Cookie', out.data.cookies)
res.set('Set-Cookie', out.data.cookies);
}
```

Expand All @@ -65,8 +67,8 @@ if (out.data.cookies) {
Session validation checks to see that the visitor to your website or application is who they say they are, by comparing the value in the validation variables against the session data that is already stored.

```javascript
const out = await descopeClient.validateSession(session_jwt, refresh_jwt)
const out = await descopeClient.validateSession(session_jwt, refresh_jwt);
if (out?.cookies) {
res.set('Set-Cookie', out.cookies)
res.set('Set-Cookie', out.cookies);
}
```
Loading

0 comments on commit c556d16

Please sign in to comment.