@simpleapps-com/augur-server 0.1.6 → 0.1.7
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/auth.d.ts +55 -4
- package/dist/auth.js +56 -4
- package/dist/auth.js.map +1 -1
- package/package.json +2 -2
package/dist/auth.d.ts
CHANGED
|
@@ -18,7 +18,14 @@ interface AugurSession {
|
|
|
18
18
|
token?: string;
|
|
19
19
|
expires: string;
|
|
20
20
|
}
|
|
21
|
-
/**
|
|
21
|
+
/** JWT token shape used by Augur auth. */
|
|
22
|
+
interface AugurJWT {
|
|
23
|
+
id: string;
|
|
24
|
+
username: string;
|
|
25
|
+
isVerified: boolean;
|
|
26
|
+
token?: string;
|
|
27
|
+
}
|
|
28
|
+
/** Site-specific callbacks the consumer can provide. */
|
|
22
29
|
interface AugurAuthCallbacks {
|
|
23
30
|
/** Fetch user profile from the Augur API given the user's Joomla ID. */
|
|
24
31
|
getUserProfile: (userId: string) => Promise<AugurUser | null>;
|
|
@@ -27,18 +34,62 @@ interface AugurAuthCallbacks {
|
|
|
27
34
|
cartHdrUid?: number;
|
|
28
35
|
} | null>;
|
|
29
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Minimal interface for the Augur SDK client used by auth internals.
|
|
39
|
+
* Any `AugurAPI` instance from `@simpleapps-com/augur-api` satisfies this.
|
|
40
|
+
*/
|
|
41
|
+
interface AugurAuthClient {
|
|
42
|
+
joomla: {
|
|
43
|
+
users: {
|
|
44
|
+
doc: {
|
|
45
|
+
get(userId: number, params?: {
|
|
46
|
+
edgeCache?: number | string;
|
|
47
|
+
}): Promise<{
|
|
48
|
+
data: {
|
|
49
|
+
id: number;
|
|
50
|
+
name: string;
|
|
51
|
+
username: string;
|
|
52
|
+
email: string;
|
|
53
|
+
customerId?: string;
|
|
54
|
+
contactId?: string;
|
|
55
|
+
};
|
|
56
|
+
}>;
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
commerce: {
|
|
61
|
+
cartHdr: {
|
|
62
|
+
lookup: {
|
|
63
|
+
get(params: {
|
|
64
|
+
userId: number;
|
|
65
|
+
customerId: number;
|
|
66
|
+
contactId: number;
|
|
67
|
+
cartToken?: string;
|
|
68
|
+
}): Promise<{
|
|
69
|
+
data: {
|
|
70
|
+
cartHdrUid?: number;
|
|
71
|
+
};
|
|
72
|
+
}>;
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
}
|
|
30
77
|
interface CreateAuthConfigOptions {
|
|
31
|
-
/**
|
|
32
|
-
|
|
78
|
+
/** Augur SDK client. When provided, auth SDK calls are handled internally. */
|
|
79
|
+
augurClient?: AugurAuthClient;
|
|
80
|
+
/** Site-specific callbacks. Override internal SDK calls when custom behavior is needed. */
|
|
81
|
+
callbacks?: Partial<AugurAuthCallbacks>;
|
|
33
82
|
/** NextAuth secret (defaults to NEXT_PUBLIC_AUTH_SECRET env var). */
|
|
34
83
|
secret?: string;
|
|
35
84
|
/** Session max age in seconds (defaults to 4 hours). */
|
|
36
85
|
maxAge?: number;
|
|
37
86
|
/** Default customer ID when profile doesn't provide one. */
|
|
38
87
|
defaultCustomerId?: string | number;
|
|
88
|
+
/** Default contact ID for cart header lookups. */
|
|
89
|
+
defaultContactId?: string | number;
|
|
39
90
|
/** Enable NextAuth debug logging (defaults to NODE_ENV === "development"). */
|
|
40
91
|
debug?: boolean;
|
|
41
92
|
}
|
|
42
93
|
declare function createAuthConfig(options: CreateAuthConfigOptions): NextAuthConfig;
|
|
43
94
|
|
|
44
|
-
export { type AugurAuthCallbacks, type AugurSession, type AugurUser, type CreateAuthConfigOptions, createAuthConfig };
|
|
95
|
+
export { type AugurAuthCallbacks, type AugurAuthClient, type AugurJWT, type AugurSession, type AugurUser, type CreateAuthConfigOptions, createAuthConfig };
|
package/dist/auth.js
CHANGED
|
@@ -2,24 +2,75 @@ import "./chunk-DGUM43GV.js";
|
|
|
2
2
|
|
|
3
3
|
// src/auth.ts
|
|
4
4
|
import Credentials from "next-auth/providers/credentials";
|
|
5
|
-
|
|
5
|
+
function buildInternalCallbacks(augurClient) {
|
|
6
|
+
return {
|
|
7
|
+
async getUserProfile(userId) {
|
|
8
|
+
if (!userId) return null;
|
|
9
|
+
const uid = parseInt(userId, 10);
|
|
10
|
+
if (isNaN(uid)) return null;
|
|
11
|
+
const result = await augurClient.joomla.users.doc.get(uid, {
|
|
12
|
+
edgeCache: 1
|
|
13
|
+
});
|
|
14
|
+
const data = result.data;
|
|
15
|
+
if (!data) return null;
|
|
16
|
+
return {
|
|
17
|
+
id: String(data.id),
|
|
18
|
+
name: data.name,
|
|
19
|
+
username: data.username,
|
|
20
|
+
email: data.email,
|
|
21
|
+
isVerified: true,
|
|
22
|
+
customerId: data.customerId,
|
|
23
|
+
contactId: data.contactId
|
|
24
|
+
};
|
|
25
|
+
},
|
|
26
|
+
async cartHdrLookup(userId, _token, contactId, customerId) {
|
|
27
|
+
const result = await augurClient.commerce.cartHdr.lookup.get({
|
|
28
|
+
userId: Number(userId) || 0,
|
|
29
|
+
customerId: Number(customerId) || 0,
|
|
30
|
+
contactId: Number(contactId) || 0
|
|
31
|
+
});
|
|
32
|
+
return result.data ?? null;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function resolveCallbacks(options) {
|
|
37
|
+
const { augurClient, callbacks } = options;
|
|
38
|
+
if (augurClient) {
|
|
39
|
+
const internal = buildInternalCallbacks(augurClient);
|
|
40
|
+
return {
|
|
41
|
+
getUserProfile: callbacks?.getUserProfile ?? internal.getUserProfile,
|
|
42
|
+
cartHdrLookup: callbacks?.cartHdrLookup ?? internal.cartHdrLookup
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
if (!callbacks?.getUserProfile) {
|
|
46
|
+
throw new Error(
|
|
47
|
+
"createAuthConfig requires either `augurClient` or `callbacks.getUserProfile`"
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
getUserProfile: callbacks.getUserProfile,
|
|
52
|
+
cartHdrLookup: callbacks.cartHdrLookup
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
async function lookupCartHdr(siteCallbacks, profile, defaultCustomerId, defaultContactId) {
|
|
6
56
|
if (!siteCallbacks.cartHdrLookup || !profile.id) return void 0;
|
|
7
57
|
const cartHdr = await siteCallbacks.cartHdrLookup(
|
|
8
58
|
profile.id,
|
|
9
59
|
"",
|
|
10
|
-
profile.contactId ?? "",
|
|
60
|
+
profile.contactId ?? defaultContactId ?? "",
|
|
11
61
|
profile.customerId ?? defaultCustomerId ?? ""
|
|
12
62
|
);
|
|
13
63
|
return cartHdr?.cartHdrUid;
|
|
14
64
|
}
|
|
15
65
|
function createAuthConfig(options) {
|
|
16
66
|
const {
|
|
17
|
-
callbacks: siteCallbacks,
|
|
18
67
|
secret = process.env.NEXT_PUBLIC_AUTH_SECRET,
|
|
19
68
|
maxAge = 4 * 60 * 60,
|
|
20
69
|
defaultCustomerId,
|
|
70
|
+
defaultContactId,
|
|
21
71
|
debug = process.env.NODE_ENV === "development"
|
|
22
72
|
} = options;
|
|
73
|
+
const siteCallbacks = resolveCallbacks(options);
|
|
23
74
|
return {
|
|
24
75
|
providers: [
|
|
25
76
|
Credentials({
|
|
@@ -67,7 +118,8 @@ function createAuthConfig(options) {
|
|
|
67
118
|
const cartHdrUid = await lookupCartHdr(
|
|
68
119
|
siteCallbacks,
|
|
69
120
|
userProfile,
|
|
70
|
-
defaultCustomerId
|
|
121
|
+
defaultCustomerId,
|
|
122
|
+
defaultContactId
|
|
71
123
|
);
|
|
72
124
|
return buildSession({
|
|
73
125
|
name: userProfile.name ?? session.user?.name,
|
package/dist/auth.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/auth.ts"],"sourcesContent":["// @simpleapps-com/augur-server/auth\n// NextAuth 5 configuration factory for Augur ecommerce sites.\n\nimport type { NextAuthConfig } from \"next-auth\";\nimport Credentials from \"next-auth/providers/credentials\";\n\n/** Base user fields shared across all Augur sites. */\nexport interface AugurUser {\n id: string;\n username: string;\n isVerified: boolean;\n name?: string;\n email?: string;\n customerId?: string | number;\n contactId?: string | number;\n cartHdrUid?: number;\n token?: string;\n}\n\n/** Session shape returned by all Augur sites. */\nexport interface AugurSession {\n user: AugurUser;\n token?: string;\n expires: string;\n}\n\n/** Site-specific callbacks the consumer must provide. */\nexport interface AugurAuthCallbacks {\n /** Fetch user profile from the Augur API given the user's Joomla ID. */\n getUserProfile: (userId: string) => Promise<AugurUser | null>;\n /** Look up or create a cart header for the authenticated user. */\n cartHdrLookup?: (\n userId: string | number,\n token: string,\n contactId: string | number,\n customerId: string | number,\n ) => Promise<{ cartHdrUid?: number } | null>;\n}\n\nexport interface CreateAuthConfigOptions {\n /** Site-specific callbacks. */\n callbacks: AugurAuthCallbacks;\n /** NextAuth secret (defaults to NEXT_PUBLIC_AUTH_SECRET env var). */\n secret?: string;\n /** Session max age in seconds (defaults to 4 hours). */\n maxAge?: number;\n /** Default customer ID when profile doesn't provide one. */\n defaultCustomerId?: string | number;\n /** Enable NextAuth debug logging (defaults to NODE_ENV === \"development\"). */\n debug?: boolean;\n}\n\n/**\n * Create a NextAuth 5 configuration for an Augur ecommerce site.\n *\n * Each site calls this factory with its own getUserProfile and\n * cartHdrLookup implementations. The result is passed to NextAuth().\n *\n * @example\n * ```ts\n * // auth.ts\n * import NextAuth from \"next-auth\";\n * import { createAuthConfig } from \"@simpleapps-com/augur-server/auth\";\n * import { getUserProfile, cartHdrLookup } from \"./lib/actions/users\";\n *\n * export const { handlers, signIn, signOut, auth } = NextAuth(\n * createAuthConfig({\n * callbacks: { getUserProfile, cartHdrLookup },\n * defaultCustomerId: process.env.NEXT_PUBLIC_DEFAULT_CUSTOMER_ID,\n * }),\n * );\n * ```\n */\nasync function lookupCartHdr(\n siteCallbacks: AugurAuthCallbacks,\n profile: AugurUser,\n defaultCustomerId?: string | number,\n): Promise<number | undefined> {\n if (!siteCallbacks.cartHdrLookup || !profile.id) return undefined;\n const cartHdr = await siteCallbacks.cartHdrLookup(\n profile.id,\n \"\",\n profile.contactId ?? \"\",\n profile.customerId ?? defaultCustomerId ?? \"\",\n );\n return cartHdr?.cartHdrUid;\n}\n\nexport function createAuthConfig(\n options: CreateAuthConfigOptions,\n): NextAuthConfig {\n const {\n callbacks: siteCallbacks,\n secret = process.env.NEXT_PUBLIC_AUTH_SECRET,\n maxAge = 4 * 60 * 60,\n defaultCustomerId,\n debug = process.env.NODE_ENV === \"development\",\n } = options;\n\n return {\n providers: [\n Credentials({\n name: \"Credentials\",\n credentials: {\n username: { label: \"Username\", type: \"text\" },\n password: { label: \"Password\", type: \"password\" },\n id: { label: \"ID\", type: \"text\" },\n isVerified: { label: \"Is Verified\", type: \"text\" },\n token: { label: \"Token\", type: \"text\" },\n },\n async authorize(credentials) {\n if (!credentials) return null;\n\n const { id, isVerified, username, token } = credentials as {\n id: string;\n isVerified: string;\n username: string;\n token: string;\n };\n\n return {\n id,\n isVerified: isVerified === \"true\",\n username,\n token,\n };\n },\n }),\n ],\n callbacks: {\n async signIn() {\n return true;\n },\n async redirect({ baseUrl }) {\n return baseUrl;\n },\n async session({ session, token }) {\n const baseUser = {\n id: (token.id as string) || \"\",\n username: (token.username as string) || \"\",\n isVerified: (token.isVerified as boolean) || false,\n };\n\n const buildSession = (userOverrides = {}) => ({\n ...session,\n user: { ...session.user, ...baseUser, ...userOverrides },\n token: token.token as string | undefined,\n });\n\n try {\n const userProfile = await siteCallbacks.getUserProfile(baseUser.id);\n if (!userProfile) return buildSession();\n\n const cartHdrUid = await lookupCartHdr(\n siteCallbacks,\n userProfile,\n defaultCustomerId,\n );\n\n return buildSession({\n name: userProfile.name ?? session.user?.name,\n email: userProfile.email ?? session.user?.email,\n customerId: userProfile.customerId ?? defaultCustomerId,\n contactId: userProfile.contactId,\n cartHdrUid,\n });\n } catch {\n return buildSession();\n }\n },\n async jwt({ token, user }) {\n if (user) {\n const typedUser = user as {\n id: string;\n isVerified: boolean;\n username: string;\n token: string;\n };\n\n return {\n ...token,\n id: typedUser.id,\n isVerified: typedUser.isVerified,\n username: typedUser.username,\n token: typedUser.token,\n };\n }\n return token;\n },\n },\n secret,\n debug,\n session: {\n strategy: \"jwt\",\n maxAge,\n },\n trustHost: true,\n };\n}\n"],"mappings":";;;AAIA,OAAO,iBAAiB;AAqExB,eAAe,cACb,eACA,SACA,mBAC6B;AAC7B,MAAI,CAAC,cAAc,iBAAiB,CAAC,QAAQ,GAAI,QAAO;AACxD,QAAM,UAAU,MAAM,cAAc;AAAA,IAClC,QAAQ;AAAA,IACR;AAAA,IACA,QAAQ,aAAa;AAAA,IACrB,QAAQ,cAAc,qBAAqB;AAAA,EAC7C;AACA,SAAO,SAAS;AAClB;AAEO,SAAS,iBACd,SACgB;AAChB,QAAM;AAAA,IACJ,WAAW;AAAA,IACX,SAAS,QAAQ,IAAI;AAAA,IACrB,SAAS,IAAI,KAAK;AAAA,IAClB;AAAA,IACA,QAAQ,QAAQ,IAAI,aAAa;AAAA,EACnC,IAAI;AAEJ,SAAO;AAAA,IACL,WAAW;AAAA,MACT,YAAY;AAAA,QACV,MAAM;AAAA,QACN,aAAa;AAAA,UACX,UAAU,EAAE,OAAO,YAAY,MAAM,OAAO;AAAA,UAC5C,UAAU,EAAE,OAAO,YAAY,MAAM,WAAW;AAAA,UAChD,IAAI,EAAE,OAAO,MAAM,MAAM,OAAO;AAAA,UAChC,YAAY,EAAE,OAAO,eAAe,MAAM,OAAO;AAAA,UACjD,OAAO,EAAE,OAAO,SAAS,MAAM,OAAO;AAAA,QACxC;AAAA,QACA,MAAM,UAAU,aAAa;AAC3B,cAAI,CAAC,YAAa,QAAO;AAEzB,gBAAM,EAAE,IAAI,YAAY,UAAU,MAAM,IAAI;AAO5C,iBAAO;AAAA,YACL;AAAA,YACA,YAAY,eAAe;AAAA,YAC3B;AAAA,YACA;AAAA,UACF;AAAA,QACF;AAAA,MACF,CAAC;AA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AAAA,IACA,WAAW;AAAA,EACb;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/auth.ts"],"sourcesContent":["// @simpleapps-com/augur-server/auth\n// NextAuth 5 configuration factory for Augur ecommerce sites.\n\nimport type { NextAuthConfig } from \"next-auth\";\nimport Credentials from \"next-auth/providers/credentials\";\n\n/** Base user fields shared across all Augur sites. */\nexport interface AugurUser {\n id: string;\n username: string;\n isVerified: boolean;\n name?: string;\n email?: string;\n customerId?: string | number;\n contactId?: string | number;\n cartHdrUid?: number;\n token?: string;\n}\n\n/** Session shape returned by all Augur sites. */\nexport interface AugurSession {\n user: AugurUser;\n token?: string;\n expires: string;\n}\n\n/** JWT token shape used by Augur auth. */\nexport interface AugurJWT {\n id: string;\n username: string;\n isVerified: boolean;\n token?: string;\n}\n\n/** Site-specific callbacks the consumer can provide. */\nexport interface AugurAuthCallbacks {\n /** Fetch user profile from the Augur API given the user's Joomla ID. */\n getUserProfile: (userId: string) => Promise<AugurUser | null>;\n /** Look up or create a cart header for the authenticated user. */\n cartHdrLookup?: (\n userId: string | number,\n token: string,\n contactId: string | number,\n customerId: string | number,\n ) => Promise<{ cartHdrUid?: number } | null>;\n}\n\n/**\n * Minimal interface for the Augur SDK client used by auth internals.\n * Any `AugurAPI` instance from `@simpleapps-com/augur-api` satisfies this.\n */\nexport interface AugurAuthClient {\n joomla: {\n users: {\n doc: {\n get(\n userId: number,\n params?: { edgeCache?: number | string },\n ): Promise<{\n data: {\n id: number;\n name: string;\n username: string;\n email: string;\n customerId?: string;\n contactId?: string;\n };\n }>;\n };\n };\n };\n commerce: {\n cartHdr: {\n lookup: {\n get(params: {\n userId: number;\n customerId: number;\n contactId: number;\n cartToken?: string;\n }): Promise<{\n data: { cartHdrUid?: number };\n }>;\n };\n };\n };\n}\n\nexport interface CreateAuthConfigOptions {\n /** Augur SDK client. When provided, auth SDK calls are handled internally. */\n augurClient?: AugurAuthClient;\n /** Site-specific callbacks. Override internal SDK calls when custom behavior is needed. */\n callbacks?: Partial<AugurAuthCallbacks>;\n /** NextAuth secret (defaults to NEXT_PUBLIC_AUTH_SECRET env var). */\n secret?: string;\n /** Session max age in seconds (defaults to 4 hours). */\n maxAge?: number;\n /** Default customer ID when profile doesn't provide one. */\n defaultCustomerId?: string | number;\n /** Default contact ID for cart header lookups. */\n defaultContactId?: string | number;\n /** Enable NextAuth debug logging (defaults to NODE_ENV === \"development\"). */\n debug?: boolean;\n}\n\n/** Build internal callbacks that call the Augur SDK directly. */\nfunction buildInternalCallbacks(\n augurClient: AugurAuthClient,\n): AugurAuthCallbacks {\n return {\n async getUserProfile(userId: string): Promise<AugurUser | null> {\n if (!userId) return null;\n const uid = parseInt(userId, 10);\n if (isNaN(uid)) return null;\n const result = await augurClient.joomla.users.doc.get(uid, {\n edgeCache: 1,\n });\n const data = result.data;\n if (!data) return null;\n return {\n id: String(data.id),\n name: data.name,\n username: data.username,\n email: data.email,\n isVerified: true,\n customerId: data.customerId,\n contactId: data.contactId,\n };\n },\n async cartHdrLookup(\n userId: string | number,\n _token: string,\n contactId: string | number,\n customerId: string | number,\n ): Promise<{ cartHdrUid?: number } | null> {\n const result = await augurClient.commerce.cartHdr.lookup.get({\n userId: Number(userId) || 0,\n customerId: Number(customerId) || 0,\n contactId: Number(contactId) || 0,\n });\n return result.data ?? null;\n },\n };\n}\n\n/** Resolve final callbacks: callback overrides take priority over internal SDK calls. */\nfunction resolveCallbacks(\n options: CreateAuthConfigOptions,\n): AugurAuthCallbacks {\n const { augurClient, callbacks } = options;\n\n if (augurClient) {\n const internal = buildInternalCallbacks(augurClient);\n return {\n getUserProfile: callbacks?.getUserProfile ?? internal.getUserProfile,\n cartHdrLookup: callbacks?.cartHdrLookup ?? internal.cartHdrLookup,\n };\n }\n\n if (!callbacks?.getUserProfile) {\n throw new Error(\n \"createAuthConfig requires either `augurClient` or `callbacks.getUserProfile`\",\n );\n }\n\n return {\n getUserProfile: callbacks.getUserProfile,\n cartHdrLookup: callbacks.cartHdrLookup,\n };\n}\n\n/**\n * Create a NextAuth 5 configuration for an Augur ecommerce site.\n *\n * Accepts either an `augurClient` (recommended) for zero-boilerplate auth,\n * or explicit `callbacks` for custom behavior. Callback overrides take\n * priority when both are provided.\n *\n * @example\n * ```ts\n * // Recommended: pass the SDK client directly\n * import NextAuth from \"next-auth\";\n * import { createAuthConfig } from \"@simpleapps-com/augur-server/auth\";\n * import { augurClient } from \"@/lib/augur-client\";\n *\n * export const { handlers, signIn, signOut, auth } = NextAuth(\n * createAuthConfig({\n * augurClient,\n * defaultCustomerId: process.env.NEXT_PUBLIC_DEFAULT_CUSTOMER_ID,\n * defaultContactId: process.env.NEXT_PUBLIC_DEFAULT_CONTACT_ID,\n * }),\n * );\n * ```\n *\n * @example\n * ```ts\n * // Custom callbacks (backward-compatible)\n * import NextAuth from \"next-auth\";\n * import { createAuthConfig } from \"@simpleapps-com/augur-server/auth\";\n *\n * export const { handlers, signIn, signOut, auth } = NextAuth(\n * createAuthConfig({\n * callbacks: { getUserProfile, cartHdrLookup },\n * defaultCustomerId: process.env.NEXT_PUBLIC_DEFAULT_CUSTOMER_ID,\n * }),\n * );\n * ```\n */\nasync function lookupCartHdr(\n siteCallbacks: AugurAuthCallbacks,\n profile: AugurUser,\n defaultCustomerId?: string | number,\n defaultContactId?: string | number,\n): Promise<number | undefined> {\n if (!siteCallbacks.cartHdrLookup || !profile.id) return undefined;\n const cartHdr = await siteCallbacks.cartHdrLookup(\n profile.id,\n \"\",\n profile.contactId ?? defaultContactId ?? \"\",\n profile.customerId ?? defaultCustomerId ?? \"\",\n );\n return cartHdr?.cartHdrUid;\n}\n\nexport function createAuthConfig(\n options: CreateAuthConfigOptions,\n): NextAuthConfig {\n const {\n secret = process.env.NEXT_PUBLIC_AUTH_SECRET,\n maxAge = 4 * 60 * 60,\n defaultCustomerId,\n defaultContactId,\n debug = process.env.NODE_ENV === \"development\",\n } = options;\n\n const siteCallbacks = resolveCallbacks(options);\n\n return {\n providers: [\n Credentials({\n name: \"Credentials\",\n credentials: {\n username: { label: \"Username\", type: \"text\" },\n password: { label: \"Password\", type: \"password\" },\n id: { label: \"ID\", type: \"text\" },\n isVerified: { label: \"Is Verified\", type: \"text\" },\n token: { label: \"Token\", type: \"text\" },\n },\n async authorize(credentials) {\n if (!credentials) return null;\n\n const { id, isVerified, username, token } = credentials as {\n id: string;\n isVerified: string;\n username: string;\n token: string;\n };\n\n return {\n id,\n isVerified: isVerified === \"true\",\n username,\n token,\n };\n },\n }),\n ],\n callbacks: {\n async signIn() {\n return true;\n },\n async redirect({ baseUrl }) {\n return baseUrl;\n },\n async session({ session, token }) {\n const baseUser = {\n id: (token.id as string) || \"\",\n username: (token.username as string) || \"\",\n isVerified: (token.isVerified as boolean) || false,\n };\n\n const buildSession = (userOverrides = {}) => ({\n ...session,\n user: { ...session.user, ...baseUser, ...userOverrides },\n token: token.token as string | undefined,\n });\n\n try {\n const userProfile = await siteCallbacks.getUserProfile(baseUser.id);\n if (!userProfile) return buildSession();\n\n const cartHdrUid = await lookupCartHdr(\n siteCallbacks,\n userProfile,\n defaultCustomerId,\n defaultContactId,\n );\n\n return buildSession({\n name: userProfile.name ?? session.user?.name,\n email: userProfile.email ?? session.user?.email,\n customerId: userProfile.customerId ?? defaultCustomerId,\n contactId: userProfile.contactId,\n cartHdrUid,\n });\n } catch {\n return buildSession();\n }\n },\n async jwt({ token, user }) {\n if (user) {\n const typedUser = user as {\n id: string;\n isVerified: boolean;\n username: string;\n token: string;\n };\n\n return {\n ...token,\n id: typedUser.id,\n isVerified: typedUser.isVerified,\n username: typedUser.username,\n token: typedUser.token,\n };\n }\n return token;\n },\n },\n secret,\n debug,\n session: {\n strategy: \"jwt\",\n maxAge,\n },\n trustHost: true,\n };\n}\n"],"mappings":";;;AAIA,OAAO,iBAAiB;AAqGxB,SAAS,uBACP,aACoB;AACpB,SAAO;AAAA,IACL,MAAM,eAAe,QAA2C;AAC9D,UAAI,CAAC,OAAQ,QAAO;AACpB,YAAM,MAAM,SAAS,QA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AC5C,GAAG;AAAA,UACH,MAAM,EAAE,GAAG,QAAQ,MAAM,GAAG,UAAU,GAAG,cAAc;AAAA,UACvD,OAAO,MAAM;AAAA,QACf;AAEA,YAAI;AACF,gBAAM,cAAc,MAAM,cAAc,eAAe,SAAS,EAAE;AAClE,cAAI,CAAC,YAAa,QAAO,aAAa;AAEtC,gBAAM,aAAa,MAAM;AAAA,YACvB;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,UACF;AAEA,iBAAO,aAAa;AAAA,YAClB,MAAM,YAAY,QAAQ,QAAQ,MAAM;AAAA,YACxC,OAAO,YAAY,SAAS,QAAQ,MAAM;AAAA,YAC1C,YAAY,YAAY,cAAc;AAAA,YACtC,WAAW,YAAY;AAAA,YACvB;AAAA,UACF,CAAC;AAAA,QACH,QAAQ;AACN,iBAAO,aAAa;AAAA,QACtB;AAAA,MACF;AAAA,MACA,MAAM,IAAI,EAAE,OAAO,KAAK,GAAG;AACzB,YAAI,MAAM;AACR,gBAAM,YAAY;AAOlB,iBAAO;AAAA,YACL,GAAG;AAAA,YACH,IAAI,UAAU;AAAA,YACd,YAAY,UAAU;AAAA,YACtB,UAAU,UAAU;AAAA,YACpB,OAAO,UAAU;AAAA,UACnB;AAAA,QACF;AACA,eAAO;AAAA,MACT;AAAA,IACF;AAAA,IACA;AAAA,IACA;AAAA,IACA,SAAS;AAAA,MACP,UAAU;AAAA,MACV;AAAA,IACF;AAAA,IACA,WAAW;AAAA,EACb;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simpleapps-com/augur-server",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "Server-side utilities for Augur ecommerce sites (Redis caching, SDK helpers, auth)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"valibot": "^1.0.0",
|
|
28
|
-
"@simpleapps-com/augur-utils": "0.1.
|
|
28
|
+
"@simpleapps-com/augur-utils": "0.1.7"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
31
|
"@simpleapps-com/augur-api": "^0.9.6",
|