95 lines
4.5 KiB
Markdown
95 lines
4.5 KiB
Markdown
# Token Authentication
|
|
|
|
This Package contains a Token Authentication Module for NestJs with Organizations UserAccounts Groups and Rights access.
|
|
|
|
## Required external Services
|
|
|
|
You have to implement these Interfaces and give the Implementations into the Dynamic Module.
|
|
|
|
| Service | Injection Token | Interface in Contracts | Description |
|
|
| ----------------------- | ----------------------------------- | ------------------------ | -------------------------------------------------------------- |
|
|
| Token Service | @apihub24/token_service | TokenService | a Service that generates the Token for authentication |
|
|
| Password Service | @apihub24/password_service | PasswordService | a Service that hash the Password |
|
|
| Session Service | @apihub24/session_service | SessionService | a Service that handles Session Get, Save, Delete Operations |
|
|
| Mail Service | @apihub24/mail_verification_service | MailVerificationService | a Service that handles Send Verify Mail and the verify Request |
|
|
| Account Repository | @apihub24/account_repository | Repository<Account> | a Service that handles Session Get, Save, Delete Operations |
|
|
| Group Repository | @apihub24/group_repository | Repository<Group> | a Service that handles Session Get, Save, Delete Operations |
|
|
| Right Repository | @apihub24/right_repository | Repository<Right> | a Service that handles Session Get, Save, Delete Operations |
|
|
| Organization Repository | @apihub24/organization_repository | Repository<Organization> | a Service that handles Session Get, Save, Delete Operations |
|
|
|
|
If you want to use a InMemory Repository you not have to implement itself you can use the package [@apihub24/in-memory-repository](https://www.npmjs.com/package/@apihub24/in-memory-repository).
|
|
|
|
## Example
|
|
|
|
```typescript
|
|
import { Module } from "@nestjs/common";
|
|
import {
|
|
Account,
|
|
Group,
|
|
Organization,
|
|
Right,
|
|
TokenAuthenticationModule,
|
|
} from "@apihub24/token-authentication";
|
|
import { LoginController } from "./controllers/login.controller";
|
|
import { LogoutController } from "./controllers/logout.controller";
|
|
import { ConfigModule } from "@nestjs/config";
|
|
// a implementation of the EmailService
|
|
import { EmailVerificationModule } from "@apihub24/email-verification";
|
|
// a implementation of the InMemoryRepository used for Repositories
|
|
import { InMemoryRepository } from "@apihub24/in-memory-repository";
|
|
// a implementation of the SessionService
|
|
import { InMemorySessionsModule } from "@apihub24/in-memory-sessions";
|
|
// a implementation of the TokenService
|
|
import { JwtGeneratorModule } from "@apihub24/jwt-generator";
|
|
// a implementation of the PasswordService
|
|
import { PasswordHasherModule } from "@apihub24/password-hasher";
|
|
import { takeExportedProviders } from "@apihub24/nestjs-helper";
|
|
|
|
// use the Dynamic Modules from the Implementations
|
|
const emailVerificationModule = EmailVerificationModule.forRoot();
|
|
const sessionModule = InMemorySessionsModule.forRoot();
|
|
// JwtGeneratorModule requires the SessionModule so give them the Providers
|
|
const jwtModule = JwtGeneratorModule.forRoot([
|
|
...takeExportedProviders(sessionModule),
|
|
]);
|
|
const passwordModule = PasswordHasherModule.forRoot();
|
|
|
|
@Module({
|
|
imports: [
|
|
ConfigModule.forRoot(),
|
|
emailVerificationModule,
|
|
sessionModule,
|
|
jwtModule,
|
|
passwordModule,
|
|
TokenAuthenticationModule.forRoot([
|
|
...takeExportedProviders(emailVerificationModule),
|
|
...takeExportedProviders(sessionModule),
|
|
...takeExportedProviders(jwtModule),
|
|
...takeExportedProviders(passwordModule),
|
|
// register the Repositories with InMemoryRepository
|
|
{
|
|
provide: "@apihub24/organization_repository",
|
|
useClass: InMemoryRepository<Organization>,
|
|
},
|
|
{
|
|
provide: "@apihub24/account_repository",
|
|
useClass: InMemoryRepository<Account>,
|
|
},
|
|
{
|
|
provide: "@apihub24/group_repository",
|
|
useClass: InMemoryRepository<Group>,
|
|
},
|
|
{
|
|
provide: "@apihub24/right_repository",
|
|
useClass: InMemoryRepository<Right>,
|
|
},
|
|
]),
|
|
],
|
|
controllers: [LoginController, LogoutController],
|
|
providers: [],
|
|
// required exports for the TokenGuard
|
|
exports: [TokenAuthenticationModule, jwtModule],
|
|
})
|
|
export class AuthenticationModule {}
|
|
```
|