@cauth/prisma
Version:
Prisma integration for CAuth authentication system.
141 lines (107 loc) • 3.65 kB
Markdown
# /prisma
Prisma integration for CAuth authentication system.
## Features
- **Prisma Integration**: Database provider for CAuth using Prisma
- **Type Safety**: Full TypeScript support with Prisma Client
- **Schema Support**: Ready-to-use Prisma schema for authentication
- **Account Management**: User account CRUD operations
- **Token Management**: Refresh token handling
- **OTP Support**: One-time password functionality
## Installation
```bash
npm install /prisma /core /client
# or
yarn add /prisma /core /client
# or
pnpm add /prisma /core /client
```
## Quick Start
1. Add the authentication models to your Prisma schema:
```prisma
model Auth {
id String
phoneNumber String?
email String?
role String
passwordHash String
lastLogin DateTime
refreshTokens String[]
createdAt DateTime
updatedAt DateTime
user User?
otp Otp?
@
}
model Otp {
id String
auth Auth
code String
purpose OtpPurpose
expiresAt DateTime
isUsed Boolean
createdAt DateTime
updatedAt DateTime
@
}
// ADD AS MANY PURPOSES YOU WISH, BUT DON'T REMOVE RESET_PASSWORD AND LOGIN
enum OtpPurpose {
LOGIN
RESET_PASSWORD
ACTION
}
```
2. Generate Prisma Client:
```bash
npx prisma generate
```
3. Use with CAuth:
```typescript
import { PrismaClient } from '@prisma/client';
import { CAuth } from '@cauth/core';
import { PrismaProvider } from '@cauth/prisma';
import { ExpressContractor } from '@cauth/express';
const prisma = new PrismaClient();
const auth = CAuth({
roles: ['USER', 'ADMIN'],
dbContractor: new PrismaProvider(prisma),
routeContractor: new ExpressContractor(),
jwtConfig: {
accessTokenSecret: process.env.ACCESS_TOKEN_SECRET!,
refreshTokenSecret: process.env.REFRESH_TOKEN_SECRET!,
}
});
```
## API Reference
### PrismaProvider
The `PrismaProvider` class implements the `DatabaseContract` interface:
```typescript
class PrismaProvider implements DatabaseContract {
constructor(prisma: PrismaClient);
// Account Management
findAccountById<T>({ id, select }: { id: string; select?: any }): Promise<T | undefined>;
findAccountWithCredential<T>({ email, phoneNumber, select }: { email?: string; phoneNumber?: string; select?: any }): Promise<T | undefined>;
createAccount<T>({ data, select }: { data: any; select?: any }): Promise<T>;
updateAccount<T>({ id, data, select }: { id: string; data: any; select?: any }): Promise<T>;
deleteAccount({ id }: { id: string }): Promise<void>;
// Token Management
updateAccountLogin<T>({ id, refreshToken, select }: { id: string; refreshToken: string; select?: any }): Promise<T>;
removeAndAddRefreshToken({ id, refreshToken, newRefreshToken, select }: { id: string; refreshToken: string; newRefreshToken?: string; select?: any }): Promise<any>;
// OTP Management
createOTP<T>({ config }: { config: CAuthOptions }, { id, purpose }: { id: string; purpose: OtpPurpose }): Promise<T>;
verifyOTP<T>({ id, code, purpose }: { id: string; code: string; purpose: OtpPurpose }): Promise<T>;
}
```
## Development
### Prerequisites
- Node.js >= 18
- TypeScript >= 5.9
- Prisma >= 5.0
### Building
```bash
pnpm install
pnpm build
```
## License
MIT License - see LICENSE file for details.
## Support
For issues and feature requests, please visit the [GitHub repository](https://github.com/jonace-mpelule/cauth).