@soroi/auth 0.0.0-stage → 0.1.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/AGENTS.md +154 -0
- package/LICENSE +21 -0
- package/README.md +26 -2
- package/dist/account.d.ts +52 -0
- package/dist/account.js +177 -0
- package/dist/adapters/next.d.ts +6 -0
- package/dist/adapters/next.js +35 -0
- package/dist/adapters/tanstack.d.ts +12 -0
- package/dist/adapters/tanstack.js +40 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +76 -0
- package/dist/core.d.ts +122 -0
- package/dist/core.js +351 -0
- package/dist/crypto.d.ts +12 -0
- package/dist/crypto.js +69 -0
- package/dist/db.d.ts +29 -0
- package/dist/db.js +55 -0
- package/dist/email.d.ts +9 -0
- package/dist/email.js +64 -0
- package/dist/errors.d.ts +24 -0
- package/dist/errors.js +88 -0
- package/dist/index.d.ts +202 -0
- package/dist/index.js +255 -0
- package/dist/methods.d.ts +16 -0
- package/dist/methods.js +21 -0
- package/dist/migrations.d.ts +5 -0
- package/dist/migrations.js +147 -0
- package/dist/modules/oauth-server.d.ts +110 -0
- package/dist/modules/oauth-server.js +568 -0
- package/dist/modules/tokens.d.ts +31 -0
- package/dist/modules/tokens.js +107 -0
- package/dist/modules/two-factor.d.ts +41 -0
- package/dist/modules/two-factor.js +169 -0
- package/dist/modules/workspaces.d.ts +138 -0
- package/dist/modules/workspaces.js +295 -0
- package/dist/oauth/flow.d.ts +5 -0
- package/dist/oauth/flow.js +161 -0
- package/dist/oauth/providers.d.ts +29 -0
- package/dist/oauth/providers.js +124 -0
- package/dist/password-hash.d.ts +8 -0
- package/dist/password-hash.js +68 -0
- package/dist/password.d.ts +49 -0
- package/dist/password.js +181 -0
- package/dist/routes.d.ts +5 -0
- package/dist/routes.js +77 -0
- package/dist/sessions.d.ts +51 -0
- package/dist/sessions.js +184 -0
- package/dist/testing.d.ts +15 -0
- package/dist/testing.js +32 -0
- package/dist/types.d.ts +184 -0
- package/dist/types.js +1 -0
- package/package.json +75 -4
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import type { Db } from "./db.js";
|
|
2
|
+
export type User = {
|
|
3
|
+
id: string;
|
|
4
|
+
email: string;
|
|
5
|
+
emailVerified: boolean;
|
|
6
|
+
name: string | null;
|
|
7
|
+
image: string | null;
|
|
8
|
+
hasPassword: boolean;
|
|
9
|
+
createdAt: Date;
|
|
10
|
+
};
|
|
11
|
+
export type SignInMethod = "password" | "google" | "github" | "email_link" | "dev" | "token";
|
|
12
|
+
export type Session = {
|
|
13
|
+
id: string;
|
|
14
|
+
userId: string;
|
|
15
|
+
method: SignInMethod;
|
|
16
|
+
workspaceId: string | null;
|
|
17
|
+
ip: string | null;
|
|
18
|
+
userAgent: string | null;
|
|
19
|
+
createdAt: Date;
|
|
20
|
+
lastSeenAt: Date;
|
|
21
|
+
expiresAt: Date;
|
|
22
|
+
/** The verified e-mail that Google or GitHub gave at this sign-in. Null for other methods. */
|
|
23
|
+
providerEmail: string | null;
|
|
24
|
+
};
|
|
25
|
+
export type CookieOptions = {
|
|
26
|
+
httpOnly: boolean;
|
|
27
|
+
secure: boolean;
|
|
28
|
+
sameSite: "lax" | "strict";
|
|
29
|
+
path: string;
|
|
30
|
+
maxAge: number;
|
|
31
|
+
domain?: string;
|
|
32
|
+
};
|
|
33
|
+
/** Connects the package to a framework, so calls do not need the request passed in. */
|
|
34
|
+
export interface FrameworkAdapter {
|
|
35
|
+
/** The current request. May be async (Next.js builds it from `headers()`). Return the same object within one request. */
|
|
36
|
+
getRequest(): Request | Promise<Request>;
|
|
37
|
+
/** Return `false` when cookies cannot be set in this context (a Next.js server component). */
|
|
38
|
+
setCookie(name: string, value: string, options: CookieOptions): void | boolean | Promise<void | boolean>;
|
|
39
|
+
/** Throws the framework's redirect. */
|
|
40
|
+
redirect(to: string): never;
|
|
41
|
+
/** The socket address, used when no proxy header gives the client IP. */
|
|
42
|
+
getIP?(): string | null;
|
|
43
|
+
}
|
|
44
|
+
export type OAuthProviderId = "google" | "github";
|
|
45
|
+
export type OAuthMethod = {
|
|
46
|
+
kind: "oauth";
|
|
47
|
+
id: OAuthProviderId;
|
|
48
|
+
clientId: string;
|
|
49
|
+
clientSecret: string;
|
|
50
|
+
scopes?: string[];
|
|
51
|
+
/** Google only. Default "select_account". */
|
|
52
|
+
prompt?: string;
|
|
53
|
+
};
|
|
54
|
+
export type PasswordMethod = {
|
|
55
|
+
kind: "password";
|
|
56
|
+
/** Users must open the link in a verification e-mail before they can sign in. Default true. */
|
|
57
|
+
verifyEmail: boolean;
|
|
58
|
+
minLength: number;
|
|
59
|
+
/** Verifies hashes from another system (bcrypt, scrypt). Matching passwords are rehashed with argon2id. */
|
|
60
|
+
legacyVerify?: (hash: string, password: string) => boolean | Promise<boolean>;
|
|
61
|
+
};
|
|
62
|
+
export type Method = OAuthMethod | PasswordMethod;
|
|
63
|
+
/** Who may sign up and sign in. A function gets a lower-case e-mail. */
|
|
64
|
+
export type Access = "open" | "closed" | ((email: string) => boolean | Promise<boolean>);
|
|
65
|
+
export type EmailMessage = {
|
|
66
|
+
from: string;
|
|
67
|
+
to: string;
|
|
68
|
+
subject: string;
|
|
69
|
+
html: string;
|
|
70
|
+
text: string;
|
|
71
|
+
};
|
|
72
|
+
export type EmailContent = {
|
|
73
|
+
subject: string;
|
|
74
|
+
html: string;
|
|
75
|
+
text: string;
|
|
76
|
+
};
|
|
77
|
+
export type TemplateData = {
|
|
78
|
+
appName: string;
|
|
79
|
+
url: string;
|
|
80
|
+
email: string;
|
|
81
|
+
name: string | null;
|
|
82
|
+
};
|
|
83
|
+
export type InviteTemplateData = TemplateData & {
|
|
84
|
+
workspace: string;
|
|
85
|
+
invitedBy: string | null;
|
|
86
|
+
role: string;
|
|
87
|
+
};
|
|
88
|
+
export type Templates = {
|
|
89
|
+
verifyEmail(data: TemplateData): EmailContent;
|
|
90
|
+
resetPassword(data: TemplateData): EmailContent;
|
|
91
|
+
changeEmail(data: TemplateData): EmailContent;
|
|
92
|
+
emailChanged(data: TemplateData & {
|
|
93
|
+
newEmail: string;
|
|
94
|
+
}): EmailContent;
|
|
95
|
+
passwordChanged(data: Omit<TemplateData, "url">): EmailContent;
|
|
96
|
+
accountExists(data: TemplateData): EmailContent;
|
|
97
|
+
accountLinked(data: Omit<TemplateData, "url"> & {
|
|
98
|
+
provider: string;
|
|
99
|
+
}): EmailContent;
|
|
100
|
+
tokenCreated(data: Omit<TemplateData, "url"> & {
|
|
101
|
+
tokenName: string;
|
|
102
|
+
}): EmailContent;
|
|
103
|
+
invite(data: InviteTemplateData): EmailContent;
|
|
104
|
+
};
|
|
105
|
+
export type EmailConfig = {
|
|
106
|
+
from: string;
|
|
107
|
+
send(message: EmailMessage): Promise<void>;
|
|
108
|
+
templates?: Partial<Templates>;
|
|
109
|
+
};
|
|
110
|
+
/** App pages. The package never renders a page; it links and redirects to these. */
|
|
111
|
+
export type Pages = {
|
|
112
|
+
signIn: string;
|
|
113
|
+
afterSignIn: string;
|
|
114
|
+
afterSignOut: string;
|
|
115
|
+
/** Errors from redirect flows arrive here as `?error=<code>`. Default: `signIn`. */
|
|
116
|
+
error: string;
|
|
117
|
+
/** Where the verify link lands after it signs the user in. Default: `afterSignIn`. */
|
|
118
|
+
verified: string;
|
|
119
|
+
/** The app's "new password" form. The reset e-mail links here with `?token=`. */
|
|
120
|
+
resetPassword: string;
|
|
121
|
+
/** The app's code form for two-factor sign-in. */
|
|
122
|
+
twoFactor: string;
|
|
123
|
+
/** The app's invite page. The invite e-mail links here with `?token=`. */
|
|
124
|
+
acceptInvite: string;
|
|
125
|
+
};
|
|
126
|
+
export type WorkspacesConfig<R extends readonly [string, ...string[]]> = {
|
|
127
|
+
/** Highest first. The first role is the owner. */
|
|
128
|
+
roles: R;
|
|
129
|
+
/** Lowest role that may invite, rename and manage members. Default: the second role when there are 3 or more. */
|
|
130
|
+
manage?: R[number];
|
|
131
|
+
};
|
|
132
|
+
export type AuthConfig = {
|
|
133
|
+
/** The app's public URL, e.g. `https://leads.kaarel.dev`. */
|
|
134
|
+
url: string;
|
|
135
|
+
/** At least 32 characters. Generate one with `openssl rand -base64 32`. */
|
|
136
|
+
secret: string;
|
|
137
|
+
/** A database URL (Bun only) or an adapter: `pg(pool)`, `postgresJs(sql)`, `bunSql(sql)`. */
|
|
138
|
+
db: Db | string;
|
|
139
|
+
methods: Method[];
|
|
140
|
+
appName?: string;
|
|
141
|
+
framework?: FrameworkAdapter;
|
|
142
|
+
access?: Access;
|
|
143
|
+
/**
|
|
144
|
+
* How auth e-mails go out. `false` turns every e-mail flow off: sign-up with verification, password reset,
|
|
145
|
+
* e-mail change and invites answer `email_unavailable`, and notices are not sent.
|
|
146
|
+
* Without it, e-mails print to the console outside production, and the flows are off in production.
|
|
147
|
+
*/
|
|
148
|
+
email?: EmailConfig | false;
|
|
149
|
+
pages?: Partial<Pages>;
|
|
150
|
+
/** Postgres schema for the auth tables. Default "auth". */
|
|
151
|
+
schema?: string;
|
|
152
|
+
/** Mount path of `auth.routes`. Default "/auth". */
|
|
153
|
+
basePath?: string;
|
|
154
|
+
/** Default 365 days, at most 400. A session used on a new day gets a full new period. */
|
|
155
|
+
session?: {
|
|
156
|
+
days?: number;
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* `domain` shares the session with subdomains, e.g. "soroi.dev" for every app on *.soroi.dev.
|
|
160
|
+
* Apps that share it must also share `db`, `schema` and `cookie.name`.
|
|
161
|
+
*/
|
|
162
|
+
cookie?: {
|
|
163
|
+
name?: string;
|
|
164
|
+
domain?: string;
|
|
165
|
+
};
|
|
166
|
+
/** Extra origins allowed to call sign-in and other changes (besides `url`). `?next=` may also point to them. */
|
|
167
|
+
trustedOrigins?: string[];
|
|
168
|
+
/** The client IP for rate limits. Default: the last `x-forwarded-for` entry, then `x-real-ip`. */
|
|
169
|
+
ip?: (request: Request) => string | null;
|
|
170
|
+
rateLimit?: boolean;
|
|
171
|
+
/** "auto" applies migrations on first use. Default "auto". */
|
|
172
|
+
migrate?: "auto" | "manual";
|
|
173
|
+
/** Dev only: `/auth/dev` signs in as this e-mail. Ignored in production and on public hosts. */
|
|
174
|
+
devLogin?: string;
|
|
175
|
+
twoFactor?: boolean | {
|
|
176
|
+
issuer?: string;
|
|
177
|
+
};
|
|
178
|
+
workspaces?: WorkspacesConfig<readonly [string, ...string[]]>;
|
|
179
|
+
tokens?: boolean | {
|
|
180
|
+
prefix?: string;
|
|
181
|
+
};
|
|
182
|
+
/** Runs in the same transaction as the user insert. Throw to cancel the sign-up. */
|
|
183
|
+
onUserCreated?: (user: User, tx: Db) => Promise<void>;
|
|
184
|
+
};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,77 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soroi/auth",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Framework-agnostic, server-only auth for TypeScript apps. No UI, no client SDK, no runtime dependencies.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "https://auth.kaarel.dev",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/soroistack/soroi.git",
|
|
10
|
+
"directory": "packages/auth/packages/auth"
|
|
11
|
+
},
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=24.7",
|
|
18
|
+
"bun": ">=1.2"
|
|
19
|
+
},
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"default": "./dist/index.js"
|
|
24
|
+
},
|
|
25
|
+
"./tanstack": {
|
|
26
|
+
"types": "./dist/adapters/tanstack.d.ts",
|
|
27
|
+
"default": "./dist/adapters/tanstack.js"
|
|
28
|
+
},
|
|
29
|
+
"./next": {
|
|
30
|
+
"types": "./dist/adapters/next.d.ts",
|
|
31
|
+
"default": "./dist/adapters/next.js"
|
|
32
|
+
},
|
|
33
|
+
"./testing": {
|
|
34
|
+
"types": "./dist/testing.d.ts",
|
|
35
|
+
"default": "./dist/testing.js"
|
|
36
|
+
},
|
|
37
|
+
"./errors": {
|
|
38
|
+
"types": "./dist/errors.d.ts",
|
|
39
|
+
"default": "./dist/errors.js"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"bin": {
|
|
43
|
+
"soroi-auth": "./dist/cli.js"
|
|
44
|
+
},
|
|
45
|
+
"files": [
|
|
46
|
+
"dist",
|
|
47
|
+
"AGENTS.md",
|
|
48
|
+
"README.md"
|
|
49
|
+
],
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
52
|
+
"typecheck": "tsc --noEmit",
|
|
53
|
+
"test": "bun test",
|
|
54
|
+
"prepublishOnly": "bun run typecheck && bun test && bun run build"
|
|
55
|
+
},
|
|
56
|
+
"peerDependencies": {
|
|
57
|
+
"@tanstack/react-router": ">=1.131",
|
|
58
|
+
"@tanstack/react-start": ">=1.131",
|
|
59
|
+
"next": ">=15"
|
|
60
|
+
},
|
|
61
|
+
"peerDependenciesMeta": {
|
|
62
|
+
"@tanstack/react-router": {
|
|
63
|
+
"optional": true
|
|
64
|
+
},
|
|
65
|
+
"@tanstack/react-start": {
|
|
66
|
+
"optional": true
|
|
67
|
+
},
|
|
68
|
+
"next": {
|
|
69
|
+
"optional": true
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
"devDependencies": {
|
|
73
|
+
"@types/bun": "^1.3.0",
|
|
74
|
+
"@types/node": "^24.10.0",
|
|
75
|
+
"typescript": "^5.9.3"
|
|
76
|
+
}
|
|
77
|
+
}
|