@xeplr/auth 1.0.8 → 1.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/index.js +12 -0
- package/migrations/0008_super_admin.sql +58 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -267,6 +267,18 @@ var requiredEnv = [
|
|
|
267
267
|
'AUTH_PORT',
|
|
268
268
|
'AUTH_DB_NAME', // auth db (api reaches it via attach())
|
|
269
269
|
'XEPLR_AUTH_MIGRATIONS', // migrations run via migrate:up
|
|
270
|
+
|
|
271
|
+
// THE FIRST ACCOUNT — consumed by migrations/0008_super_admin.sql, which
|
|
272
|
+
// this package now ships so that every app does not hand-write the same
|
|
273
|
+
// insert against columns this package owns.
|
|
274
|
+
//
|
|
275
|
+
// Required, not optional: an install that migrates cleanly and has nobody
|
|
276
|
+
// who can sign in is not a working install, and the failure shows up much
|
|
277
|
+
// later than the cause. The migrator refuses by name if either is unset,
|
|
278
|
+
// so this listing only moves the same complaint to `check-env`, before
|
|
279
|
+
// anything starts.
|
|
280
|
+
'AUTH_SUPER_ADMIN_EMAIL',
|
|
281
|
+
'AUTH_SUPER_ADMIN_PASSWORD',
|
|
270
282
|
];
|
|
271
283
|
|
|
272
284
|
// Read at ACCESS time so it reflects the .env the app has already loaded — the
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
-- 0008_super_admin.sql
|
|
2
|
+
-- THE FIRST ACCOUNT. Every install needs one, so this package ships it rather
|
|
3
|
+
-- than leaving each app to hand-write the same SQL.
|
|
4
|
+
--
|
|
5
|
+
-- It used to be the app's job: 0006 creates the "Super Admin" ROLE but nothing
|
|
6
|
+
-- created a super admin USER, so every consumer copied a version of this file
|
|
7
|
+
-- into its own migrations-auth/. That copy restated things this package owns —
|
|
8
|
+
-- the users columns, that normalizedEmail is the lowercased one, that the role
|
|
9
|
+
-- is spelled exactly 'Super Admin', that passwords are bcrypt — so a change
|
|
10
|
+
-- here silently broke every copy out there. And it broke as "invalid email or
|
|
11
|
+
-- password", which sends somebody hunting their password rather than their
|
|
12
|
+
-- migration.
|
|
13
|
+
--
|
|
14
|
+
-- WHO is still the app's decision; only HOW lives here. Both values come from
|
|
15
|
+
-- the environment and are substituted at apply time, so the password is never
|
|
16
|
+
-- written into a file. The migrator REFUSES to run when either is unset,
|
|
17
|
+
-- naming the variable — deliberately, because the alternative is an install
|
|
18
|
+
-- that migrates cleanly and has no way to sign in.
|
|
19
|
+
--
|
|
20
|
+
-- No quotes, spaces, $ or # in the password: these are substituted into SQL
|
|
21
|
+
-- text, the same constraint every other env-substituted migration here has.
|
|
22
|
+
--
|
|
23
|
+
-- Insert-if-absent on both statements, so re-running changes nothing — and an
|
|
24
|
+
-- app that already seeded its own super admin under the same address keeps the
|
|
25
|
+
-- one it has, password included.
|
|
26
|
+
|
|
27
|
+
INSERT INTO "users" (
|
|
28
|
+
id, email, "normalizedEmail", name, pwd, "isActive", "isActivated",
|
|
29
|
+
"activatedOn", "activatedBy", "mtId1", "recordCreatedDate", "recordModifiedDate"
|
|
30
|
+
)
|
|
31
|
+
SELECT
|
|
32
|
+
encode(gen_random_bytes(12), 'hex'),
|
|
33
|
+
'${AUTH_SUPER_ADMIN_EMAIL}',
|
|
34
|
+
lower('${AUTH_SUPER_ADMIN_EMAIL}'),
|
|
35
|
+
'Super Admin',
|
|
36
|
+
crypt('${AUTH_SUPER_ADMIN_PASSWORD}', gen_salt('bf')),
|
|
37
|
+
true,
|
|
38
|
+
true,
|
|
39
|
+
now(),
|
|
40
|
+
'system',
|
|
41
|
+
-- '*' — belongs to every tenant rather than to one company, so the first
|
|
42
|
+
-- account can still sign in before any company exists.
|
|
43
|
+
'*',
|
|
44
|
+
now(),
|
|
45
|
+
now()
|
|
46
|
+
WHERE NOT EXISTS (
|
|
47
|
+
SELECT 1 FROM "users" WHERE "normalizedEmail" = lower('${AUTH_SUPER_ADMIN_EMAIL}')
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
INSERT INTO "userRolesMapping" (id, "userId", "roleId", "isActive", "mtId1", "recordCreatedDate", "recordModifiedDate")
|
|
51
|
+
SELECT encode(gen_random_bytes(12), 'hex'), u.id, r.id, true, '*', now(), now()
|
|
52
|
+
FROM "users" u
|
|
53
|
+
CROSS JOIN "roles" r
|
|
54
|
+
WHERE u."normalizedEmail" = lower('${AUTH_SUPER_ADMIN_EMAIL}')
|
|
55
|
+
AND r.name = 'Super Admin'
|
|
56
|
+
AND NOT EXISTS (
|
|
57
|
+
SELECT 1 FROM "userRolesMapping" m WHERE m."userId" = u.id AND m."roleId" = r.id
|
|
58
|
+
);
|