Skip to content

Commit

Permalink
Improve sdk (#18)
Browse files Browse the repository at this point in the history
* Start implementing new sdk

* Start implementing sdk v2

* Start implementing session

* Implement session and make publish possible

* ✨ Add PR template

* ✨ Setup Eslint and Prettier

* 🎨 Hand over code style to prettier

* 🎨 Format and lint project

* ✨ Generate open API spec

* ✨ Include X-Corbado-SDK header

* 🎨 Begin harmonising ReadMe with PHP readme

* 🧑‍💻 Enforce code style with extensions

* 🎨 Move bulk of script to makefile

* 🎨 Refactor tsconfig to accomodate cjs and esm builds

* 🎨 Refactor config.ts

* 🎨 WIP Mapping folders

* 🎨 Mapping folders cont'd

* 🚧 WIP Re-implement services

* ✨ Implement Errors

* ✨ Implement Services

* ✨ Modify Helpers

* 🎨 ✨ Modify entities

* 🎨 Sundry lint and import update

* ✨ Implement Webhook entities

* ✨ Implement Webhook

* 🧑‍💻 Add highlighter to extensions list

* ✨ Implement Validation

* ⚰️ Remove sdk folder

* 🎨 Tiny clean-up

* 🚨 Lint error fixes

* ⚡️ Tease out configs for cjs and esm

* 🎨 Update makefile to reflect tsconfig

* 🚚 Modify to please esm

* 📦 Generate builds

* 🎨 Improve Session

* 🧑‍💻 Auto-delete unused imports

* 🔧 Setup Jest

* 🔧 Configure jest to run properly

* 🔧 Get jest to read input correctly

* 🧑‍💻 Create test utils

* ✅ Create base tests

* ✅ Create Entity test

* 🎨 Fix issues caught by tests

* ✅ Create service tests

* 📦 Rebuid package based on updated core

* 🚧 WIP Create Auth tests

* Update README.md

* 🚧 WIP tests fix

* 🔧 Setup test env

* ✅ Create generic test utils

* ✨ Configure test ignore and linting

* 🐛 Fix buggy error method

* ✅ Completed tests part 1

* ⚡️ Include updates in cjs and esm builds

* 🐛 fix(helpers.ts): remove console.log statement used for debugging during tests
✨ feat(authToken.test.ts): add support for dynamic remoteAddress and userAgent values in test requests to improve test coverage
✨ feat(emailLink.test.ts): add support for dynamic redirect URL values in test requests to improve test coverage
✨ feat(emailOtp.test.ts): add support for dynamic email values in test requests to improve test coverage
✨ feat(smsOtp.test.ts): add support for dynamic phoneNumber values in test requests to improve test coverage
✨ feat(user.test.ts): add support for dynamic name and email values in test requests to improve test coverage
✨ feat(validation.test.ts): add support for dynamic email and phoneNumber values in test requests to improve test coverage
✨ feat(user.test.ts): add support for dynamic issuer and jwksURI values in test requests to improve test coverage
✨ feat(session.test.ts): add support for dynamic issuer and jwksURI values in test requests to improve test coverage
✨ feat(emailLink.test.ts): add support for dynamic email values in test requests to improve test coverage
✨ feat(emailOtp.test.ts): add support for dynamic email values in test requests to improve test coverage
✨ feat(smsOtp.test.ts): add support for dynamic phoneNumber values in test requests to improve test coverage
✨ feat(user.test.ts): add support for dynamic name and email values in test requests to improve test coverage
✨ feat(validation.test.ts): add support for dynamic email and phoneNumber values in test requests to improve test coverage
✨ feat(user.test.ts): add support for dynamic issuer and jwksURI values in test requests to improve test coverage
✨ feat(session.test.ts): add support for dynamic issuer and jwksURI values in test requests to improve test coverage

* 🔧 refactor(config.ts): rename Configuration class to Config for better naming consistency and clarity

🔧 refactor(assert.ts): move assert functions to a separate Assert class for better organization and reusability

🔧 refactor(helpers.ts): move helper functions to a separate Helper class for better organization and reusability

🔧 refactor(index.ts): update import statement for Configuration class to use the new name Config

🔧 refactor(sdk.ts): update import statement for Configuration class to use the new name Config

🔧 refactor(authTokenService.ts): update import statement for Assert and Helper from the new location

🔧 refactor(emailLinkService.ts): update import statement for Assert and Helper from the new location

🔧 refactor(emailOtpService.ts): update import statement for Assert and Helper from the new location

🔧 refactor(sessionService.ts): update import statement for Assert from the new location

🔧 refactor(smsOTPService.ts): update import statement for Assert and Helper from the new location

🔧 refactor(userService.ts): update import statement for Assert, Helper, and isErrorRsp from the new location

🔧 refactor(validationService.ts): update import statement for Assert, Helper, and isErrorRsp from the new location

* 🐛 fix(server.ts): change port variable case from lowercase port to uppercase PORT to improve semantics
✨ feat(server.ts): add support for process.env.PORT environment variable to be able to run app on a configurable port
🐛 fix(config.d.ts): change class name from Configuration to Config to improve semantics
🐛 fix(config.js): change class name from Configuration to Config to improve semantics
🐛 fix(config.js.map): update sourceMappingURL to reflect class name change
🐛 fix(assert.d.ts): change import statement from '../generated/index.js' to '../generated/index.d.ts' to fix import error
🐛 fix(assert.js): change import statement from '../errors/index.js' to '../errors/index.d.ts' to fix import error
🐛 fix(assert.js.map): update sourceMappingURL to reflect import statement change
🐛 fix(helpers.d.ts): change import statement from '../generated/index.js' to '../generated/index.d.ts' to fix import error
🐛 fix(helpers.js): change import statement from '../errors/index.js' to '../errors/index.d.ts' to fix import error
🐛 fix(helpers.js.map): update sourceMappingURL to reflect import statement change
🐛 fix(index.d.ts): change import statement from './config.js' to './config.d.ts' to fix import error
🐛 fix(index.js.map): update sourceMappingURL to reflect import statement change
🐛 fix(sdk.d.ts): change import statement from './config.js' to './config.d.ts' to fix import error
🐛 fix(sdk.js.map): update sourceMappingURL to reflect import statement change
🐛 fix(authTokenService.js): change import statement from '../heplers/index.js' to '../helpers/index.js' to fix import error
🐛 fix(emailLinkService.js): change import statement from '../heplers/index.js' to '../helpers/index.js' to fix import error
🐛 fix(emailOtpService.js): change import statement from '../heplers/index.js' to '../helpers/index.js' to fix import error
🐛 fix(sessionService.js): change import statement from '../heplers/index.js' to '../helpers/index.js' to fix import error
🐛 fix(smsOtpService.js): change import statement from '../heplers/index.js' to '../helpers/index.js' to fix import error
🐛 fix(userService.js): change import statement from

* 🐛 fix(server.ts): change port variable case from lowercase port to uppercase PORT to improve semantics
✨ feat(server.ts): add support for process.env.PORT environment variable to be able to run app on a configurable port
🐛 fix(config.d.ts): rename Configuration class to Config to improve naming consistency
🐛 fix(config.js): rename Configuration class to Config to improve naming consistency
🐛 fix(config.js.map): update sourceMappingURL after renaming Configuration class to Config
🐛 fix(assert.d.ts): fix import path for ErrorRsp in assert.d.ts
🐛 fix(assert.js): fix import path for BaseError and httpStatusCodes in assert.js
🐛 fix(assert.js.map): update sourceMappingURL after fixing import path in assert.js
🐛 fix(helpers.d.ts): fix import path for ErrorRsp in helpers.d.ts
🐛 fix(helpers.js): fix import path for BaseError and httpStatusCodes in helpers.js
🐛 fix(helpers.js.map): update sourceMappingURL after fixing import path in helpers.js
🐛 fix(index.d.ts): fix import path for Config in index.d.ts
🐛 fix(index.js): fix import path for Config in index.js
🐛 fix(index.js.map): update sourceMappingURL after fixing import path in index.js
🐛 fix(sdk.d.ts): fix import path for Config in sdk.d.ts
🐛 fix(sdk.js.map): update sourceMappingURL after fixing import path in sdk.js
🐛 fix(authTokenService.js): fix import path for isErrorRsp in authTokenService.js
🐛 fix(emailLinkService.js): fix import path for isErrorRsp in emailLinkService.js
🐛 fix(emailOtpService.js): fix import path for isErrorRsp and Helper in emailOtpService.js
🐛 fix(sessionService.js): fix import path for Assert in sessionService.js
🐛 fix(smsOtpService.js): fix import path for isErrorRsp and Helper in smsOtpService.js
🐛 fix(userService.js): fix import path for isErrorRsp, Helper, and User in userService.js
🐛 fix(validationService.js): fix import path for isErrorRsp, Helper, and Validation in validationService.js
🐛 fix(webhook.js): fix import path for BaseError, httpStatusCodes, Assert, and Helper in webhook.js

* 🐛 fix(webhook.ts): fix typo in import statement to correctly import Helper module

The import statement for the Helper module was misspelled as "heplers" instead of "helpers". This commit fixes the typo to correctly import the Helper module.

* 🔧 chore(README.md): update Corbado SDK badges and fix class names in code examples
🔧 chore(jest.config.js): update Jest configuration to collect coverage and generate reports
🔧 chore(makefile): update test command to include coverage report generation
🔧 chore(tsconfig.eslint.json): update ESLint configuration to include jest.config.js in linting

* 🔥 chore(build.yml): remove outdated build workflow file

✨ feat(PULL_REQUEST_TEMPLATE.md): add pull request template to provide a standardized format for pull requests

✨ feat(build.yml): add new build workflow file with updated steps and configurations for building and testing the project

* 🔧 chore(build.yml): update workflow to include steps for verifying version, installing dependencies, building, and running tests

* 🔧 chore(build.yml): update Node.js version to 14 for compatibility and performance improvements
🔒 chore(build.yml): update cache action to use actions/cache@v2 for better caching performance
🔍 chore(build.yml): add step to audit dependencies for security vulnerabilities
📦 chore(build.yml): update npm install command to use npm ci for deterministic builds
🚀 chore(build.yml): add step to build project if present
🧪 chore(build.yml): add step to run tests with environment variables PROJECT_ID and API_SECRET

* 🔧 chore(build.yml): update actions/cache version from v2 to v3 for better performance and compatibility

* 🔧 chore(.gitignore): remove package-lock.json from gitignore to include it in version control
🔒 chore(package.json): update axios dependency to version 1.6.5 for bug fixes and improvements

* 🔍 chore(build.yml): remove npm audit step from the build workflow to improve build time and reduce noise in the workflow logs
🔧 chore(build.yml): remove npm audit step from the build workflow as it is not necessary for the build process

* 🐛 Re-init package-lock.json

* 🔧 chore(build.yml): update cache-npm step to improve caching efficiency and add npm list step for debugging purposes
🚀 ci(build.yml): add npm list step to list the state of node modules when cache is not hit to aid in debugging

* 🔧 chore(.gitignore): remove unnecessary lib/ and tests/setupJest.js from gitignore
📦 chore(package.json): update dotenv package version to 16.3.1
🔧 chore(tests/integration/emailLink.test.ts): add try-catch block to handle errors during email send
🔧 chore(tests/integration/emailOtp.test.ts): add try-catch block to handle errors during email send
🔧 chore(tests/setupJest.js): fix typo in module.exports statement

* 🔧 chore(jest.config.js): update Jest transform configuration to use 'tsconfig.esm.json' for transforming TypeScript files with ES modules support
🐛 fix(tests): update error assertions in integration and unit tests to correctly access httpStatusCode property of ServerError and statusCode property of BaseError
🔧 chore(tsconfig.esm.json): enable 'esModuleInterop' compiler option to allow interoperability between CommonJS and ES modules

* 🔧 chore(build.yml): update Node.js version to 18 in the setup step to use the latest version
🔒 chore(build.yml): update environment variable names for secrets to match the new naming convention for better security

* 🔧 chore(tsconfig.base.json): enable 'forceConsistentCasingInFileNames' option to enforce consistent casing in file names

* 🐛 fix(user.ts): remove unnecessary file extension from import statements
🐛 fix(index.ts): remove unnecessary file extension from import statements
🐛 fix(server.ts): remove unnecessary file extension from import statements
🐛 fix(errors/index.ts): remove unnecessary file extension from import statements
🐛 fix(errors/serverError.ts): remove unnecessary file extension from import statements
🐛 fix(helpers/assert.ts): remove unnecessary file extension from import statements
🐛 fix(helpers/helpers.ts): remove unnecessary file extension from import statements
🐛 fix(helpers/index.ts): remove unnecessary file extension from import statements
🐛 fix(sdk.ts): remove unnecessary file extension from import statements
🐛 fix(services/authTokenService.ts): remove unnecessary file extension from import statements
🐛 fix(services/emailLinkService.ts): remove unnecessary file extension from import statements
🐛 fix(services/emailOtpService.ts): remove unnecessary file extension from import statements
🐛 fix(services/index.ts): remove unnecessary file extension from import statements
🐛 fix(services/sessionService.ts): remove unnecessary file extension from import statements
🐛 fix(services/smsOTPService.ts): remove unnecessary file extension from import statements
🐛 fix(services/userService.ts): remove unnecessary file extension from import statements
🐛 fix(services/validationService.ts): remove unnecessary file extension from import statements
🐛 fix(webhook/entities/authMethodsRequest.ts): remove unnecessary file extension from import statements
🐛 fix(webhook/entities/authMethodsResponse.ts): remove unnecessary file extension from import statements
🐛 fix(webhook/entities/index.ts): remove unnecessary file extension from import statements
🐛 fix(webhook/entities/passwordVerifyRequest.ts): remove unnecessary file extension from import statements
🐛 fix(webhook/entities/passwordVerifyResponse.ts): remove unnecessary file extension from import statements
🐛 fix(webhook/webhook.ts): remove unnecessary file extension from import statements
🐛 fix(tests/unit/config.test.ts): remove unnecessary file extension from import statements
🐛 fix(tests/unit/entities/user.test.ts): remove unnecessary file extension from import statements
🐛 fix(tests/unit/sdk.test.ts): remove unnecessary file extension from import statements

* 🐛 fix(server.ts): change port variable case from lowercase port to uppercase PORT to improve semantics
✨ feat(server.ts): add support for process.env.PORT environment variable to be able to run app on a configurable port
🐛 fix(serverError.d.ts): remove export keyword from ServerError class declaration to fix import issues
🐛 fix(serverError.js): remove export keyword from ServerError class declaration to fix import issues
🐛 fix(serverError.js.map): remove export keyword from ServerError class declaration to fix import issues
🐛 fix(serverError.js.map): remove export keyword from ServerError class declaration to fix import issues
🐛 fix(serverError.js.map): remove export keyword from ServerError class declaration to fix import issues
🐛 fix(serverError.js.map): remove export keyword from ServerError class declaration to fix import issues
🐛 fix(serverError.js.map): remove export keyword from ServerError class declaration to fix import issues
🐛 fix(serverError.js.map): remove export keyword from ServerError class declaration to fix import issues
🐛 fix(serverError.js.map): remove export keyword from ServerError class declaration to fix import issues
🐛 fix(serverError.js.map): remove export keyword from ServerError class declaration to fix import issues
🐛 fix(serverError.js.map): remove export keyword from ServerError class declaration to fix import issues
🐛 fix(serverError.js.map): remove export keyword from ServerError class declaration to fix import issues
🐛 fix(serverError.js.map): remove export keyword from ServerError class declaration to fix import issues
🐛 fix(serverError.js.map): remove export keyword from ServerError class declaration to fix import issues
🐛 fix(serverError.js.map): remove export keyword from ServerError class declaration to fix import issues
🐛 fix(serverError.js.map): remove export keyword from ServerError class declaration to fix import issues
🐛 fix(serverError.js.map): remove export keyword from ServerError class declaration to fix import issues
🐛 fix(serverError.js.map): remove export keyword from ServerError class declaration to fix import issues
🐛 fix(serverError.js.map): remove export keyword from ServerError class declaration to fix import issues
🐛 fix(serverError.js.map): remove export keyword from ServerError class declaration to fix import issues
🐛 fix(serverError.js.map): remove export keyword from ServerError

* 🔥 refactor(smsOTPService.ts): remove SmsOTP service file

The SmsOTP service file has been removed as it is no longer needed in the project. This file contained the implementation of the SmsOTPInterface, which provided methods for sending and validating SMS OTP codes. However, this functionality is no longer required and has been refactored out of the project.

* ✨ feat(smsOtpService.ts): add SmsOTP service to handle sending and validating SMS OTP codes

The `SmsOTP` service is responsible for sending and validating SMS OTP codes. It implements the `SmsOTPInterface` interface and provides two methods: `send` and `validate`.

The `send` method sends an SMS OTP code using the provided request object. It handles any errors that occur during the send operation and throws a `BaseError` if an error response is received.

The `validate` method validates an SMS OTP code using the provided ID and request object. It handles any errors that occur during the validation operation and throws a `BaseError` if an error response is received.

This service is designed to work with an Axios instance and requires it to be passed in during initialization.

* 🚀 chore(build.yml): add Codecov and jest-badge-deploy-action steps to the build workflow for better test coverage reporting and publishing
🔧 chore(README.md): update coverage badges to use the new Codecov badges and remove outdated badges
🔧 chore(jest.config.js): add 'json-summary' to the coverageReporters array to generate summary coverage report in JSON format

* Renamed environment variables

* 🐛 fix(helpers.ts): remove unnecessary comment in convertToServerError method
🐛 fix(sdk.ts): remove unnecessary comment in createClient method
🔥 chore(setupJest.template.js): remove unnecessary setupJest template file
🔥 chore(utils.ts): remove unnecessary import and method in Utils class

* 🔧 chore(api.ts): add support for additional frontend frameworks (Angular, Svelte, Next.js, Nuxt.js, Flutter) and backend language (Other)
🔧 chore(backend_api_public.yml): update enum values for frontend frameworks and backend language to include new options (Angular, Svelte, Next.js, Nuxt.js, Flutter, Other)

* 🔧 fix(config.ts): update DefaultBackendAPI to use CORBADO_BACKEND_API environment variable if available, otherwise use an empty string
🔧 fix(config.ts): update DefaultFrontendAPI to use a placeholder [projectID] instead of process.env.PROJECT_ID
🔧 fix(helpers.ts): update error object in createServerErrorFromNodeError method to use error name and message dynamically

* 📝 docs(todo.md): update todo list

- Mark API Documentation task as incomplete
- Mark Linting task as incomplete
- Mark Contributing task as incomplete

* 📝 docs: add example environment file with placeholders for project ID, API secret, and backend API URL
🔥 chore(makefile): remove unnecessary clean targets for dist and cjs-test directories

* 🚀 feat(docs): add documentation for build process

📝 docs/build.md: Provide an overview of the build process in the Corbado Node.js SDK. Explain the key targets in the Makefile and their purposes. Also, describe how to build the project and provide some notes on extensions in imports and directory imports.

🚀 feat(docs): add documentation for configuration

📝 docs/configuration.md: Explain how to create a configuration object in the Corbado Node.js SDK and provide details on the validation and properties of the Config class.

🚀 feat(docs): add documentation for project overview

📝 docs/overview.md: Provide an overview of the Corbado Node.js SDK, including its key features, installation instructions, and a brief introduction to the project.

🚀 feat(docs): add documentation for setup instructions

📝 docs/setup.md: Provide step-by-step instructions on how to set up the Corbado Node.js SDK, including cloning the repository, navigating to the project directory, and installing dependencies.

🚀 feat(docs): add documentation for code structure

📝 docs/structure.md: Explain the directory structure of the Corbado Node.js SDK, including the purpose of each directory and file.

🚀 feat(docs): add documentation for testing framework

📝 docs/tests.md: Provide an overview of the testing framework used in the Corbado Node.js SDK, including information on the test structure, running tests, and an example test suite.

* 🔥 Remove cjs from module tracking

* 🔥 Remove esm from module tracking

* 🔧 chore(.gitignore): add cjs/ and esm/ directories to .gitignore to exclude them from version control

🔧 chore(index.ts): remove unnecessary line breaks and whitespace to improve code readability

🔧 chore(sdk.ts): remove commented out code to improve code cleanliness

🔧 chore(config.test.ts): remove unnecessary line breaks and whitespace to improve code readability

🔧 chore(sdk.test.ts): remove unnecessary line breaks and whitespace to improve code readability

🔧 chore(utils.ts): remove unnecessary line breaks and whitespace to improve code readability

* 🔧 fix(emailOtp.test.ts): use a random test email instead of a hardcoded email to improve test reliability and maintainability

* 🐛 fix(user.test.ts): fix typo in function name from getusers() to getUsers() for consistency and readability

* 🔄 refactor(sdk.ts): rename getUsers method to users for better readability and consistency

🔄 refactor(utils.ts): update method call from getUsers().create to users().create for consistency with SDK class changes

* ✨ feat(config.test.ts): add integration tests for the Configuration class to ensure proper instantiation and validation of project ID and API secret

✨ feat(sdk.test.ts): add integration tests for the SDK class to ensure proper instantiation and creation of various service objects

✨ feat(authToken.test.ts): add unit test for the AuthToken class to validate a valid auth token

✨ feat(emailLink.test.ts): add unit test for the EmailLink class to send an email link and validate it

✨ feat(emailOtp.test.ts): add unit test for the EmailOtp class to send an email code and validate it

✨ feat(smsOtp.test.ts): add unit test for the SmsOtp class to send an SMS code and validate it

✨ feat(user.test.ts): add unit test for the User class to create a user and retrieve user information

✨ feat(validation.test.ts): add unit test for the Validation class to validate email and phone number

* 🐛 fix(authToken.test.ts): remove unnecessary import of axios from authToken.test.ts
🐛 fix(emailLink.test.ts): remove unnecessary import of axios from emailLink.test.ts
🐛 fix(emailOtp.test.ts): remove unnecessary import of axios from emailOtp.test.ts
🐛 fix(smsOtp.test.ts): remove unnecessary import of axios from smsOtp.test.ts
🐛 fix(user.test.ts): remove unnecessary import of axios from user.test.ts
🐛 fix(utils.ts): add missing import of AxiosMockAdapter in utils.ts
✨ feat(utils.ts): add utility functions MockAxiosInstance and restoreMock to simplify mocking axios in tests

* 🐛 fix(httpStatusCodes.ts): add new error code for invalid URL
🐛 fix(assert.ts): add validation for URL in the assert class to ensure it is valid and meets certain criteria

* 🔧 chore(config.ts): update DefaultBackendAPI to use a hardcoded value instead of relying on environment variable for consistency and reliability
✨ feat(config.ts): add methods setFrontendAPI and setBackendAPI to allow dynamic configuration of frontend and backend APIs

* 🔧 chore(server.ts): change port variable case from lowercase port to uppercase PORT to improve semantics
✨ feat(server.ts): add support for process.env.PORT environment variable to be able to run app on a configurable port
📦 deps(package.json): add memory-cache-node package as a dependency
🔧 chore(config.ts): import MemoryCache from memory-cache-node and axios from axios
🔧 chore(config.ts): add JWKSCache property to ConfigInterface and set it to DefaultJwksCache
🔧 chore(config.ts): add DefaultClient property to ConfigInterface and set it to DefaultClient
🔧 chore(config.ts): add setHttpClient method to Config class to set the http client
🔧 chore(config.ts): add setJwksCache method to Config class to set the JWKS cache
🔧 chore(config.ts): add setShortSessionCookieName method to Config class to set the short session cookie name
🔧 chore(index.ts): remove export of DefaultBackendAPI, DefaultFrontendAPI, DefaultShortSessionCookieName, and DefaultCacheMaxAge
🔧 chore(index.ts): remove import of DefaultBackendAPI, DefaultFrontendAPI, DefaultShortSessionCookieName, and DefaultCacheMaxAge
🔧 chore(index.ts): remove export of DefaultBackendAPI, DefaultFrontendAPI, DefaultShortSessionCookieName, and DefaultCacheMaxAge
🔧 chore(index.ts): remove import of DefaultBackendAPI, DefaultFrontendAPI, DefaultShortSessionCookieName, and DefaultCacheMaxAge
🔧 chore(sdk.ts): add jwksCache parameter to Session constructor and assign it to this.JWKSCache
🔧 chore(sdk.ts): add axiosClient parameter to Session constructor and assign it to this.Client
🔧 chore(sessionService.ts): add client parameter to Session constructor and assign it to this.client
🔧 chore(sessionService.ts): add jwksCache parameter to Session constructor and assign it to this.jwksCache
🔧 chore(sessionService.ts): add cacheMaxAge parameter to Session constructor and assign it to this.cacheMaxAge
🔧 chore(sessionService.ts): add jwksCache property to Session class and assign it to DefaultJwksCache
🔧 chore(sessionService.ts): add cacheMaxAge property to Session class and assign it to DefaultCacheMaxAge
🔧 chore(session

* 🔧 chore(README.md): update Node SDK reference and examples to reflect the correct SDK usage and services

🔧 chore(sdk.ts): rename emailOtp() method to emailOtps() for consistency with other service methods

🔧 chore(emailOtp.test.ts): update method calls from sdk.emailOtp() to sdk.emailOtps()

🔧 chore(smsOtp.test.ts): update method calls from sdk.smsOtp() to sdk.smsOtps()

🔧 chore(config.test.ts): add unit tests for the Config class to ensure proper instantiation and default values

🔧 chore(sdk.test.ts): add unit tests for the SDK class to ensure proper instantiation and creation of service objects

* 🔧 chore(package.json): add "build" script to run the "make build" command

* Session access fix

* Removed jwsk cache, harmonized config with PHP

* Updated README.md

* More bug fixes

* 🔨 refactor(sessionService.ts): remove unused RequestWithCookies interface and related methods
🔒 chore(sessionService.ts): change validateShortSessionValue and getCurrentUser methods to accept token as a string parameter instead of RequestWithCookies object

* 🔧 chore(package.json): remove unused dependency 'memory-cache-node' to optimize package size and improve maintainability
🔧 chore(config.ts): remove unused import of 'memory-cache-node' to improve code readability and maintainability
🔧 chore(sdk.ts): update jwks URI concatenation to use template literals for better code readability
🔧 chore(sessionService.ts): remove unused import of 'memory-cache-node' to improve code readability and maintainability
🔧 chore(config.test.ts): remove unused import of 'DefaultJwksCache' to improve code readability and maintainability
🔧 chore(session.test.ts): remove unused import of 'MemoryCache' to improve code readability and maintainability

* Minor changes

* Fixed JWKS cache

* Fixed integration test

* Added environment check

* 🐛 fix(config.ts): fix import statement for Assert module to include file extension to avoid potential import errors

* 🔧 chore(config.test.ts): add unit tests for the Configuration class to ensure correct instantiation and default values
🔧 chore(sdk.test.ts): add unit tests for the SDK class to ensure correct instantiation and creation of various objects
🔧 chore(user.test.ts): remove unit tests for the User entity as they are no longer needed
🔧 chore(authToken.test.ts): remove unit tests for the AuthToken service as they are no longer needed
🔧 chore(emailLink.test.ts): remove unit tests for the EmailLink service as they are no longer needed
🔧 chore(emailOtp.test.ts): remove unit tests for the EmailOTP service as they are no longer needed
🔧 chore(session.test.ts): remove unit tests for the Session service as they are no longer needed
🔧 chore(smsOtp.test.ts): remove unit tests for the SmsOTP service as they are no longer needed
🔧 chore(user.test.ts): remove unit tests for the User service as they are no longer needed

* ✨ feat(sessionExample.ts): add example code for handling session cookies and authentication

This commit adds a new file `sessionExample.ts` which contains example code for handling session cookies and authentication in an Express.js application. The code sets up an Express server, configures cookie parsing, and initializes the Corbado SDK with the necessary credentials.

The code includes three routes:
- The root route ("/") simply returns a "Hello world!" message.
- The "/setCookie" route sets a short session cookie with a predefined value. The cookie is set with a maximum age of 15 minutes and is marked as HTTP-only.
- The "/logged-in" route demonstrates how to handle authentication using the Corbado SDK. It retrieves the short session cookie from the request, verifies the user's authentication status, and displays user information if authenticated. If the user is not authenticated, it redirects to a login page.

The server is set to listen on port 8080.

This code serves as an example for developers who want to implement session cookies and authentication using the Corbado SDK in their Express.js applications.

---------

Co-authored-by: Tobias Albrecht <tobias.albrecht@albrecht.services>
Co-authored-by: Sam <sam.odum@corbado.com>
  • Loading branch information
3 people committed Jan 19, 2024
1 parent 87c75ee commit 4c00f5b
Show file tree
Hide file tree
Showing 122 changed files with 34,822 additions and 3,401 deletions.
4 changes: 0 additions & 4 deletions .babelrc

This file was deleted.

12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = true
42 changes: 42 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module",
"project": "./tsconfig.eslint.json"
},
"env": { "jest": true },
"extends": [
"airbnb-base",
"airbnb-typescript/base",
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"plugin:prettier/recommended"
],
"ignorePatterns": ["lib/**/*.js"],
"rules": {
"@typescript-eslint/restrict-template-expressions": 0,
"max-classes-per-file": ["error", { "ignoreExpressions": true }],
"class-methods-use-this": ["error", { "exceptMethods": ["createAnonymousUser", "#createClient"] }],
"object-curly-newline": [
"error",
{
"ObjectExpression": {
"multiline": true,
"minProperties": 5
},
"ObjectPattern": {
"multiline": true
},
"ImportDeclaration": {
"multiline": true
},
"ExportDeclaration": {
"multiline": true
}
}
]
}
}
3 changes: 3 additions & 0 deletions .example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CORBADO_PROJECT_ID=pro-xxxxxxxxxxxxxxxxxxx
CORBADO_API_SECRET=corbado1_xxxxxxxxxxxxxxxxxxxxxxxxxx
CORBADO_BACKEND_API=https://backendapi.corbado.io
26 changes: 0 additions & 26 deletions .github/workflow/build.yml

This file was deleted.

38 changes: 38 additions & 0 deletions .github/workflows/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context. List any dependencies that are required for this change.

Fixes # (issue)

## Type of change

Please delete options that are not relevant.

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] This change requires a documentation update

# How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

- [ ] Test A
- [ ] Test B

**Test Configuration**:
* Firmware version:
* Hardware:
* Toolchain:
* SDK:

# Checklist:

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream modules
56 changes: 56 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Build workflow

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18

- name: Cache node modules
id: cache-npm
uses: actions/cache@v3
env:
cache-name: cache-node-modules
with:
# npm cache files are stored in `~/.npm` on Linux/macOS
path: ~/.npm
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- if: ${{ steps.cache-npm.outputs.cache-hit != 'true' }}
name: List the state of node modules
continue-on-error: true
run: npm list

- name: Install dependencies
run: npm install

- name: Build project
run: npm run build --if-present

- name: Run tests
run: npm test
env:
CORBADO_PROJECT_ID: ${{ secrets.CORBADO_PROJECT_ID }}
CORBADO_API_SECRET: ${{ secrets.CORBADO_API_SECRET }}
CORBADO_BACKEND_API: ${{ secrets.CORBADO_BACKEND_API }}

- name: Upload coverage report to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

- name: Upload jest report And Publish
uses: nolleh/jest-badge-deploy-action@latest
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,6 @@ typings/
.tern-port

.idea/

cjs/
esm/
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"semi": true,
"trailingComma": "all",
"singleQuote": true,
"printWidth": 120,
"tabWidth": 2
}
12 changes: 12 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"recommendations": [
// JS Language plugins (~required)
"dbaeumer.vscode-eslint",
"mgmcdermott.vscode-language-babel",
"esbenp.prettier-vscode",

// Highly recommended
"jgclark.vscode-todo-highlight",
"EditorConfig.editorconfig"
]
}
12 changes: 12 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
// Show the path in the top window bar.
"window.title": "${rootName}${separator}${activeEditorMedium}",

// Formatting
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": ["source.fixAll.format", "source.fixAll.eslint", "source.removeUnusedImports"],

// Extension settings
"npm.packageManager": "npm"
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Corbado GmbH
Copyright (c) 2024 Corbado GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
63 changes: 63 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
GEN_DIR := ./.gen
TARGET_DIR := src/generated
OPENAPI_SPEC_URL := https://api.corbado.com/docs/api/openapi/backend_api_public.yml
OPENAPI_IMAGE := openapitools/openapi-generator-cli
SOURCE_FILES := $(shell find src/ -type f -name '*.ts')

.PHONY:all
all: build

.PHONY:build
build: cjs/build esm/build

.PHONY:lint
lint:
npx eslint --quiet 'src/**/*.ts' 'tests/**/*.ts'

.PHONY:lint-fix
lint-fix: fix

.PHONY:fix
fix:
npx eslint --quiet 'src/**/*.ts' 'tests/**/*.ts' --fix

.PHONY:watch
watch:
npx tsc --watch

.PHONY:start
start: build

.PHONY: openapi_generate
openapi_generate:
@echo "Generating OpenAPI TypeScript Axios code..."
@mkdir -p $(GEN_DIR) $(TARGET_DIR)
@curl -s -o $(GEN_DIR)/backend_api_public.yml $(OPENAPI_SPEC_URL)
@docker pull openapitools/openapi-generator-cli
@docker run -v $(GEN_DIR):/local openapitools/openapi-generator-cli generate -g typescript-axios -i /local/backend_api_public.yml -o /local --additional-properties=invokerPackage=Corbado\\Generated
@cp -r $(GEN_DIR)/* $(TARGET_DIR)
@rm -rf $(GEN_DIR)
@echo "OpenAPI TypeScript Axios code generation done!"


.PHONY:clean
clean:
rm -rf esm cjs $(TARGET_DIR)

.PHONY: test
test:
@npx jest --coverage

.PHONY: cjs/build
cjs/build: $(SOURCE_FILES)
npx tsc -p tsconfig.cjs.json
echo '{"type": "commonjs"}' > cjs/package.json
@# Creating a small file to keep track of the last build time
touch cjs/build

.PHONY: esm/build
esm/build: $(SOURCE_FILES)
npx tsc -p tsconfig.esm.json
echo '{"type": "module"}' > esm/package.json
@# Creating a small file to keep track of the last build time
touch esm/build
Loading

0 comments on commit 4c00f5b

Please sign in to comment.