@mostajs/auth-lite 0.2.0 → 0.3.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.ts CHANGED
@@ -27,4 +27,13 @@ export interface AuthLiteConfig {
27
27
  loginErrorPath?: string;
28
28
  /** Redirect on signup error (default "/signup?error=<kind>"). */
29
29
  signupErrorPath?: (kind: 'invalid' | 'exists') => string;
30
+ /**
31
+ * Embedding in a **cross-site iframe** (ex. CodeSandbox preview `*.csb.app`) :
32
+ * un cookie `sameSite:'lax'` n'est PAS renvoyé → session perdue. Mettre `true`
33
+ * pour utiliser `sameSite:'none'; secure:true` **quand la requête est en
34
+ * https** (détecté via `x-forwarded-proto`), et retomber sur `lax` en http
35
+ * (localhost dev). Défaut `false` (lax). ⚠️ `none` réduit la protection CSRF —
36
+ * à n'activer que pour des previews embarquées, pas pour une vraie prod hors-iframe.
37
+ */
38
+ crossSiteCookie?: boolean;
30
39
  }
package/dist/next.js CHANGED
@@ -38,12 +38,35 @@ export function createAuthHandlers(config) {
38
38
  const afterLogout = config.afterLogout ?? '/';
39
39
  const loginError = config.loginErrorPath ?? '/login?error=invalid';
40
40
  const signupError = config.signupErrorPath ?? ((k) => `/signup?error=${k}`);
41
- async function openSession(sessions, userId) {
41
+ const crossSite = config.crossSiteCookie ?? false;
42
+ /** La requête arrive-t-elle en https (proxy forwarde `x-forwarded-proto`, ou URL https) ? */
43
+ function isHttps(req) {
44
+ if (req.headers.get('x-forwarded-proto') === 'https')
45
+ return true;
46
+ try {
47
+ return new URL(req.url).protocol === 'https:';
48
+ }
49
+ catch {
50
+ return false;
51
+ }
52
+ }
53
+ /**
54
+ * Attributs du cookie de session. `crossSiteCookie` + https → `sameSite:'none'`
55
+ * + `secure` (cookie renvoyé en iframe cross-site, ex. CodeSandbox). Sinon
56
+ * `sameSite:'lax'` (et pas de `secure`, pour que localhost http garde la session).
57
+ */
58
+ function cookieOpts(req, expires) {
59
+ if (crossSite && isHttps(req)) {
60
+ return { httpOnly: true, sameSite: 'none', secure: true, path: '/', expires };
61
+ }
62
+ return { httpOnly: true, sameSite: 'lax', path: '/', expires };
63
+ }
64
+ async function openSession(req, sessions, userId) {
42
65
  const token = randomBytes(32).toString('hex');
43
66
  const expiresAt = new Date(Date.now() + ttlMs);
44
67
  await sessions.create({ token, user: userId, expiresAt });
45
68
  const res = see(afterAuth);
46
- res.cookies.set(cookie, token, { httpOnly: true, sameSite: 'lax', path: '/', expires: expiresAt });
69
+ res.cookies.set(cookie, token, cookieOpts(req, expiresAt));
47
70
  return res;
48
71
  }
49
72
  /** POST handler — verify credentials, start a session. */
@@ -56,7 +79,7 @@ export function createAuthHandlers(config) {
56
79
  if (!user || !verifyPassword(password, user.passwordHash)) {
57
80
  return see(loginError);
58
81
  }
59
- return openSession(sessions, user.id);
82
+ return openSession(req, sessions, user.id);
60
83
  }
61
84
  /** POST handler — create the account, start a session. */
62
85
  async function signup(req) {
@@ -72,7 +95,7 @@ export function createAuthHandlers(config) {
72
95
  return see(signupError('exists'));
73
96
  }
74
97
  const user = await users.create({ email, name, passwordHash: hashPassword(password) });
75
- return openSession(sessions, user.id);
98
+ return openSession(req, sessions, user.id);
76
99
  }
77
100
  /** POST handler — destroy the session (DB + cookie). */
78
101
  async function logout(req) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mostajs/auth-lite",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Minimal email/password + session auth for Next.js on @mostajs/orm. No native addon — boots in Bolt.new / StackBlitz / edge.",
5
5
  "license": "AGPL-3.0-or-later",
6
6
  "author": "Dr Hamid MADANI <drmdh@msn.com>",