hi-secure 1.0.25 → 1.0.27
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/dist/core/HiSecure.d.ts +15 -36
- package/dist/core/HiSecure.d.ts.map +1 -1
- package/dist/core/HiSecure.js +78 -138
- package/dist/core/HiSecure.js.map +1 -1
- package/dist/core/useSecure.d.ts +4 -1
- package/dist/core/useSecure.d.ts.map +1 -1
- package/dist/core/useSecure.js +58 -13
- package/dist/core/useSecure.js.map +1 -1
- package/dist/index.d.ts +1 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -4
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/readme.md +155 -93
- package/src/core/HiSecure.ts +490 -261
- package/src/core/useSecure.ts +94 -37
- package/src/index.ts +25 -11
package/dist/index.js
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
// import { HiSecure } from "./core/HiSecure.js";
|
|
3
|
+
// import { useSecure, secureRoute } from "./core/useSecure.js";
|
|
2
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.secureRoute = exports.useSecure = exports.
|
|
5
|
+
exports.secureRoute = exports.useSecure = exports.HiSecure = exports.header = exports.param = exports.query = exports.body = exports.z = void 0;
|
|
6
|
+
// export { z } from "zod";
|
|
7
|
+
// export { body, query, param, header } from "express-validator";
|
|
8
|
+
// const hiSecure = HiSecure.getInstance();
|
|
9
|
+
// export {
|
|
10
|
+
// HiSecure,
|
|
11
|
+
// hiSecure,
|
|
12
|
+
// useSecure,
|
|
13
|
+
// secureRoute
|
|
14
|
+
// };
|
|
15
|
+
// export default hiSecure;
|
|
4
16
|
const HiSecure_js_1 = require("./core/HiSecure.js");
|
|
5
17
|
Object.defineProperty(exports, "HiSecure", { enumerable: true, get: function () { return HiSecure_js_1.HiSecure; } });
|
|
6
18
|
const useSecure_js_1 = require("./core/useSecure.js");
|
|
@@ -13,7 +25,4 @@ Object.defineProperty(exports, "body", { enumerable: true, get: function () { re
|
|
|
13
25
|
Object.defineProperty(exports, "query", { enumerable: true, get: function () { return express_validator_1.query; } });
|
|
14
26
|
Object.defineProperty(exports, "param", { enumerable: true, get: function () { return express_validator_1.param; } });
|
|
15
27
|
Object.defineProperty(exports, "header", { enumerable: true, get: function () { return express_validator_1.header; } });
|
|
16
|
-
const hiSecure = HiSecure_js_1.HiSecure.getInstance();
|
|
17
|
-
exports.hiSecure = hiSecure;
|
|
18
|
-
exports.default = hiSecure;
|
|
19
28
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,iDAAiD;AACjD,gEAAgE;;;AAGhE,2BAA2B;AAC3B,kEAAkE;AAElE,2CAA2C;AAE3C,YAAY;AACZ,wBAAwB;AACxB,wBAAwB;AACxB,wBAAwB;AACxB,wBAAwB;AACxB,KAAK;AAEL,2BAA2B;AAK3B,oDAA8C;AAQ5C,yFARO,sBAAQ,OAQP;AAPV,sDAA6D;AAQ3D,0FARO,wBAAS,OAQP;AACT,4FATkB,0BAAW,OASlB;AAPb,2BAAwB;AAAf,wFAAA,CAAC,OAAA;AACV,uDAA+D;AAAtD,yGAAA,IAAI,OAAA;AAAE,0GAAA,KAAK,OAAA;AAAE,0GAAA,KAAK,OAAA;AAAE,2GAAA,MAAM,OAAA","sourcesContent":["// import { HiSecure } from \"./core/HiSecure.js\";\r\n// import { useSecure, secureRoute } from \"./core/useSecure.js\";\r\n\r\n\r\n// export { z } from \"zod\";\r\n// export { body, query, param, header } from \"express-validator\";\r\n\r\n// const hiSecure = HiSecure.getInstance();\r\n\r\n// export { \r\n// HiSecure, \r\n// hiSecure, \r\n// useSecure, \r\n// secureRoute \r\n// };\r\n\r\n// export default hiSecure;\r\n\r\n\r\n\r\n\r\nimport { HiSecure } from \"./core/HiSecure.js\";\r\nimport { useSecure, secureRoute } from \"./core/useSecure.js\";\r\n\r\nexport { z } from \"zod\";\r\nexport { body, query, param, header } from \"express-validator\";\r\n\r\n\r\nexport {\r\n HiSecure,\r\n useSecure,\r\n secureRoute\r\n};\r\n\r\n"]}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -485,7 +485,7 @@ It covers signup, JWT login, Google login, role-based access control, and proper
|
|
|
485
485
|
<ul>
|
|
486
486
|
<li>Signup using email and password</li>
|
|
487
487
|
<li>Login using email and password (JWT-based)</li>
|
|
488
|
-
<li>Login with Google (ID token verification)</li>
|
|
488
|
+
<li>Login with Google (ID token verification) - Added Soon in Docs </li>
|
|
489
489
|
<li>Role-based protected routes</li>
|
|
490
490
|
<li>Optional authentication support</li>
|
|
491
491
|
<li>Correct HiSecure bootstrap with reset rules</li>
|
|
@@ -511,7 +511,7 @@ HiSecure.getInstance({
|
|
|
511
511
|
enabled: true,
|
|
512
512
|
jwtSecret: process.env.JWT_SECRET || "supersecret_32_chars_minimum",
|
|
513
513
|
jwtExpiresIn: "1d",
|
|
514
|
-
googleClientId: process.env.GOOGLE_CLIENT_ID
|
|
514
|
+
googleClientId: process.env.GOOGLE_CLIENT_ID // this only added if need googleLogin as well
|
|
515
515
|
}
|
|
516
516
|
});
|
|
517
517
|
|
|
@@ -560,128 +560,190 @@ export default router;
|
|
|
560
560
|
|
|
561
561
|
<h4>Signup (Email and Password)</h4>
|
|
562
562
|
|
|
563
|
-
<pre><code>
|
|
563
|
+
<pre><code>
|
|
564
|
+
import { HiSecure } from "hi-secure";
|
|
564
565
|
import { HttpError } from "../core/errors/HttpError.js";
|
|
565
566
|
import User from "../models/User.js";
|
|
566
567
|
|
|
567
|
-
export const signup = async (req, res, next) => {
|
|
568
|
-
try {
|
|
569
|
-
const { email, password, name } = req.body;
|
|
570
|
-
|
|
571
|
-
if (!email || !password) {
|
|
572
|
-
throw HttpError.BadRequest("Email and password required");
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
const existing = await User.findOne({ email });
|
|
576
|
-
if (existing) {
|
|
577
|
-
throw HttpError.Conflict("User already exists");
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
const passwordHash = await HiSecure.hash(password);
|
|
581
568
|
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
});
|
|
569
|
+
const JWT_OPTIONS = {
|
|
570
|
+
issuer: 'hi-secure-backend',
|
|
571
|
+
audience: ['web-app', 'mobile-app'],
|
|
572
|
+
expiresIn: '7d',
|
|
573
|
+
subject: 'user-authentication'
|
|
574
|
+
};
|
|
589
575
|
|
|
590
|
-
const token = HiSecure.jwt.sign({
|
|
591
|
-
userId: user.id,
|
|
592
|
-
roles: user.roles
|
|
593
|
-
});
|
|
594
576
|
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
577
|
+
exports.registerUser = async(req, res) => {
|
|
578
|
+
try {
|
|
579
|
+
const { name, email, password } = req.body;
|
|
580
|
+
|
|
581
|
+
const existingUser = await User.findOne({ email });
|
|
582
|
+
if (existingUser) {
|
|
583
|
+
return res.status(400).json({
|
|
584
|
+
error: 'User already exists'
|
|
585
|
+
});
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
const hashedPassword = await HiSecure.hash(password);
|
|
589
|
+
|
|
590
|
+
const user = await User.create({
|
|
591
|
+
name,
|
|
592
|
+
email,
|
|
593
|
+
password: hashedPassword
|
|
594
|
+
});
|
|
595
|
+
|
|
596
|
+
const token = HiSecure.jwt.sign({
|
|
597
|
+
userId: user._id.toString(),
|
|
598
|
+
email: user.email,
|
|
599
|
+
name: user.name,
|
|
600
|
+
role: 'user'
|
|
601
|
+
},
|
|
602
|
+
JWT_OPTIONS
|
|
603
|
+
);
|
|
604
|
+
|
|
605
|
+
res.status(201).json({
|
|
606
|
+
message: 'User registered successfully',
|
|
607
|
+
token,
|
|
608
|
+
user: {
|
|
609
|
+
id: user._id,
|
|
610
|
+
name: user.name,
|
|
611
|
+
email: user.email
|
|
612
|
+
}
|
|
613
|
+
});
|
|
614
|
+
|
|
615
|
+
} catch (error) {
|
|
616
|
+
console.error('Registration error:', error);
|
|
617
|
+
res.status(500).json({
|
|
618
|
+
error: 'Registration failed',
|
|
619
|
+
details: error.message
|
|
620
|
+
});
|
|
621
|
+
}
|
|
599
622
|
};
|
|
623
|
+
|
|
600
624
|
</code></pre>
|
|
601
625
|
|
|
602
626
|
<hr/>
|
|
603
627
|
|
|
604
628
|
<h4>Login (Email and Password)</h4>
|
|
605
629
|
|
|
606
|
-
<pre><code>
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
630
|
+
<pre><code>
|
|
631
|
+
|
|
632
|
+
exports.loginUser = async(req, res) => {
|
|
633
|
+
try {
|
|
634
|
+
const { email, password } = req.body;
|
|
635
|
+
|
|
636
|
+
const user = await User.findOne({ email });
|
|
637
|
+
if (!user) {
|
|
638
|
+
return res.status(401).json({
|
|
639
|
+
error: 'Invalid credentials'
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
const isValid = await HiSecure.verify(password, user.password);
|
|
644
|
+
if (!isValid) {
|
|
645
|
+
return res.status(401).json({
|
|
646
|
+
error: 'Invalid credentials'
|
|
647
|
+
});
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
const token = HiSecure.jwt.sign({
|
|
651
|
+
userId: user._id.toString(),
|
|
652
|
+
email: user.email,
|
|
653
|
+
name: user.name,
|
|
654
|
+
role: 'user'
|
|
655
|
+
},
|
|
656
|
+
JWT_OPTIONS
|
|
657
|
+
);
|
|
658
|
+
|
|
659
|
+
res.json({
|
|
660
|
+
message: 'Login successful',
|
|
661
|
+
token,
|
|
662
|
+
user: {
|
|
663
|
+
id: user._id,
|
|
664
|
+
name: user.name,
|
|
665
|
+
email: user.email
|
|
666
|
+
}
|
|
667
|
+
});
|
|
668
|
+
|
|
669
|
+
} catch (error) {
|
|
670
|
+
console.error('Login error:', error);
|
|
671
|
+
res.status(500).json({
|
|
672
|
+
error: 'Login failed',
|
|
673
|
+
details: error.message
|
|
674
|
+
});
|
|
613
675
|
}
|
|
676
|
+
};
|
|
614
677
|
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
}
|
|
678
|
+
</code></pre>
|
|
679
|
+
|
|
680
|
+
<hr/>
|
|
619
681
|
|
|
620
|
-
|
|
621
|
-
userId: user.id,
|
|
622
|
-
roles: user.roles
|
|
623
|
-
});
|
|
682
|
+
<h3>Role-Based Protected Routes</h3>
|
|
624
683
|
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
684
|
+
<pre><code>app.get(
|
|
685
|
+
"/admin",
|
|
686
|
+
HiSecure.auth({ roles: ["admin"] }),
|
|
687
|
+
(req, res) => {
|
|
688
|
+
res.json({ message: "Welcome Admin" });
|
|
628
689
|
}
|
|
629
|
-
|
|
690
|
+
);
|
|
630
691
|
</code></pre>
|
|
631
692
|
|
|
632
|
-
<hr/>
|
|
633
693
|
|
|
634
|
-
<
|
|
694
|
+
<pre>
|
|
695
|
+
<code>
|
|
696
|
+
const router = express.Router();
|
|
697
|
+
router.post(
|
|
698
|
+
'/register',
|
|
635
699
|
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
throw HttpError.BadRequest("Google idToken required");
|
|
641
|
-
}
|
|
700
|
+
HiSecure.validate([
|
|
701
|
+
body("name")
|
|
702
|
+
.notEmpty().withMessage("Name is required")
|
|
703
|
+
.isLength({ min: 3 }).withMessage("Name must be at least 3 characters"),
|
|
642
704
|
|
|
643
|
-
|
|
705
|
+
body("email")
|
|
706
|
+
.notEmpty().withMessage("Email is required")
|
|
707
|
+
.isEmail().withMessage("Invalid email format"),
|
|
644
708
|
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
709
|
+
body("password")
|
|
710
|
+
.notEmpty().withMessage("Password is required")
|
|
711
|
+
.isLength({ min: 6 }).withMessage("Password must be at least 6 characters"),
|
|
712
|
+
]),
|
|
648
713
|
|
|
649
|
-
|
|
714
|
+
registerUser
|
|
715
|
+
);
|
|
650
716
|
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
email: googleUser.email,
|
|
654
|
-
name: googleUser.name,
|
|
655
|
-
provider: "google",
|
|
656
|
-
providerId: googleUser.sub,
|
|
657
|
-
roles: ["user"]
|
|
658
|
-
});
|
|
659
|
-
}
|
|
717
|
+
router.post(
|
|
718
|
+
'/login',
|
|
660
719
|
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
720
|
+
HiSecure.validate([
|
|
721
|
+
body("email")
|
|
722
|
+
.notEmpty().withMessage("Email is required")
|
|
723
|
+
.isEmail().withMessage("Invalid email format"),
|
|
665
724
|
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
}
|
|
670
|
-
};
|
|
671
|
-
</code></pre>
|
|
725
|
+
body("password")
|
|
726
|
+
.notEmpty().withMessage("Password is required")
|
|
727
|
+
]),
|
|
672
728
|
|
|
673
|
-
|
|
729
|
+
HiSecure.rateLimit({ max: 5, windowMs: 15 * 60 * 1000 }),
|
|
674
730
|
|
|
675
|
-
|
|
731
|
+
loginUser
|
|
732
|
+
);
|
|
676
733
|
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
734
|
+
router.get(
|
|
735
|
+
'/profile',
|
|
736
|
+
HiSecure.auth({ required: true }),
|
|
737
|
+
getProfile
|
|
738
|
+
);
|
|
739
|
+
|
|
740
|
+
<!-- U can also add validator [Either zod Or express-validator] -->
|
|
741
|
+
router.post('/create', HiSecure.auth({ required: true }), createTask)
|
|
742
|
+
router.get('/get', HiSecure.auth({ required: true }), getTask)
|
|
743
|
+
router.put('/:id', HiSecure.auth({ required: true }), updateTask)
|
|
744
|
+
router.psot('/health',heatlh);
|
|
745
|
+
</code>
|
|
746
|
+
</pre>
|
|
685
747
|
|
|
686
748
|
<hr/>
|
|
687
749
|
|