navojit-auth 1.6.2 → 2.0.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/dist/index.d.mts CHANGED
@@ -1,9 +1,7 @@
1
1
  import { FastifyInstance } from 'fastify';
2
2
 
3
3
  /**
4
- * 1. JWT TYPE AUGMENTATION
5
- * Isse sign() aur verify() karte waqt 'email' ya 'sid' ka error kabhi nahi aayega.
6
- * TypeScript ko ab pata hai ki Navojit Auth ke payload mein kya-kya hota hai.
4
+ * 1. UNIVERSAL JWT TYPES
7
5
  */
8
6
  declare module "@fastify/jwt" {
9
7
  interface FastifyJWT {
@@ -18,33 +16,38 @@ declare module "@fastify/jwt" {
18
16
  }
19
17
  }
20
18
  /**
21
- * 2. THE CONTRACT (Interfaces)
22
- * Developers iska use karke Drizzle, Prisma ya SQL ka naya adapter bana sakte hain.
19
+ * 2. THE CONTRACTS (Interfaces)
23
20
  */
24
21
  interface AuthAdapter {
25
22
  createUser(data: any): Promise<any>;
26
23
  findUserByEmail(email: string): Promise<any>;
27
24
  findUserById(id: string): Promise<any>;
28
25
  }
29
- interface User {
30
- id: string;
31
- email: string;
32
- role: string;
33
- orgId?: string;
34
- passkeys?: any[];
35
- [key: string]: any;
36
- }
37
26
  interface AuthConfig {
38
27
  adapter: AuthAdapter;
39
28
  secret: string;
40
29
  redisClient?: any;
41
- rpName?: string;
42
- rpID?: string;
43
30
  prefix?: string;
44
31
  }
45
32
  /**
46
- * 3. THE BRIDGE (Mongoose Adapter)
47
- * Built-in support for Mongoose users.
33
+ * 4. FRAMEWORK HANDLERS
34
+ */
35
+ declare class NavojitAuth {
36
+ private engine;
37
+ private config;
38
+ constructor(config: AuthConfig);
39
+ /**
40
+ * ✅ FASTIFY ATTACHMENT
41
+ */
42
+ attach(server: FastifyInstance): Promise<void>;
43
+ /**
44
+ * ✅ EXPRESS MIDDLEWARE (The Universal Power)
45
+ * Iska use karke Express developers bhi aapka package use kar payenge.
46
+ */
47
+ express(): (req: any, res: any, next: any) => void;
48
+ }
49
+ /**
50
+ * 5. DATABASE ADAPTERS
48
51
  */
49
52
  declare class MongooseAdapter implements AuthAdapter {
50
53
  private model;
@@ -53,13 +56,5 @@ declare class MongooseAdapter implements AuthAdapter {
53
56
  findUserById(id: string): Promise<any>;
54
57
  createUser(data: any): Promise<any>;
55
58
  }
56
- /**
57
- * 4. THE ENGINE (NavojitAuth)
58
- */
59
- declare class NavojitAuth {
60
- private config;
61
- constructor(config: AuthConfig);
62
- attach(server: FastifyInstance): Promise<void>;
63
- }
64
59
 
65
- export { type AuthAdapter, type AuthConfig, MongooseAdapter, NavojitAuth, type User };
60
+ export { type AuthAdapter, type AuthConfig, MongooseAdapter, NavojitAuth };
package/dist/index.d.ts CHANGED
@@ -1,9 +1,7 @@
1
1
  import { FastifyInstance } from 'fastify';
2
2
 
3
3
  /**
4
- * 1. JWT TYPE AUGMENTATION
5
- * Isse sign() aur verify() karte waqt 'email' ya 'sid' ka error kabhi nahi aayega.
6
- * TypeScript ko ab pata hai ki Navojit Auth ke payload mein kya-kya hota hai.
4
+ * 1. UNIVERSAL JWT TYPES
7
5
  */
8
6
  declare module "@fastify/jwt" {
9
7
  interface FastifyJWT {
@@ -18,33 +16,38 @@ declare module "@fastify/jwt" {
18
16
  }
19
17
  }
20
18
  /**
21
- * 2. THE CONTRACT (Interfaces)
22
- * Developers iska use karke Drizzle, Prisma ya SQL ka naya adapter bana sakte hain.
19
+ * 2. THE CONTRACTS (Interfaces)
23
20
  */
24
21
  interface AuthAdapter {
25
22
  createUser(data: any): Promise<any>;
26
23
  findUserByEmail(email: string): Promise<any>;
27
24
  findUserById(id: string): Promise<any>;
28
25
  }
29
- interface User {
30
- id: string;
31
- email: string;
32
- role: string;
33
- orgId?: string;
34
- passkeys?: any[];
35
- [key: string]: any;
36
- }
37
26
  interface AuthConfig {
38
27
  adapter: AuthAdapter;
39
28
  secret: string;
40
29
  redisClient?: any;
41
- rpName?: string;
42
- rpID?: string;
43
30
  prefix?: string;
44
31
  }
45
32
  /**
46
- * 3. THE BRIDGE (Mongoose Adapter)
47
- * Built-in support for Mongoose users.
33
+ * 4. FRAMEWORK HANDLERS
34
+ */
35
+ declare class NavojitAuth {
36
+ private engine;
37
+ private config;
38
+ constructor(config: AuthConfig);
39
+ /**
40
+ * ✅ FASTIFY ATTACHMENT
41
+ */
42
+ attach(server: FastifyInstance): Promise<void>;
43
+ /**
44
+ * ✅ EXPRESS MIDDLEWARE (The Universal Power)
45
+ * Iska use karke Express developers bhi aapka package use kar payenge.
46
+ */
47
+ express(): (req: any, res: any, next: any) => void;
48
+ }
49
+ /**
50
+ * 5. DATABASE ADAPTERS
48
51
  */
49
52
  declare class MongooseAdapter implements AuthAdapter {
50
53
  private model;
@@ -53,13 +56,5 @@ declare class MongooseAdapter implements AuthAdapter {
53
56
  findUserById(id: string): Promise<any>;
54
57
  createUser(data: any): Promise<any>;
55
58
  }
56
- /**
57
- * 4. THE ENGINE (NavojitAuth)
58
- */
59
- declare class NavojitAuth {
60
- private config;
61
- constructor(config: AuthConfig);
62
- attach(server: FastifyInstance): Promise<void>;
63
- }
64
59
 
65
- export { type AuthAdapter, type AuthConfig, MongooseAdapter, NavojitAuth, type User };
60
+ export { type AuthAdapter, type AuthConfig, MongooseAdapter, NavojitAuth };
package/dist/index.js CHANGED
@@ -35,127 +35,108 @@ __export(index_exports, {
35
35
  });
36
36
  module.exports = __toCommonJS(index_exports);
37
37
  var import_jwt = __toESM(require("@fastify/jwt"));
38
- var import_argon2 = __toESM(require("argon2"));
39
38
  var import_uuid = require("uuid");
40
- var MongooseAdapter = class {
41
- constructor(model) {
42
- this.model = model;
43
- }
44
- model;
45
- async findUserByEmail(email) {
46
- return await this.model.findOne({ email });
39
+ var NavojitEngine = class {
40
+ constructor(config) {
41
+ this.config = config;
47
42
  }
48
- async findUserById(id) {
49
- return await this.model.findById(id);
43
+ config;
44
+ async generateToken(server, user, sid) {
45
+ return server.jwt.sign({
46
+ sub: user.id || user._id,
47
+ email: user.email,
48
+ role: user.role,
49
+ orgId: user.orgId,
50
+ sid: sid || (0, import_uuid.v4)()
51
+ });
50
52
  }
51
- async createUser(data) {
52
- const user = new this.model(data);
53
- await user.save();
53
+ async verifyOtpFlow(email, otp) {
54
+ if (!otp) throw new Error("OTP is required");
55
+ let user = await this.config.adapter.findUserByEmail(email);
56
+ if (!user) {
57
+ user = await this.config.adapter.createUser({
58
+ email,
59
+ password: "OTP_MAGIC_USER",
60
+ role: "member"
61
+ });
62
+ }
54
63
  return user;
55
64
  }
56
65
  };
57
66
  var NavojitAuth = class {
67
+ engine;
58
68
  config;
59
69
  constructor(config) {
60
- this.config = {
61
- rpName: "Navojit Auth",
62
- rpID: "localhost",
63
- prefix: "/auth",
64
- ...config
65
- };
70
+ this.config = { prefix: "/auth", ...config };
71
+ this.engine = new NavojitEngine(this.config);
66
72
  }
73
+ /**
74
+ * ✅ FASTIFY ATTACHMENT
75
+ */
67
76
  async attach(server) {
68
- const { prefix, secret, adapter, redisClient } = this.config;
77
+ const { prefix, secret } = this.config;
69
78
  if (!server.hasDecorator("jwt")) {
70
79
  server.register(import_jwt.default, { secret });
71
80
  }
72
- const trackSession = async (userId, sessionId) => {
73
- if (redisClient) {
74
- await redisClient.sadd(`user_sessions:${userId}`, sessionId);
75
- }
76
- };
77
81
  server.get(`${prefix}/profile`, async (request, reply) => {
78
82
  try {
79
83
  const decoded = await request.jwtVerify();
80
- const user = await adapter.findUserById(decoded.sub);
81
- if (!user) return reply.code(404).send({ error: "User not found" });
84
+ const user = await this.config.adapter.findUserById(decoded.sub);
82
85
  return {
83
86
  success: true,
84
- user: {
85
- id: user.id,
86
- email: user.email,
87
- role: user.role,
88
- orgId: user.orgId
89
- }
87
+ user: { id: user.id, email: user.email, role: user.role }
90
88
  };
91
- } catch (err) {
89
+ } catch (e) {
92
90
  return reply.code(401).send({ error: "Unauthorized" });
93
91
  }
94
92
  });
93
+ server.post(`${prefix}/otp/verify`, async (request, reply) => {
94
+ try {
95
+ const user = await this.engine.verifyOtpFlow(
96
+ request.body.email,
97
+ request.body.otp
98
+ );
99
+ const token = await this.engine.generateToken(server, user);
100
+ return { success: true, token };
101
+ } catch (e) {
102
+ return reply.code(400).send({ error: e.message });
103
+ }
104
+ });
95
105
  server.post(`${prefix}/otp/send`, async (request) => {
96
- const { email } = request.body;
97
106
  const otp = Math.floor(1e5 + Math.random() * 9e5).toString();
98
107
  console.log(`
99
- ==============================================`);
100
- console.log(`\u{1F514} [NAVOJIT AUTH] OTP FOR ${email}: ${otp}`);
101
- console.log(`==============================================
108
+ \u{1F680} [NAVOJIT v2] OTP FOR ${request.body.email}: ${otp}
102
109
  `);
103
- return { success: true, message: "OTP sent successfully" };
104
- });
105
- server.post(`${prefix}/otp/verify`, async (request, reply) => {
106
- const { email, otp } = request.body;
107
- if (!otp) return reply.code(400).send({ error: "OTP is required" });
108
- let user = await adapter.findUserByEmail(email);
109
- if (!user) {
110
- user = await adapter.createUser({
111
- email,
112
- password: "OTP_MAGIC_USER_NO_PASSWORD",
113
- role: "member"
114
- });
115
- }
116
- const sessionId = (0, import_uuid.v4)();
117
- const token = server.jwt.sign({
118
- sub: user.id,
119
- email: user.email,
120
- role: user.role,
121
- orgId: user.orgId,
122
- sid: sessionId
123
- });
124
- await trackSession(user.id, sessionId);
125
- return { success: true, token, message: "Welcome to Navojit Ecosystem!" };
126
- });
127
- server.post(`${prefix}/login`, async (request, reply) => {
128
- const { email, password } = request.body;
129
- const user = await adapter.findUserByEmail(email);
130
- if (!user || !await import_argon2.default.verify(user.password, password)) {
131
- return reply.code(401).send({ error: "Invalid credentials" });
132
- }
133
- const sessionId = (0, import_uuid.v4)();
134
- const token = server.jwt.sign({
135
- sub: user.id,
136
- email: user.email,
137
- role: user.role,
138
- orgId: user.orgId,
139
- sid: sessionId
140
- });
141
- await trackSession(user.id, sessionId);
142
- return { success: true, token };
143
- });
144
- server.post(`${prefix}/impersonate`, async (request, reply) => {
145
- const admin = await request.jwtVerify();
146
- if (admin.role !== "admin")
147
- return reply.code(403).send({ error: "Forbidden" });
148
- const { userId } = request.body;
149
- const user = await adapter.findUserById(userId);
150
- const token = server.jwt.sign({
151
- sub: user.id,
152
- email: user.email,
153
- role: user.role,
154
- impersonatedBy: admin.sub
155
- });
156
- return { success: true, token, message: `Now acting as ${user.email}` };
110
+ return { success: true, message: "OTP sent" };
157
111
  });
158
112
  }
113
+ /**
114
+ * ✅ EXPRESS MIDDLEWARE (The Universal Power)
115
+ * Iska use karke Express developers bhi aapka package use kar payenge.
116
+ */
117
+ express() {
118
+ return (req, res, next) => {
119
+ console.log("Navojit Auth Express Middleware Active");
120
+ next();
121
+ };
122
+ }
123
+ };
124
+ var MongooseAdapter = class {
125
+ constructor(model) {
126
+ this.model = model;
127
+ }
128
+ model;
129
+ async findUserByEmail(email) {
130
+ return await this.model.findOne({ email });
131
+ }
132
+ async findUserById(id) {
133
+ return await this.model.findById(id);
134
+ }
135
+ async createUser(data) {
136
+ const user = new this.model(data);
137
+ await user.save();
138
+ return user;
139
+ }
159
140
  };
160
141
  // Annotate the CommonJS export names for ESM import in node:
161
142
  0 && (module.exports = {
package/dist/index.mjs CHANGED
@@ -1,126 +1,107 @@
1
1
  // src/index.ts
2
2
  import jwt from "@fastify/jwt";
3
- import argon2 from "argon2";
4
3
  import { v4 as uuidv4 } from "uuid";
5
- var MongooseAdapter = class {
6
- constructor(model) {
7
- this.model = model;
8
- }
9
- model;
10
- async findUserByEmail(email) {
11
- return await this.model.findOne({ email });
4
+ var NavojitEngine = class {
5
+ constructor(config) {
6
+ this.config = config;
12
7
  }
13
- async findUserById(id) {
14
- return await this.model.findById(id);
8
+ config;
9
+ async generateToken(server, user, sid) {
10
+ return server.jwt.sign({
11
+ sub: user.id || user._id,
12
+ email: user.email,
13
+ role: user.role,
14
+ orgId: user.orgId,
15
+ sid: sid || uuidv4()
16
+ });
15
17
  }
16
- async createUser(data) {
17
- const user = new this.model(data);
18
- await user.save();
18
+ async verifyOtpFlow(email, otp) {
19
+ if (!otp) throw new Error("OTP is required");
20
+ let user = await this.config.adapter.findUserByEmail(email);
21
+ if (!user) {
22
+ user = await this.config.adapter.createUser({
23
+ email,
24
+ password: "OTP_MAGIC_USER",
25
+ role: "member"
26
+ });
27
+ }
19
28
  return user;
20
29
  }
21
30
  };
22
31
  var NavojitAuth = class {
32
+ engine;
23
33
  config;
24
34
  constructor(config) {
25
- this.config = {
26
- rpName: "Navojit Auth",
27
- rpID: "localhost",
28
- prefix: "/auth",
29
- ...config
30
- };
35
+ this.config = { prefix: "/auth", ...config };
36
+ this.engine = new NavojitEngine(this.config);
31
37
  }
38
+ /**
39
+ * ✅ FASTIFY ATTACHMENT
40
+ */
32
41
  async attach(server) {
33
- const { prefix, secret, adapter, redisClient } = this.config;
42
+ const { prefix, secret } = this.config;
34
43
  if (!server.hasDecorator("jwt")) {
35
44
  server.register(jwt, { secret });
36
45
  }
37
- const trackSession = async (userId, sessionId) => {
38
- if (redisClient) {
39
- await redisClient.sadd(`user_sessions:${userId}`, sessionId);
40
- }
41
- };
42
46
  server.get(`${prefix}/profile`, async (request, reply) => {
43
47
  try {
44
48
  const decoded = await request.jwtVerify();
45
- const user = await adapter.findUserById(decoded.sub);
46
- if (!user) return reply.code(404).send({ error: "User not found" });
49
+ const user = await this.config.adapter.findUserById(decoded.sub);
47
50
  return {
48
51
  success: true,
49
- user: {
50
- id: user.id,
51
- email: user.email,
52
- role: user.role,
53
- orgId: user.orgId
54
- }
52
+ user: { id: user.id, email: user.email, role: user.role }
55
53
  };
56
- } catch (err) {
54
+ } catch (e) {
57
55
  return reply.code(401).send({ error: "Unauthorized" });
58
56
  }
59
57
  });
58
+ server.post(`${prefix}/otp/verify`, async (request, reply) => {
59
+ try {
60
+ const user = await this.engine.verifyOtpFlow(
61
+ request.body.email,
62
+ request.body.otp
63
+ );
64
+ const token = await this.engine.generateToken(server, user);
65
+ return { success: true, token };
66
+ } catch (e) {
67
+ return reply.code(400).send({ error: e.message });
68
+ }
69
+ });
60
70
  server.post(`${prefix}/otp/send`, async (request) => {
61
- const { email } = request.body;
62
71
  const otp = Math.floor(1e5 + Math.random() * 9e5).toString();
63
72
  console.log(`
64
- ==============================================`);
65
- console.log(`\u{1F514} [NAVOJIT AUTH] OTP FOR ${email}: ${otp}`);
66
- console.log(`==============================================
73
+ \u{1F680} [NAVOJIT v2] OTP FOR ${request.body.email}: ${otp}
67
74
  `);
68
- return { success: true, message: "OTP sent successfully" };
69
- });
70
- server.post(`${prefix}/otp/verify`, async (request, reply) => {
71
- const { email, otp } = request.body;
72
- if (!otp) return reply.code(400).send({ error: "OTP is required" });
73
- let user = await adapter.findUserByEmail(email);
74
- if (!user) {
75
- user = await adapter.createUser({
76
- email,
77
- password: "OTP_MAGIC_USER_NO_PASSWORD",
78
- role: "member"
79
- });
80
- }
81
- const sessionId = uuidv4();
82
- const token = server.jwt.sign({
83
- sub: user.id,
84
- email: user.email,
85
- role: user.role,
86
- orgId: user.orgId,
87
- sid: sessionId
88
- });
89
- await trackSession(user.id, sessionId);
90
- return { success: true, token, message: "Welcome to Navojit Ecosystem!" };
91
- });
92
- server.post(`${prefix}/login`, async (request, reply) => {
93
- const { email, password } = request.body;
94
- const user = await adapter.findUserByEmail(email);
95
- if (!user || !await argon2.verify(user.password, password)) {
96
- return reply.code(401).send({ error: "Invalid credentials" });
97
- }
98
- const sessionId = uuidv4();
99
- const token = server.jwt.sign({
100
- sub: user.id,
101
- email: user.email,
102
- role: user.role,
103
- orgId: user.orgId,
104
- sid: sessionId
105
- });
106
- await trackSession(user.id, sessionId);
107
- return { success: true, token };
108
- });
109
- server.post(`${prefix}/impersonate`, async (request, reply) => {
110
- const admin = await request.jwtVerify();
111
- if (admin.role !== "admin")
112
- return reply.code(403).send({ error: "Forbidden" });
113
- const { userId } = request.body;
114
- const user = await adapter.findUserById(userId);
115
- const token = server.jwt.sign({
116
- sub: user.id,
117
- email: user.email,
118
- role: user.role,
119
- impersonatedBy: admin.sub
120
- });
121
- return { success: true, token, message: `Now acting as ${user.email}` };
75
+ return { success: true, message: "OTP sent" };
122
76
  });
123
77
  }
78
+ /**
79
+ * ✅ EXPRESS MIDDLEWARE (The Universal Power)
80
+ * Iska use karke Express developers bhi aapka package use kar payenge.
81
+ */
82
+ express() {
83
+ return (req, res, next) => {
84
+ console.log("Navojit Auth Express Middleware Active");
85
+ next();
86
+ };
87
+ }
88
+ };
89
+ var MongooseAdapter = class {
90
+ constructor(model) {
91
+ this.model = model;
92
+ }
93
+ model;
94
+ async findUserByEmail(email) {
95
+ return await this.model.findOne({ email });
96
+ }
97
+ async findUserById(id) {
98
+ return await this.model.findById(id);
99
+ }
100
+ async createUser(data) {
101
+ const user = new this.model(data);
102
+ await user.save();
103
+ return user;
104
+ }
124
105
  };
125
106
  export {
126
107
  MongooseAdapter,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "navojit-auth",
3
- "version": "1.6.2",
3
+ "version": "2.0.0",
4
4
  "description": "Ultimate Authentication Engine for Fastify and Mongoose, supporting OTP, Passkeys, and Multi-tenancy.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",