@riligar/auth-elysia 1.6.3 → 1.7.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.esm.js CHANGED
@@ -20,7 +20,14 @@ const DEFAULT_CONFIG = {
20
20
  excludePaths: ['/auth/login', '/auth/register', '/auth/session'],
21
21
  onUnauthorized: set => {
22
22
  set.status = 401;
23
- return 'Sessão inválida ou expirada.'
23
+ set.headers = { ...set.headers, 'Content-Type': 'application/json' };
24
+ return {
25
+ success: false,
26
+ error: {
27
+ code: 'UNAUTHORIZED',
28
+ message: 'Sessão inválida ou expirada. Por favor, faça login novamente.',
29
+ },
30
+ }
24
31
  },
25
32
  };
26
33
 
@@ -110,12 +117,30 @@ function authPlugin(userConfig = {}) {
110
117
  .onBeforeHandle({ as: 'global' }, ({ user, set, request }) => {
111
118
  const path = new URL(request.url).pathname;
112
119
  if (config.excludePaths.some(p => path.startsWith(p))) return
113
- if (!user) return config.onUnauthorized(set)
120
+ if (!user) {
121
+ set.status = 401;
122
+ set.headers['Content-Type'] = 'application/json';
123
+ return {
124
+ success: false,
125
+ error: {
126
+ code: 'UNAUTHORIZED',
127
+ message: 'Sessão inválida ou expirada. Por favor, faça login novamente.',
128
+ },
129
+ }
130
+ }
114
131
  })
115
132
  .error(({ error, set }) => {
116
- if (String(error).includes('user.id') || String(error).includes('of null')) {
133
+ const errorStr = String(error);
134
+ if (errorStr.includes('user.id') || errorStr.includes('of null') || errorStr.includes('of undefined')) {
117
135
  set.status = 401;
118
- return 'Sessão inválida ou expirada.'
136
+ set.headers['Content-Type'] = 'application/json';
137
+ return {
138
+ success: false,
139
+ error: {
140
+ code: 'UNAUTHORIZED',
141
+ message: 'Sessão inválida ou expirada. Por favor, faça login novamente.',
142
+ },
143
+ }
119
144
  }
120
145
  })
121
146
  .group(config.prefix, app =>
package/dist/index.js CHANGED
@@ -24,7 +24,14 @@ const DEFAULT_CONFIG = {
24
24
  excludePaths: ['/auth/login', '/auth/register', '/auth/session'],
25
25
  onUnauthorized: set => {
26
26
  set.status = 401;
27
- return 'Sessão inválida ou expirada.'
27
+ set.headers = { ...set.headers, 'Content-Type': 'application/json' };
28
+ return {
29
+ success: false,
30
+ error: {
31
+ code: 'UNAUTHORIZED',
32
+ message: 'Sessão inválida ou expirada. Por favor, faça login novamente.',
33
+ },
34
+ }
28
35
  },
29
36
  };
30
37
 
@@ -114,12 +121,30 @@ function authPlugin(userConfig = {}) {
114
121
  .onBeforeHandle({ as: 'global' }, ({ user, set, request }) => {
115
122
  const path = new URL(request.url).pathname;
116
123
  if (config.excludePaths.some(p => path.startsWith(p))) return
117
- if (!user) return config.onUnauthorized(set)
124
+ if (!user) {
125
+ set.status = 401;
126
+ set.headers['Content-Type'] = 'application/json';
127
+ return {
128
+ success: false,
129
+ error: {
130
+ code: 'UNAUTHORIZED',
131
+ message: 'Sessão inválida ou expirada. Por favor, faça login novamente.',
132
+ },
133
+ }
134
+ }
118
135
  })
119
136
  .error(({ error, set }) => {
120
- if (String(error).includes('user.id') || String(error).includes('of null')) {
137
+ const errorStr = String(error);
138
+ if (errorStr.includes('user.id') || errorStr.includes('of null') || errorStr.includes('of undefined')) {
121
139
  set.status = 401;
122
- return 'Sessão inválida ou expirada.'
140
+ set.headers['Content-Type'] = 'application/json';
141
+ return {
142
+ success: false,
143
+ error: {
144
+ code: 'UNAUTHORIZED',
145
+ message: 'Sessão inválida ou expirada. Por favor, faça login novamente.',
146
+ },
147
+ }
123
148
  }
124
149
  })
125
150
  .group(config.prefix, app =>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riligar/auth-elysia",
3
- "version": "1.6.3",
3
+ "version": "1.7.0",
4
4
  "type": "module",
5
5
  "description": "Auth SDK for ElysiaJS with JWT and JWKS",
6
6
  "main": "dist/index.js",
package/src/index.js CHANGED
@@ -19,7 +19,14 @@ const DEFAULT_CONFIG = {
19
19
  excludePaths: ['/auth/login', '/auth/register', '/auth/session'],
20
20
  onUnauthorized: set => {
21
21
  set.status = 401
22
- return 'Sessão inválida ou expirada.'
22
+ set.headers = { ...set.headers, 'Content-Type': 'application/json' }
23
+ return {
24
+ success: false,
25
+ error: {
26
+ code: 'UNAUTHORIZED',
27
+ message: 'Sessão inválida ou expirada. Por favor, faça login novamente.',
28
+ },
29
+ }
23
30
  },
24
31
  }
25
32
 
@@ -109,12 +116,30 @@ export function authPlugin(userConfig = {}) {
109
116
  .onBeforeHandle({ as: 'global' }, ({ user, set, request }) => {
110
117
  const path = new URL(request.url).pathname
111
118
  if (config.excludePaths.some(p => path.startsWith(p))) return
112
- if (!user) return config.onUnauthorized(set)
119
+ if (!user) {
120
+ set.status = 401
121
+ set.headers['Content-Type'] = 'application/json'
122
+ return {
123
+ success: false,
124
+ error: {
125
+ code: 'UNAUTHORIZED',
126
+ message: 'Sessão inválida ou expirada. Por favor, faça login novamente.',
127
+ },
128
+ }
129
+ }
113
130
  })
114
131
  .error(({ error, set }) => {
115
- if (String(error).includes('user.id') || String(error).includes('of null')) {
132
+ const errorStr = String(error)
133
+ if (errorStr.includes('user.id') || errorStr.includes('of null') || errorStr.includes('of undefined')) {
116
134
  set.status = 401
117
- return 'Sessão inválida ou expirada.'
135
+ set.headers['Content-Type'] = 'application/json'
136
+ return {
137
+ success: false,
138
+ error: {
139
+ code: 'UNAUTHORIZED',
140
+ message: 'Sessão inválida ou expirada. Por favor, faça login novamente.',
141
+ },
142
+ }
118
143
  }
119
144
  })
120
145
  .group(config.prefix, app =>