swift-auth 1.0.0 → 1.2.0
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 +261 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
# Swift Auth 🚀
|
|
2
|
+
|
|
3
|
+
A lightweight, type-safe authentication library for Next.js 15/16 and Upstash Redis. Swift Auth handles the heavy lifting of session management, password security, and cookie handling so you can focus on building your features.
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
## Table of Contents
|
|
10
|
+
|
|
11
|
+
- [Features](#features)
|
|
12
|
+
- [Installation](#installation)
|
|
13
|
+
- [Prerequisites](#prerequisites)
|
|
14
|
+
- [Quick Start](#quick-start)
|
|
15
|
+
- [Configuration](#configuration)
|
|
16
|
+
- [Core Usage](#core-usage)
|
|
17
|
+
- [Password Security](#password-security)
|
|
18
|
+
- [API Reference](#api-reference)
|
|
19
|
+
- [Why Swift Auth?](#why-swift-auth)
|
|
20
|
+
- [Troubleshooting](#troubleshooting)
|
|
21
|
+
- [License](#license)
|
|
22
|
+
|
|
23
|
+
## Features
|
|
24
|
+
|
|
25
|
+
✨ **Type-Safe**: Full TypeScript support with generic user types
|
|
26
|
+
🔐 **Secure**: Built-in password hashing with scrypt and timing-safe comparison
|
|
27
|
+
🍪 **Cookie Handling**: Automatic HTTP-only cookie management
|
|
28
|
+
⚡ **Next.js 15+**: Fully compatible with the asynchronous `cookies()` API
|
|
29
|
+
📦 **Minimal**: Only includes what you need—control exactly what's stored in Redis
|
|
30
|
+
🌍 **Production Ready**: Automatic secure flag handling for dev/production environments
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npm install swift-auth
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Or with yarn:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
yarn add swift-auth
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Or with pnpm:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
pnpm add swift-auth
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Prerequisites
|
|
51
|
+
|
|
52
|
+
- Node.js 18+
|
|
53
|
+
- Next.js 15 or 16
|
|
54
|
+
- [Upstash Redis](https://upstash.com) account with connection credentials
|
|
55
|
+
|
|
56
|
+
## Quick Start
|
|
57
|
+
|
|
58
|
+
1. **Set up environment variables**:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
REDIS_URL=https://<your-redis-url>
|
|
62
|
+
REDIS_TOKEN=<your-redis-token>
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
2. **Initialize auth in a shared file** (`lib/auth.ts`):
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
import { createAuth } from "swift-auth";
|
|
69
|
+
|
|
70
|
+
export type User = {
|
|
71
|
+
id: string;
|
|
72
|
+
name: string;
|
|
73
|
+
email: string;
|
|
74
|
+
created_at: Date;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export const auth = createAuth<User>({
|
|
78
|
+
redis: {
|
|
79
|
+
url: process.env.REDIS_URL!,
|
|
80
|
+
token: process.env.REDIS_TOKEN!,
|
|
81
|
+
},
|
|
82
|
+
ttl: 60 * 60 * 24 * 7, // 7 Days
|
|
83
|
+
sessionFields: ["id", "name", "email", "created_at"],
|
|
84
|
+
});
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
3. **Use in your Server Action** (login example):
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
"use server";
|
|
91
|
+
|
|
92
|
+
import { auth } from "@/lib/auth";
|
|
93
|
+
import { cookies } from "next/headers";
|
|
94
|
+
|
|
95
|
+
export async function signIn(user: User) {
|
|
96
|
+
const cookieStore = await cookies();
|
|
97
|
+
await auth.createUserSession(user, cookieStore);
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Configuration
|
|
102
|
+
|
|
103
|
+
Initialize your auth instance in a shared file (e.g., `lib/auth.ts`). This ensures your Redis client and session settings are consistent across your app.
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
import { createAuth } from "swift-auth";
|
|
107
|
+
|
|
108
|
+
export type User = {
|
|
109
|
+
id: string;
|
|
110
|
+
name: string;
|
|
111
|
+
email: string;
|
|
112
|
+
created_at: Date;
|
|
113
|
+
// ... any other fields
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
export const auth = createAuth<User>({
|
|
117
|
+
redis: {
|
|
118
|
+
url: process.env.REDIS_URL!,
|
|
119
|
+
token: process.env.REDIS_TOKEN!,
|
|
120
|
+
},
|
|
121
|
+
ttl: 60 * 60 * 24 * 7, // 7 Days in seconds
|
|
122
|
+
sessionFields: ["id", "name", "email", "created_at"], // Only these fields are stored in Redis
|
|
123
|
+
});
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
**Configuration Options:**
|
|
127
|
+
|
|
128
|
+
- `redis.url`: Your Upstash Redis connection URL
|
|
129
|
+
- `redis.token`: Your Upstash Redis authentication token
|
|
130
|
+
- `ttl`: Session time-to-live in seconds (default: 7 days)
|
|
131
|
+
- `sessionFields`: Array of user properties to persist in Redis
|
|
132
|
+
|
|
133
|
+
## Core Usage
|
|
134
|
+
|
|
135
|
+
### Create a Session (Login)
|
|
136
|
+
|
|
137
|
+
Use this inside a Next.js Server Action. It automatically generates a secure Session ID, stores the user data in Redis, and sets an HTTP-only cookie.
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
"use server";
|
|
141
|
+
|
|
142
|
+
import { auth } from "@/lib/auth";
|
|
143
|
+
import { cookies } from "next/headers";
|
|
144
|
+
|
|
145
|
+
export async function signIn(user: User) {
|
|
146
|
+
const cookieStore = await cookies();
|
|
147
|
+
await auth.createUserSession(user, cookieStore);
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Get Current User
|
|
152
|
+
|
|
153
|
+
Retrieve the session data in any Server Component or Layout.
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
import { auth } from "@/lib/auth";
|
|
157
|
+
import { cookies } from "next/headers";
|
|
158
|
+
|
|
159
|
+
export default async function Dashboard() {
|
|
160
|
+
const user = await auth.getCurrentUser(await cookies());
|
|
161
|
+
|
|
162
|
+
if (!user) return <div>Please log in</div>;
|
|
163
|
+
return <div>Welcome back, {user.name}</div>;
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Update User Session
|
|
168
|
+
|
|
169
|
+
Modify session data without creating a new session ID:
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
"use server";
|
|
173
|
+
|
|
174
|
+
import { auth } from "@/lib/auth";
|
|
175
|
+
import { cookies } from "next/headers";
|
|
176
|
+
|
|
177
|
+
export async function updateProfile(user: User) {
|
|
178
|
+
const cookieStore = await cookies();
|
|
179
|
+
await auth.updateUserSession(user, cookieStore);
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### End Session (Logout)
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
"use server";
|
|
187
|
+
|
|
188
|
+
import { auth } from "@/lib/auth";
|
|
189
|
+
import { cookies } from "next/headers";
|
|
190
|
+
|
|
191
|
+
export async function signOut() {
|
|
192
|
+
await auth.removeUserFromSession(await cookies());
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## Password Security
|
|
197
|
+
|
|
198
|
+
Swift Auth provides built-in helpers for secure password management using Node's `scrypt` and `timingSafeEqual`.
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
// 1. Registering a user
|
|
202
|
+
const salt = auth.generateSalt();
|
|
203
|
+
const hashedPassword = await auth.hashPassword("my-password", salt);
|
|
204
|
+
|
|
205
|
+
// 2. Verifying a user during login
|
|
206
|
+
const isValid = await auth.comparePassword({
|
|
207
|
+
password: "my-password",
|
|
208
|
+
salt: salt,
|
|
209
|
+
hashedPassword: hashedPassword,
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
if (isValid) {
|
|
213
|
+
// Password is correct
|
|
214
|
+
} else {
|
|
215
|
+
// Password is incorrect
|
|
216
|
+
}
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## API Reference
|
|
220
|
+
|
|
221
|
+
| Method | Description |
|
|
222
|
+
| -------------------------------------- | ------------------------------------------------------- |
|
|
223
|
+
| `getCurrentUser(cookieStore)` | Returns the session data from Redis based on the cookie |
|
|
224
|
+
| `createUserSession(user, cookieStore)` | Creates a new session and sets the browser cookie |
|
|
225
|
+
| `updateUserSession(user, cookieStore)` | Updates the Redis data without changing the Session ID |
|
|
226
|
+
| `removeUserFromSession(cookieStore)` | Deletes the Redis key and clears the browser cookie |
|
|
227
|
+
| `hashPassword(password, salt)` | Hashes a string using scrypt |
|
|
228
|
+
| `comparePassword(options)` | Prevents timing attacks while verifying passwords |
|
|
229
|
+
| `generateSalt()` | Generates a cryptographic salt for password hashing |
|
|
230
|
+
|
|
231
|
+
## Why Swift Auth?
|
|
232
|
+
|
|
233
|
+
- **Next.js 15+ Optimized**: Fully compatible with the new asynchronous `cookies()` API
|
|
234
|
+
- **BigInt & Date Handling**: Built-in serialization for complex JavaScript objects that standard JSON fails to handle
|
|
235
|
+
- **Automatic Secure Cookies**: Intelligently sets `secure: true` in production and `secure: false` for localhost development
|
|
236
|
+
- **Minimized Payload**: By defining `sessionFields`, you control exactly what data lives in your Redis RAM, keeping costs low and performance high
|
|
237
|
+
- **Type-Safe**: Full TypeScript support with generics for your user type
|
|
238
|
+
- **Zero Dependencies**: Uses only Node.js built-ins for cryptography
|
|
239
|
+
|
|
240
|
+
## Troubleshooting
|
|
241
|
+
|
|
242
|
+
### "Redis connection failed"
|
|
243
|
+
|
|
244
|
+
- Verify your `REDIS_URL` and `REDIS_TOKEN` environment variables
|
|
245
|
+
- Check that your Upstash Redis instance is active
|
|
246
|
+
- Ensure your Node.js version is 18 or higher
|
|
247
|
+
|
|
248
|
+
### "Session not found"
|
|
249
|
+
|
|
250
|
+
- Confirm the user has a valid cookie set
|
|
251
|
+
- Check that the Redis TTL hasn't expired
|
|
252
|
+
- Verify that `sessionFields` includes the fields you're trying to access
|
|
253
|
+
|
|
254
|
+
### Type errors with custom User type
|
|
255
|
+
|
|
256
|
+
- Ensure your `User` type is exported from your auth module
|
|
257
|
+
- Import the `User` type in components: `import type { User } from "@/lib/auth"`
|
|
258
|
+
|
|
259
|
+
## License
|
|
260
|
+
|
|
261
|
+
MIT © Taimoor Safdar
|