Skip to content

Commit e0926bf

Browse files
committed
feat: add auth package with password adapter example
1 parent b993399 commit e0926bf

File tree

18 files changed

+682
-132
lines changed

18 files changed

+682
-132
lines changed

eslint.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ const config = tseslint.config({
4343
...prettier,
4444
...javascript,
4545
...typescript,
46-
...imports
46+
...imports,
47+
'ts/no-namespace': 'off'
4748
}
4849
});
4950

examples/with-auth/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"@palmares/node-std": "workspace:*",
3232
"@palmares/sequelize-engine": "workspace:*",
3333
"@palmares/server": "workspace:*",
34-
"@palmares/password-auth": "workspace:*"
34+
"@palmares/password-auth": "workspace:*",
35+
"@palmares/auth": "workspace:*"
3536
}
3637
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { domain } from '@palmares/core';
22
import { Response, path, serverDomainModifier } from '@palmares/server';
3+
import { getAdapters } from 'packages/auth/dist/src/conf';
34

45
export default domain('core', import.meta.dirname, {
56
modifiers: [serverDomainModifier],
67
getRoutes: () =>
78
path('/test').get(async () => {
8-
return Response.json({
9-
message: 'Hello world'
10-
});
9+
return getAdapters();
1110
})
1211
});

examples/with-auth/src/settings.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import NodeStd from '@palmares/node-std';
66
import ServerDomain, { Response } from '@palmares/server';
77
import { dirname, resolve } from 'path';
88

9+
import PasswordAuthAdapter, { passwordAdapter } from '@palmares/password-auth';
910
import CoreDomain from './core';
11+
import defineAuthDomain, { Auth, AuthAdapter } from '@palmares/auth';
1012

1113
export default defineSettings({
1214
basePath: dirname(resolve(import.meta.dirname)),
@@ -30,7 +32,7 @@ export default defineSettings({
3032
[
3133
ServerDomain,
3234
{
33-
servers: {
35+
servers: {
3436
default: {
3537
server: ExpressServerAdapter,
3638
debug: true,
@@ -54,6 +56,26 @@ export default defineSettings({
5456
}
5557
}
5658
],
59+
defineAuthDomain({
60+
adapters: [passwordAdapter.new({
61+
prefix: 'my-prefix',
62+
suffix: 'my-suffix'
63+
})]
64+
}),
5765
CoreDomain
5866
]
5967
});
68+
69+
declare global {
70+
namespace Palmares {
71+
interface PAuth {
72+
adapters: [ReturnType<typeof PasswordAuthAdapter.new>]
73+
}
74+
}
75+
}
76+
77+
const auth = Auth
78+
79+
type test2 = Palmares.PAuth['adapters'][number]
80+
81+
type test3 = ReturnType<typeof PasswordAuthAdapter.new>

examples/with-auth/tsconfig.json

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
{
2-
"extends": "../../tsconfig.json",
3-
"compilerOptions": {
4-
"declaration": false,
5-
"outDir": "./dist",
6-
"types": ["node"],
7-
"lib": ["DOM", "ES2020", "DOM.Iterable"],
8-
"paths": {
9-
"*": ["node_modules/*"],
10-
"@palmares/server": ["../../packages/server/dist"],
11-
"@palmares/core": ["../../packages/server/node_modules/@palmares/core"],
12-
}
13-
},
14-
"include": ["src/**/*"]
15-
}
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"module": "ESNext",
5+
"moduleResolution": "bundler",
6+
"declaration": false,
7+
"outDir": "./dist",
8+
"types": ["node"],
9+
"lib": ["DOM", "ES2020", "DOM.Iterable"]
10+
},
11+
"include": ["src/**/*", "manage.ts"],
12+
"exclude": []
13+
}

libs/password-auth/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,13 @@
4444
},
4545
"homepage": "https://github.com/palmaresHQ/palmares#readme",
4646
"dependencies": {
47+
"@palmares/auth": "workspace:*",
4748
"@palmares/core": "workspace:*",
4849
"@palmares/events": "workspace:*",
49-
"@palmares/logging": "workspace:*"
50+
"@palmares/logging": "workspace:*",
51+
"bcrypt": "^5.1.1"
52+
},
53+
"devDependencies": {
54+
"@types/bcrypt": "^5.0.2"
5055
}
5156
}

libs/password-auth/src/adapter.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { authAdapter } from '@palmares/auth';
2+
import { compare, hash } from 'bcrypt';
3+
4+
export const passwordAdapter = authAdapter((_args: { suffix: string; prefix: string }) => ({
5+
name: 'password',
6+
methods: {
7+
hash: (password: string) => {
8+
return hash(password, 10);
9+
},
10+
validate: (password: string, toValidate: string) => {
11+
return compare(password, toValidate);
12+
}
13+
}
14+
}));

libs/password-auth/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { passwordAdapter } from './adapter';
2+
3+
export { passwordAdapter };
4+
export { passwordAdapter as default };

packages/auth/CHANGELOG.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
11
# @palmares/auth
22

3-
## [UNRELEASED]
3+
All notable changes to this package will be documented in this file.
4+
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
5+
6+
## [Unreleased]
7+
8+
### Added
9+
- Core authentication domain with zero dependencies
10+
- Flexible adapter system for implementing different auth strategies
11+
- Type-safe authentication flows with comprehensive TypeScript support
12+
- Base `AuthAdapter` class with factory pattern for creating custom adapters
13+
- Core authentication interface with:
14+
- Generic type system for credentials, results, and identifiers
15+
- Optional instance and configuration types
16+
- Minimal required methods (`authenticate`, `getIdentifier`)
17+
- Optional event system with standard auth events:
18+
- `auth:attempt`, `auth:success`, `auth:failure`
19+
- `auth:logout`, `auth:refresh`, `auth:revoke`
20+
- `auth:validate`, `auth:error`
21+
- Optional hook system with:
22+
- Pre/post operation hooks
23+
- Priority-based execution
24+
- Typed context data
25+
- Async support
26+
- Authentication domain with core methods:
27+
- `authenticate` - Handle user authentication
28+
- `verify` - Verify authentication credentials
29+
- `invalidate` - Invalidate authentication credentials
30+
- Error handling system with:
31+
- Base `AuthException` class
32+
- `AuthAdapterException` for adapter-specific errors
33+
- `NotImplementedAuthAdapterException` for unimplemented methods

0 commit comments

Comments
 (0)