-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
21 changed files
with
4,917 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Changesets | ||
|
||
Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works | ||
with multi-package repos, or single-package repos to help you version and publish your code. You can | ||
find the full documentation for it [in our repository](https://github.com/changesets/changesets) | ||
|
||
We have a quick list of common questions to get you started engaging with this project in | ||
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md) |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json", | ||
"changelog": ["@changesets/changelog-github", { "repo": "maticzav/sessions" }], | ||
"commit": false, | ||
"fixed": [], | ||
"linked": [], | ||
"access": "public", | ||
"baseBranch": "main", | ||
"updateInternalDependencies": "patch", | ||
"bumpVersionsWithWorkspaceProtocolOnly": true, | ||
"ignore": [] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'authsessions': patch | ||
--- | ||
|
||
create initial staging version of the sessions package |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
name: Create Release and Publish Packages | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
|
||
concurrency: ${{ github.workflow }}-${{ github.ref }} | ||
|
||
jobs: | ||
publish: | ||
name: Publish Packages | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout Main | ||
uses: actions/checkout@v3 | ||
|
||
- uses: pnpm/action-setup@v2 | ||
with: | ||
version: 8.1.0 | ||
|
||
- name: Setup Node Environment | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version-file: '.nvmrc' | ||
cache: 'pnpm' | ||
|
||
- name: Install Dependencies | ||
run: pnpm i --frozen-lockfile | ||
|
||
- name: Create Release Pull Request or Publish to npm | ||
id: changesets | ||
uses: changesets/action@v1 | ||
with: | ||
version: pnpm run version | ||
publish: pnpm run release | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
NPM_TOKEN: ${{ secrets.NPM_TOKEN }} |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
name: Test Package | ||
|
||
on: | ||
push: | ||
branches-ignore: | ||
- main | ||
pull_request: | ||
branches-ignore: | ||
- main | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.ref }} | ||
|
||
jobs: | ||
test: | ||
name: Test Sessions Package | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout Main | ||
uses: actions/checkout@v3 | ||
|
||
- uses: pnpm/action-setup@v2 | ||
with: | ||
version: 8.1.0 | ||
|
||
- name: Setup Node Environment | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version-file: '.nvmrc' | ||
cache: 'pnpm' | ||
|
||
- name: Install Workspace Dependencies | ||
run: pnpm i --frozen-lockfile | ||
|
||
- name: Build Packages | ||
run: pnpm run build | ||
|
||
- name: Test Packages and Template | ||
run: pnpm run test |
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 |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
lerna-debug.log* | ||
|
||
# Diagnostic reports (https://nodejs.org/api/report.html) | ||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
*.lcov | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
bower_components | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (https://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directories | ||
node_modules/ | ||
jspm_packages/ | ||
|
||
# TypeScript v1 declaration files | ||
typings/ | ||
|
||
# TypeScript cache | ||
*.tsbuildinfo | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional eslint cache | ||
.eslintcache | ||
|
||
# Microbundle cache | ||
.rpt2_cache/ | ||
.rts2_cache_cjs/ | ||
.rts2_cache_es/ | ||
.rts2_cache_umd/ | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
# Yarn Integrity file | ||
.yarn-integrity | ||
|
||
# dotenv environment variables file | ||
.env | ||
.env.test | ||
|
||
# parcel-bundler cache (https://parceljs.org/) | ||
.cache | ||
|
||
# Next.js build output | ||
.next | ||
|
||
# Nuxt.js build / generate output | ||
.nuxt | ||
dist | ||
|
||
# Gatsby files | ||
.cache/ | ||
# Comment in the public line in if your project uses Gatsby and *not* Next.js | ||
# https://nextjs.org/blog/next-9-1#public-directory-support | ||
# public | ||
|
||
# vuepress build output | ||
.vuepress/dist | ||
|
||
# Serverless directories | ||
.serverless/ | ||
|
||
# FuseBox cache | ||
.fusebox/ | ||
|
||
# DynamoDB Local files | ||
.dynamodb/ | ||
|
||
# TernJS port file | ||
.tern-port | ||
|
||
.DS_Store |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
v16.20.0 |
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,91 @@ | ||
# sessions | ||
Manage NodeJS server sessions easily in-memory or in Redis. | ||
# Sessions | ||
|
||
> Sessions contains a set of different session management classes. | ||
Each sessions variant implements a general `ISessions` interface and may be swapped in as a replacement for any existing sessions provider. | ||
|
||
## Quick Start | ||
|
||
> NOTE: Examples below use `trpc` and `fastify`, but the API is completely framework agnostic and should be easy to port to any other server framework. | ||
````ts | ||
|
||
type SessionMeta = { | ||
// anything that's serializable | ||
} | ||
|
||
// InMemorySessions (best for local development). | ||
const sessions = new InMemorySessions<string, SessionMeta>() | ||
|
||
import { RedisClientType, createClient } from 'redis' | ||
|
||
// RedisSessions (best for production environment). | ||
const redis: RedisClientType = createClient({ | ||
socket: { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
host: process.env.REDIS_HOST!, | ||
port: 6379, | ||
}, | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
password: process.env.REDIS_PASSWORD!, | ||
}) | ||
|
||
const sessions = new RedisSessions<string, SessionMeta>({ redis }) | ||
|
||
|
||
export type Context = { | ||
/** | ||
* The session ID of the authenticated user if there exists one. | ||
*/ | ||
session: SessionId | null | ||
|
||
/** | ||
* Access to session related services. | ||
*/ | ||
sessions: ISessions<string, SessionMeta> | ||
} | ||
|
||
|
||
// Create a context with a shared sessions instance. | ||
await server.register(fastifyTRPCPlugin, { | ||
prefix: '/trpc', | ||
logLevel: 'debug', | ||
trpcOptions: { | ||
router: root, | ||
createContext: ({ req, res }: CreateFastifyContextOptions): Context => { | ||
const session = SessionUtils.getSessionIdFromAuthToken(req.headers.authorization) | ||
|
||
return { sessions, session } | ||
}, | ||
}, | ||
}) | ||
|
||
// Getting session from request header. | ||
const session = SessionUtils.getSessionIdFromAuthToken(req.headers.authorization) | ||
|
||
const userId = await ctx.sessions.getUserIdFromSession(session) | ||
const meta = await ctx.sessions.getSessionMeta(session) | ||
|
||
// Creating sessions and getting tokens. | ||
const session = await ctx.sessions.createSession({ | ||
userId: user.id, | ||
label: input.label, | ||
meta: {}, | ||
}) | ||
const token = SessionUtils.getAuthTokenForSessionId(session) | ||
|
||
// Destroying sesssions. | ||
await ctx.sessions.destroySession(ctx.session) | ||
|
||
// Listing sessions of a given user. | ||
const userId = await ctx.sessions.getUserIdFromSession(session) | ||
const allUserSessions = await ctx.sessions.getSessionsForUser(userId) | ||
|
||
// Listing all sessions in the system. | ||
const allExistingSessions = await sessions.listSessions() | ||
``` | ||
|
||
### License | ||
|
||
MIT @ Matic Zavadlal | ||
```` |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { Config } from 'jest' | ||
|
||
const config: Config = { | ||
verbose: true, | ||
testEnvironment: 'node', | ||
transform: { | ||
'^.+\\.tsx?$': 'ts-jest', | ||
}, | ||
testRegex: '(/tests/.*|(\\.|/)test)\\.tsx?$', | ||
testPathIgnorePatterns: ['/node_modules/', '/__fixtures__/', '/dist/'], | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], | ||
} | ||
|
||
export default config |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "authsessions", | ||
"version": "0.0.1", | ||
"scripts": { | ||
"track": "changeset add", | ||
"version": "changeset version", | ||
"prerelease": "pnpm build", | ||
"release": "pnpm publish -r --access=public", | ||
"build": "tsc", | ||
"test:types": "tsc --noEmit", | ||
"test:lib": "jest" | ||
}, | ||
"dependencies": { | ||
"ioredis": "^5.3.1", | ||
"luxon": "^3.2.1", | ||
"redis": "^4.6.5", | ||
"uuid": "^9.0.0" | ||
}, | ||
"devDependencies": { | ||
"@changesets/changelog-github": "^0.4.8", | ||
"@changesets/cli": "^2.26.1", | ||
"@types/jest": "28.1.8", | ||
"@types/luxon": "^3.2.0", | ||
"@types/node": "18.11.9", | ||
"@types/uuid": "^9.0.1", | ||
"jest": "29.3.1", | ||
"prettier": "^2.8.7", | ||
"ts-jest": "29.0.3", | ||
"ts-node": "^10.9.1", | ||
"ts-node-dev": "^2.0.0", | ||
"typescript": "^5.1.3" | ||
} | ||
} |
Oops, something went wrong.