Skip to content

Commit 0e11b58

Browse files
committed
feat: add auth package with password adapter example
1 parent 091867d commit 0e11b58

File tree

5 files changed

+66
-24
lines changed

5 files changed

+66
-24
lines changed
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
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

78
getRoutes: () =>
8-
path('/test').get(() => {
9-
return Response.json({ message: 'hello' });
9+
path('/test').get(async () => {
10+
return getAdapters();
1011
})
1112
});

examples/with-auth/src/settings.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import defineAuthConfig, { getAuth } from '@palmares/auth';
2+
import defineAuthDomain, { Auth, AuthAdapter } from '@palmares/auth';
23
import ConsoleLogging from '@palmares/console-logging';
34
import PalmaresCoreDomain, { defineSettings } from '@palmares/core';
45
import { ExpressServerAdapter } from '@palmares/express-adapter';
@@ -65,9 +66,28 @@ export default defineSettings({
6566
}
6667
}
6768
],
68-
defineAuthConfig({
69-
adapters: [passwordAdapter.new({ prefix: 'my-prefix', suffix: 'my-suffix' })]
69+
defineAuthDomain({
70+
adapters: [
71+
passwordAdapter.new({
72+
prefix: 'my-prefix',
73+
suffix: 'my-suffix'
74+
})
75+
]
7076
}),
7177
CoreDomain
7278
]
7379
});
80+
81+
declare global {
82+
namespace Palmares {
83+
interface PAuth {
84+
adapters: [ReturnType<typeof PasswordAuthAdapter.new>];
85+
}
86+
}
87+
}
88+
89+
const auth = Auth;
90+
91+
type test2 = Palmares.PAuth['adapters'][number];
92+
93+
type test3 = ReturnType<typeof PasswordAuthAdapter.new>;

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

packages/auth/src/auth.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import { getAdapters } from './conf';
22

3-
import type { AuthAdapters } from '.';
43
import type { AdapterMethods, AuthAdapter } from './adapter';
54

6-
type AuthProxy<TAdapters extends readonly (AuthAdapter | unknown)[]> = {
7-
[KAdapter in TAdapters[number] as KAdapter extends AuthAdapter
8-
? KAdapter['name']
9-
: never]: KAdapter extends AuthAdapter ? KAdapter['methods'] : never;
5+
type AuthProxy<TAdapters> = {
6+
// [KAdapter in TAdapters[number] as KAdapter['name']]: KAdapter['methods'];
107
};
118

129
const createAdapterProxy = <TMethods extends AdapterMethods>(methods: TMethods): TMethods =>
@@ -19,14 +16,13 @@ const createAdapterProxy = <TMethods extends AdapterMethods>(methods: TMethods):
1916
}
2017
});
2118

22-
export function getAuth<TAdapters extends AuthAdapters & Palmares.PAuth = AuthAdapters & Palmares.PAuth>(): AuthProxy<
23-
TAdapters['adapters']
24-
> {
25-
return new Proxy(
19+
type GetAdapters = ReturnType<typeof getAdapters>;
20+
21+
const createAuthProxy = <TAdapters = Palmares.PAuth extends { adapters: any } ? true : false>(adapters: GetAdapters) =>
22+
new Proxy(
2623
{},
2724
{
2825
get(_, adapterName: string) {
29-
const adapters = getAdapters();
3026
const adapter = adapters.find((a) => a.name === adapterName);
3127

3228
if (adapter) {
@@ -36,5 +32,6 @@ export function getAuth<TAdapters extends AuthAdapters & Palmares.PAuth = AuthAd
3632
throw new Error(`Adapter "${adapterName}" not found`);
3733
}
3834
}
39-
) as any;
40-
}
35+
) as AuthProxy<TAdapters>;
36+
37+
export const Auth = createAuthProxy(getAdapters());

packages/auth/src/index.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { defineAuthDomain } from './domain';
22

3-
import type { AuthAdapter } from './adapter';
4-
53
export { authAdapter, AuthAdapter, type AdapterMethods } from './adapter';
64
export {
75
AuthenticationFailedException,
@@ -16,11 +14,7 @@ export {
1614
} from './exceptions';
1715
export type { AuthConfigurationType, AuthAdapterType } from './types';
1816

19-
export { getAuth } from './auth';
17+
export { Auth } from './auth';
2018
export { defineAuthDomain as default };
2119

22-
export interface AuthAdapters<TAdapters extends readonly (AuthAdapter | unknown)[] = unknown[]> {
23-
adapters: TAdapters;
24-
}
25-
2620
export { getAdapters } from './conf';

0 commit comments

Comments
 (0)