@ofeklabs/horizon-auth 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +66 -0
- package/package.json +1 -1
- package/prisma/README.md +143 -0
package/README.md
CHANGED
|
@@ -99,6 +99,72 @@ Each application has its own authentication.
|
|
|
99
99
|
|
|
100
100
|
### 1. Install
|
|
101
101
|
|
|
102
|
+
```bash
|
|
103
|
+
npm install @ofeklabs/horizon-auth @prisma/client
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### 2. **CRITICAL**: Add Required Prisma Models
|
|
107
|
+
|
|
108
|
+
The package requires specific Prisma models in your schema. Copy all models from `node_modules/@ofeklabs/horizon-auth/prisma/schema.prisma` to your `prisma/schema.prisma`:
|
|
109
|
+
|
|
110
|
+
```prisma
|
|
111
|
+
// Your existing datasource and generator
|
|
112
|
+
datasource db {
|
|
113
|
+
provider = "postgresql"
|
|
114
|
+
url = env("DATABASE_URL")
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
generator client {
|
|
118
|
+
provider = "prisma-client-js"
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// ADD these models from @ofeklabs/horizon-auth
|
|
122
|
+
model User {
|
|
123
|
+
id String @id @default(cuid())
|
|
124
|
+
email String @unique
|
|
125
|
+
fullName String?
|
|
126
|
+
passwordHash String?
|
|
127
|
+
emailVerified Boolean @default(false)
|
|
128
|
+
emailVerifyToken String? @unique
|
|
129
|
+
resetToken String? @unique
|
|
130
|
+
resetTokenExpiry DateTime?
|
|
131
|
+
tenantId String @default("default")
|
|
132
|
+
roles String[] @default(["user"])
|
|
133
|
+
isActive Boolean @default(true)
|
|
134
|
+
deactivationReason String?
|
|
135
|
+
refreshTokens RefreshToken[]
|
|
136
|
+
socialAccounts SocialAccount[]
|
|
137
|
+
devices Device[]
|
|
138
|
+
pushTokens PushToken[]
|
|
139
|
+
twoFactorAuth TwoFactorAuth?
|
|
140
|
+
backupCodes BackupCode[]
|
|
141
|
+
createdAt DateTime @default(now())
|
|
142
|
+
updatedAt DateTime @updatedAt
|
|
143
|
+
|
|
144
|
+
@@index([email])
|
|
145
|
+
@@index([tenantId])
|
|
146
|
+
@@index([isActive])
|
|
147
|
+
@@map("users")
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
model RefreshToken {
|
|
151
|
+
// ... (see prisma/schema.prisma for complete definition)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// ... (copy all other models)
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
**See `node_modules/@ofeklabs/horizon-auth/prisma/README.md` for complete schema and customization options.**
|
|
158
|
+
|
|
159
|
+
Then generate Prisma Client and run migrations:
|
|
160
|
+
|
|
161
|
+
```bash
|
|
162
|
+
npx prisma generate
|
|
163
|
+
npx prisma migrate dev --name add-horizon-auth-models
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### 3. Create PrismaModule
|
|
167
|
+
|
|
102
168
|
```bash
|
|
103
169
|
npm install @ofeklabs/horizon-auth @prisma/client ioredis passport passport-jwt
|
|
104
170
|
npm install -D prisma
|
package/package.json
CHANGED
package/prisma/README.md
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
# Prisma Schema for @ofeklabs/horizon-auth
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
This directory contains the Prisma schema required by the @ofeklabs/horizon-auth package. Your application MUST include these models in its own `schema.prisma` file.
|
|
6
|
+
|
|
7
|
+
## Required Setup
|
|
8
|
+
|
|
9
|
+
### Step 1: Copy Models to Your Schema
|
|
10
|
+
|
|
11
|
+
Copy all models from `schema.prisma` in this directory to your application's `prisma/schema.prisma` file.
|
|
12
|
+
|
|
13
|
+
**IMPORTANT**: Merge the models - don't replace your entire schema!
|
|
14
|
+
|
|
15
|
+
### Step 2: Generate Prisma Client
|
|
16
|
+
|
|
17
|
+
After adding the models, regenerate your Prisma Client:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npx prisma generate
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### Step 3: Run Migrations
|
|
24
|
+
|
|
25
|
+
Create and apply the database migration:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npx prisma migrate dev --name add-horizon-auth-models
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Required Models
|
|
32
|
+
|
|
33
|
+
The package requires these models to function:
|
|
34
|
+
|
|
35
|
+
### Core Models (Always Required)
|
|
36
|
+
- **User** - Core user authentication and profile
|
|
37
|
+
- **RefreshToken** - JWT refresh token storage with rotation
|
|
38
|
+
|
|
39
|
+
### Feature-Specific Models (Required if feature is enabled)
|
|
40
|
+
- **SocialAccount** - OAuth social login (Google, Facebook)
|
|
41
|
+
- **Device** - Device management and tracking
|
|
42
|
+
- **PushToken** - Push notification token storage
|
|
43
|
+
- **TwoFactorAuth** - TOTP 2FA secrets
|
|
44
|
+
- **BackupCode** - 2FA backup codes
|
|
45
|
+
|
|
46
|
+
## Model Relationships
|
|
47
|
+
|
|
48
|
+
```
|
|
49
|
+
User
|
|
50
|
+
├── RefreshToken[] (one-to-many)
|
|
51
|
+
├── SocialAccount[] (one-to-many)
|
|
52
|
+
├── Device[] (one-to-many)
|
|
53
|
+
├── PushToken[] (one-to-many)
|
|
54
|
+
├── TwoFactorAuth? (one-to-one)
|
|
55
|
+
└── BackupCode[] (one-to-many)
|
|
56
|
+
|
|
57
|
+
Device
|
|
58
|
+
├── RefreshToken[] (one-to-many)
|
|
59
|
+
└── PushToken[] (one-to-many)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Customization
|
|
63
|
+
|
|
64
|
+
You can customize the models to fit your needs:
|
|
65
|
+
|
|
66
|
+
### Adding Fields
|
|
67
|
+
You can add additional fields to any model:
|
|
68
|
+
|
|
69
|
+
```prisma
|
|
70
|
+
model User {
|
|
71
|
+
// ... existing fields from horizon-auth
|
|
72
|
+
|
|
73
|
+
// Your custom fields
|
|
74
|
+
phoneNumber String?
|
|
75
|
+
avatar String?
|
|
76
|
+
preferences Json?
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Changing Table Names
|
|
81
|
+
The `@@map()` directive controls the database table name:
|
|
82
|
+
|
|
83
|
+
```prisma
|
|
84
|
+
model User {
|
|
85
|
+
// ...
|
|
86
|
+
@@map("users") // Table name in database
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Adding Relations
|
|
91
|
+
You can add relations to your own models:
|
|
92
|
+
|
|
93
|
+
```prisma
|
|
94
|
+
model User {
|
|
95
|
+
// ... existing fields from horizon-auth
|
|
96
|
+
|
|
97
|
+
// Your custom relations
|
|
98
|
+
orders Order[]
|
|
99
|
+
profile UserProfile?
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Multi-Tenant Support
|
|
104
|
+
|
|
105
|
+
The User model includes a `tenantId` field for multi-tenant applications. If you're not using multi-tenancy, you can:
|
|
106
|
+
|
|
107
|
+
1. Keep the field (it defaults to "default")
|
|
108
|
+
2. Remove the field and related indexes (requires modifying the package's services)
|
|
109
|
+
|
|
110
|
+
## Database Support
|
|
111
|
+
|
|
112
|
+
The schema is designed for PostgreSQL but can be adapted for other databases:
|
|
113
|
+
|
|
114
|
+
- **PostgreSQL** - Fully supported (recommended)
|
|
115
|
+
- **MySQL** - Supported (change `@default(cuid())` to `@default(uuid())`)
|
|
116
|
+
- **SQLite** - Supported for development (not recommended for production)
|
|
117
|
+
- **MongoDB** - Not supported (package uses relational features)
|
|
118
|
+
|
|
119
|
+
## Troubleshooting
|
|
120
|
+
|
|
121
|
+
### Error: "Cannot read properties of undefined (reading 'findUnique')"
|
|
122
|
+
|
|
123
|
+
**Cause**: The User model is missing from your schema.
|
|
124
|
+
|
|
125
|
+
**Solution**: Copy all models from this schema to your application's schema.
|
|
126
|
+
|
|
127
|
+
### Error: "Invalid `prisma.user.findUnique()` invocation"
|
|
128
|
+
|
|
129
|
+
**Cause**: Prisma Client wasn't regenerated after adding models.
|
|
130
|
+
|
|
131
|
+
**Solution**: Run `npx prisma generate` and restart your application.
|
|
132
|
+
|
|
133
|
+
### Error: "Unknown field 'emailVerifyToken' on model 'User'"
|
|
134
|
+
|
|
135
|
+
**Cause**: Your User model is missing required fields.
|
|
136
|
+
|
|
137
|
+
**Solution**: Ensure you copied the complete User model definition, not just the model name.
|
|
138
|
+
|
|
139
|
+
## Need Help?
|
|
140
|
+
|
|
141
|
+
- Check the [Migration Guide](../MIGRATION.md)
|
|
142
|
+
- Review the [Main README](../README.md)
|
|
143
|
+
- Open an issue on GitHub
|