-
-
Notifications
You must be signed in to change notification settings - Fork 5
feat: auth package #433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Vicentesan
wants to merge
34
commits into
main
Choose a base branch
from
auth-package
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
feat: auth package #433
Changes from 33 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
44b743a
docs: add goals/instructions for auth package
Vicentesan 5fd1707
Merge branch 'main' into feat/auth-package
Vicentesan 4e949ec
Merge branch 'main' into feat/auth-package
Vicentesan 5a5fa6b
feat: add base domain file
Vicentesan b993399
setup initial project for vini
nicolasmelo1 e0926bf
feat: add auth package with password adapter example
Vicentesan 6d4b2f6
fix typings
nicolasmelo1 f6f3b34
fix typings
nicolasmelo1 e0f4a93
fix linting
Vicentesan 46ccd1d
fix: test example
brenoliradev f181ff7
chore(deps): bump drizzle-kit from 0.30.4 to 0.30.5 (#447)
dependabot[bot] b34a3b1
fix: serverless handler
brenoliradev 90a7059
fix: serverless handler
brenoliradev 6b9fe06
fix: include customServerInstance
brenoliradev 12724af
feat: implement method 'new' to ExpressServer
brenoliradev 45d12dc
changeset
brenoliradev d08dd15
feat: constructor method
brenoliradev 79aa81e
docs: add goals/instructions for auth package
Vicentesan 091867d
setup initial project for vini
nicolasmelo1 0e11b58
feat: add auth package with password adapter example
Vicentesan 51f8b31
fix merge errors
Vicentesan de247e5
chore: update export/import sort
Vicentesan 0f4ab1b
feat: add login route in example
Vicentesan dfc5b07
feat: add jwt adapter
Vicentesan b54b8c1
chore: update jwt-adapter docs
Vicentesan 6d90b91
feat: add jwt adapter to with-auth example
Vicentesan d4fe3ff
chore: remove old JWT package
Vicentesan 577e9ea
feat: add jwt adapter using Jose as inspiration
Vicentesan 4f35c7c
chore: update readme
Vicentesan 6e0743c
chore: update linting
Vicentesan b430c40
resolve merge conflicts
nicolasmelo1 6101cdc
feat: add salt rounds config to password adapter
Vicentesan 4383799
feat: jwt adapter using classes
Vicentesan f3d1b28
update examples/with-auth/src/core/auth.ts
Vicentesan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,6 @@ | ||
| --- | ||
| '@palmares/express-adapter': patch | ||
| '@palmares/server': patch | ||
| --- | ||
|
|
||
| "new" method for creating express servers |
This file contains hidden or 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 @@ | ||
| --- | ||
| '@palmares/server': patch | ||
| --- | ||
|
|
||
| getAllRouters passing options at incorrect position |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,19 @@ | ||
| name: with-auth | ||
|
|
||
| services: | ||
| db: | ||
| container_name: with-auth_db | ||
| image: bitnami/postgresql:latest | ||
| environment: | ||
| POSTGRES_USER: postgres | ||
| POSTGRES_PASSWORD: postgres | ||
| POSTGRES_DB: POSTGRES_DB | ||
| POSTGRESQL_REPLICATION_USE_PASSFILE: "no" | ||
| volumes: | ||
| - postgres_data:/var/lib/postgresql/data | ||
| - ./priv/init-db.sh:/docker-entrypoint-initdb.d/init-db.sh | ||
| ports: | ||
| - 5432:5432 | ||
|
|
||
| volumes: | ||
| postgres_data: |
This file contains hidden or 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,11 @@ | ||
| import { defineConfig } from 'drizzle-kit'; | ||
| import { env } from './src/env'; | ||
|
|
||
| export default defineConfig({ | ||
| schema: 'drizzle/schemas', | ||
| out: 'drizzle/migrations', | ||
| dialect: 'postgresql', | ||
| dbCredentials: { | ||
| url: env.DATABASE_URL | ||
| } | ||
| }); |
9 changes: 9 additions & 0 deletions
9
examples/with-auth/drizzle/migrations/0000_initial_db_structure.sql
This file contains hidden or 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,9 @@ | ||
| CREATE TABLE "users" ( | ||
| "id" uuid PRIMARY KEY NOT NULL, | ||
| "first_name" varchar(255) NOT NULL, | ||
| "last_name" varchar(255) NOT NULL, | ||
| "email" text, | ||
| "password" text NOT NULL, | ||
| CONSTRAINT "users_id_unique" UNIQUE("id"), | ||
| CONSTRAINT "users_email_unique" UNIQUE("email") | ||
| ); |
77 changes: 77 additions & 0 deletions
77
examples/with-auth/drizzle/migrations/meta/0000_snapshot.json
This file contains hidden or 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,77 @@ | ||
| { | ||
| "id": "0c57587a-46ae-47d0-ab01-fb42b01f4a6c", | ||
| "prevId": "00000000-0000-0000-0000-000000000000", | ||
| "version": "7", | ||
| "dialect": "postgresql", | ||
| "tables": { | ||
| "public.users": { | ||
| "name": "users", | ||
| "schema": "", | ||
| "columns": { | ||
| "id": { | ||
| "name": "id", | ||
| "type": "uuid", | ||
| "primaryKey": true, | ||
| "notNull": true | ||
| }, | ||
| "first_name": { | ||
| "name": "first_name", | ||
| "type": "varchar(255)", | ||
| "primaryKey": false, | ||
| "notNull": true | ||
| }, | ||
| "last_name": { | ||
| "name": "last_name", | ||
| "type": "varchar(255)", | ||
| "primaryKey": false, | ||
| "notNull": true | ||
| }, | ||
| "email": { | ||
| "name": "email", | ||
| "type": "text", | ||
| "primaryKey": false, | ||
| "notNull": false | ||
| }, | ||
| "password": { | ||
| "name": "password", | ||
| "type": "text", | ||
| "primaryKey": false, | ||
| "notNull": true | ||
| } | ||
| }, | ||
| "indexes": {}, | ||
| "foreignKeys": {}, | ||
| "compositePrimaryKeys": {}, | ||
| "uniqueConstraints": { | ||
| "users_id_unique": { | ||
| "name": "users_id_unique", | ||
| "nullsNotDistinct": false, | ||
| "columns": [ | ||
| "id" | ||
| ] | ||
| }, | ||
| "users_email_unique": { | ||
| "name": "users_email_unique", | ||
| "nullsNotDistinct": false, | ||
| "columns": [ | ||
| "email" | ||
| ] | ||
| } | ||
| }, | ||
| "policies": {}, | ||
| "checkConstraints": {}, | ||
| "isRLSEnabled": false | ||
| } | ||
| }, | ||
| "enums": {}, | ||
| "schemas": {}, | ||
| "sequences": {}, | ||
| "roles": {}, | ||
| "policies": {}, | ||
| "views": {}, | ||
| "_meta": { | ||
| "columns": {}, | ||
| "schemas": {}, | ||
| "tables": {} | ||
| } | ||
| } |
This file contains hidden or 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,13 @@ | ||
| { | ||
| "version": "7", | ||
| "dialect": "postgresql", | ||
| "entries": [ | ||
| { | ||
| "idx": 0, | ||
| "version": "7", | ||
| "when": 1741633229271, | ||
| "tag": "0000_initial_db_structure", | ||
| "breakpoints": true | ||
| } | ||
| ] | ||
| } |
This file contains hidden or 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 @@ | ||
| /** Automatically generated by @palmares/drizzle-engine on 2025-03-10T18:54:40.648Z */ | ||
|
|
||
| import * as d from '@palmares/drizzle-engine/pg-core'; | ||
|
|
||
| export const users = d.pgTable('users', { | ||
| id: d.uuid('id').primaryKey().notNull().unique(), | ||
| firstName: d.varchar('first_name', { length: 255 }).notNull(), | ||
| lastName: d.varchar('last_name', { length: 255 }).notNull(), | ||
| email: d.text('email').unique(), | ||
| password: d.text('password').notNull() | ||
| }); | ||
|
|
This file contains hidden or 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,10 @@ | ||
| import config from '../../eslint.config.js'; | ||
|
|
||
| /** @type {import('eslint').Linter.RulesRecord} */ | ||
| const configs = [{ | ||
| ...config[0], | ||
| name: '@palmares/auth', | ||
| files: ['src/**/*.ts',], | ||
| }]; | ||
|
|
||
| export default configs; |
This file contains hidden or 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 @@ | ||
| import { Commands } from '@palmares/core'; | ||
|
|
||
| import settings from './src/settings'; | ||
|
|
||
| Commands.handleCommands(settings, process.argv.slice(2)); |
This file contains hidden or 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,4 @@ | ||
| import { Commands } from '@palmares/core'; | ||
| import settings from './src/settings'; | ||
|
|
||
| Commands.handleCommands(settings, process.argv.slice(2)); |
This file contains hidden or 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,47 @@ | ||
| { | ||
| "name": "@examples/with-auth", | ||
| "version": "0.0.0", | ||
| "description": "Tests the auth package", | ||
| "keywords": [ | ||
| "palmares", | ||
| "with", | ||
| "auth" | ||
| ], | ||
| "homepage": "https://github.com/palmaresHQ/palmares#readme", | ||
| "bugs": { | ||
| "url": "https://github.com/palmaresHQ/palmares/issues" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/palmaresHQ/palmares.git" | ||
| }, | ||
| "license": "MIT", | ||
| "author": "Vicente Sanchez", | ||
| "type": "module", | ||
| "main": "manage.ts", | ||
| "scripts": { | ||
| "dev": "tsx watch --env-file=.env manage.ts runserver", | ||
| "db:generate": "drizzle-kit generate --name", | ||
| "db:migrate": "tsx --env-file=.env manage.drizzle.ts drizzleMigrate", | ||
| "db:load": "tsx --env-file=.env manage.drizzle.ts load-models", | ||
| "db:studio": "drizzle-kit studio" | ||
| }, | ||
| "dependencies": { | ||
| "@palmares/auth": "workspace:*", | ||
| "@palmares/console-logging": "workspace:*", | ||
| "@palmares/core": "workspace:*", | ||
| "@palmares/databases": "workspace:*", | ||
| "@palmares/drizzle-engine": "workspace:*", | ||
| "@palmares/express-adapter": "workspace:*", | ||
| "@palmares/logging": "workspace:*", | ||
| "@palmares/node-std": "workspace:*", | ||
| "@palmares/password-auth": "workspace:*", | ||
| "@palmares/schemas": "workspace:*", | ||
| "@palmares/server": "workspace:*", | ||
| "@palmares/zod-schema": "workspace:*", | ||
| "@palmares/jwt-adapter": "workspace:*" | ||
| }, | ||
| "devDependencies": { | ||
| "drizzle-kit": "^0.30.5" | ||
| } | ||
| } |
This file contains hidden or 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,10 @@ | ||
| #!/bin/bash | ||
| set -e | ||
|
|
||
| # Run the standard entrypoint setup from Postgres | ||
| export PGPASSWORD="$POSTGRES_PASSWORD" | ||
|
|
||
| psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL | ||
| CREATE DATABASE with_auth_dev; | ||
| CREATE DATABASE with_auth_test; | ||
| EOSQL |
This file contains hidden or 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,3 @@ | ||
| import { getAuth } from 'packages/auth/dist/src'; | ||
|
|
||
Vicentesan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| export const auth = getAuth(); | ||
This file contains hidden or 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,51 @@ | ||
| import { domain } from '@palmares/core'; | ||
| import { databaseDomainModifier } from '@palmares/databases'; | ||
| import { migrate } from '@palmares/drizzle-engine/node-postgres/migrator'; | ||
| import * as p from '@palmares/schemas'; | ||
| import { Response, path, serverDomainModifier } from '@palmares/server'; | ||
| import { ZodSchemaAdapter } from '@palmares/zod-schema'; | ||
|
|
||
| import { auth } from './auth'; | ||
| import { db } from '../db'; | ||
| import { getUserByEmail } from '../db/repositories/users'; | ||
| import { users } from '../db/schemas'; | ||
|
|
||
| p.setDefaultAdapter(new ZodSchemaAdapter()); | ||
|
|
||
| export default domain('core', import.meta.dirname, { | ||
| modifiers: [serverDomainModifier, databaseDomainModifier], | ||
|
|
||
| commands: { | ||
| drizzleMigrate: { | ||
| description: 'Migrate the database using drizzle', | ||
| keywordArgs: undefined, | ||
| positionalArgs: undefined, | ||
| handler: () => { | ||
| migrate(db, { migrationsFolder: './drizzle/migrations' }); | ||
| } | ||
| } | ||
| }, | ||
|
|
||
| getRoutes: () => | ||
| path('/login/password').post(async ({ body }) => { | ||
| const { email, password } = body; | ||
|
|
||
| const user = await getUserByEmail(email); | ||
|
|
||
| if (!user[0]) return Response.json({ message: 'User not found', status: 404 }); | ||
|
|
||
| const isPasswordValid = await auth.password.validate(password, user[0].password); | ||
|
|
||
| if (!isPasswordValid) return Response.json({ message: 'Invalid password', status: 401 }); | ||
|
|
||
| const token = await new auth.jwt.SignJwt({ sub: user[0].id }).sign(process.env.JWT_SECRET!); | ||
|
|
||
| return Response.json({ token }); | ||
| }), | ||
|
|
||
| getModels: () => ({ | ||
| users | ||
| }), | ||
|
|
||
| getMigrations: () => [] | ||
| }); |
This file contains hidden or 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,16 @@ | ||
| import DrizzleDatabaseAdapter from '@palmares/drizzle-engine'; | ||
| import { drizzle as drizzleBetterPostgres } from '@palmares/drizzle-engine/node-postgres'; | ||
|
|
||
| import * as databaseSchema from './schemas'; | ||
| import { env } from '../env'; | ||
|
|
||
| export const db = drizzleBetterPostgres(env.DATABASE_URL, { | ||
| schema: databaseSchema, | ||
| logger: env.APP_ENV !== 'prod' | ||
| }); | ||
|
|
||
| export const databaseEngine = DrizzleDatabaseAdapter.new({ | ||
| output: './drizzle', | ||
| type: 'postgres', | ||
| drizzle: db | ||
| }); |
This file contains hidden or 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,23 @@ | ||
| import { users } from '../schemas/users'; | ||
|
|
||
| import type { InferModel } from '@palmares/databases'; | ||
|
|
||
| export async function createUser(data: InferModel<typeof users, 'create'>) { | ||
| return await users.default.set((qs) => qs.data(data)); | ||
| } | ||
|
|
||
| export async function getUserByEmail(email: string) { | ||
| return await users.default.get((qs) => qs.where({ email })); | ||
| } | ||
|
|
||
| export async function getUserById(id: string) { | ||
| return await users.default.get((qs) => qs.where({ id })); | ||
| } | ||
|
|
||
| export async function updateUser(id: string, data: InferModel<typeof users, 'update'>) { | ||
| return await users.default.set((qs) => qs.where({ id }).data(data)); | ||
| } | ||
|
|
||
| export async function deleteUser(id: string) { | ||
| return await users.default.remove((qs) => qs.where({ id })); | ||
| } |
This file contains hidden or 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 @@ | ||
| export * from './users'; |
This file contains hidden or 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,11 @@ | ||
| import { Manager, define, fields } from '@palmares/databases'; | ||
|
|
||
| export const users = define('users', { | ||
| fields: { | ||
| id: fields.uuid().primaryKey().unique(), | ||
| firstName: fields.char({ maxLen: 255 }), | ||
| lastName: fields.char({ maxLen: 255 }), | ||
| email: fields.text().unique(), | ||
| password: fields.text() | ||
| } | ||
| }); |
This file contains hidden or 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 { z } from '@palmares/zod-schema'; | ||
|
|
||
| /** | ||
| * here im not using directly @palmares/schemas cuz it requeries | ||
| * use of async functions and drizzle does not support top-level await | ||
| * */ | ||
|
|
||
| const envSchema = z.object({ | ||
| APP_ENV: z.enum(['dev', 'prod', 'test']).default('dev'), | ||
| DATABASE_URL: z.string(), | ||
| JWT_SECRET: z.string() | ||
| }); | ||
|
|
||
| export const env = envSchema.parse(process.env); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unresolved merge conflict markers detected. Please resolve the conflict and remove all merge conflict markers.