@meeovi/auth 1.0.2 → 1.0.4

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.
@@ -0,0 +1,8 @@
1
+ export interface AuthConfig {
2
+ baseUrl: any;
3
+ authProvider: string;
4
+ authUrl?: string;
5
+ sessionCookieName?: string;
6
+ }
7
+ export declare function setAuthConfig(newConfig: Partial<AuthConfig>): void;
8
+ export declare function getAuthConfig(): AuthConfig;
package/dist/config.js ADDED
@@ -0,0 +1,12 @@
1
+ let config = {
2
+ authProvider: 'better-auth',
3
+ authUrl: '',
4
+ sessionCookieName: 'session',
5
+ baseUrl: undefined
6
+ };
7
+ export function setAuthConfig(newConfig) {
8
+ config = { ...config, ...newConfig };
9
+ }
10
+ export function getAuthConfig() {
11
+ return config;
12
+ }
@@ -0,0 +1,17 @@
1
+ export interface FrameworkContext {
2
+ getRequestURL?(): string;
3
+ getRequestHeaders?(): Record<string, string>;
4
+ getConfig?(): any;
5
+ reloadApp?(): void;
6
+ state?<T>(key: string, init: () => T): {
7
+ value: T;
8
+ };
9
+ }
10
+ /**
11
+ * Frameworks (Nuxt, React, etc.) call this once during initialization.
12
+ */
13
+ export declare function setFrameworkContext(newCtx: FrameworkContext): void;
14
+ /**
15
+ * Auth providers use this to access framework-specific helpers.
16
+ */
17
+ export declare function getFrameworkContext(): FrameworkContext;
@@ -0,0 +1,13 @@
1
+ let ctx = {};
2
+ /**
3
+ * Frameworks (Nuxt, React, etc.) call this once during initialization.
4
+ */
5
+ export function setFrameworkContext(newCtx) {
6
+ ctx = { ...ctx, ...newCtx };
7
+ }
8
+ /**
9
+ * Auth providers use this to access framework-specific helpers.
10
+ */
11
+ export function getFrameworkContext() {
12
+ return ctx;
13
+ }
@@ -0,0 +1,9 @@
1
+ export * from './useAuth';
2
+ export * from "better-auth";
3
+ export * from "@better-auth/cli";
4
+ export * from "@better-auth/oauth-provider";
5
+ export * from "@better-auth/passkey";
6
+ export * from "@better-auth/scim";
7
+ export * from "@better-auth/sso";
8
+ export * from "@better-auth/stripe";
9
+ export * as polarSh from "@polar-sh/better-auth";
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ export * from './useAuth';
2
+ export * from "better-auth";
3
+ export * from "@better-auth/cli";
4
+ export * from "@better-auth/oauth-provider";
5
+ export * from "@better-auth/passkey";
6
+ export * from "@better-auth/scim";
7
+ export * from "@better-auth/sso";
8
+ export * from "@better-auth/stripe";
9
+ export * as polarSh from "@polar-sh/better-auth";
@@ -0,0 +1 @@
1
+ export declare function normalizeTrailingSlash(path: string): string | null;
@@ -0,0 +1,6 @@
1
+ export function normalizeTrailingSlash(path) {
2
+ if (path !== '/' && path.endsWith('/')) {
3
+ return path.replace(/\/+$/, '') || '/';
4
+ }
5
+ return null;
6
+ }
@@ -0,0 +1,2 @@
1
+ export declare function isSeller(user: any): boolean;
2
+ export declare function isSellerRoute(path: string): boolean;
@@ -0,0 +1,6 @@
1
+ export function isSeller(user) {
2
+ return !!(user && user.role === 'seller');
3
+ }
4
+ export function isSellerRoute(path) {
5
+ return path.startsWith('/dashboard/');
6
+ }
@@ -0,0 +1,5 @@
1
+ export interface Session {
2
+ id: string;
3
+ date_created: string;
4
+ }
5
+ export declare function ensureSession(getCookie: (name: string) => any, setCookie: (name: string, value: string) => void, generateId: () => string): void;
@@ -0,0 +1,10 @@
1
+ export function ensureSession(getCookie, setCookie, generateId) {
2
+ const session = getCookie('session');
3
+ if (!session) {
4
+ const newSession = {
5
+ id: generateId(),
6
+ date_created: new Date().toISOString(),
7
+ };
8
+ setCookie('session', JSON.stringify(newSession));
9
+ }
10
+ }
@@ -0,0 +1,5 @@
1
+ export type AuthPluginOptions = {
2
+ /** whether to enable subscription support for stripe plugin */
3
+ subscription?: boolean;
4
+ };
5
+ export declare function getAuthPlugins(opts?: AuthPluginOptions): any[];
@@ -0,0 +1,18 @@
1
+ import { stripeClient } from '@better-auth/stripe/client';
2
+ import { polarClient } from '@polar-sh/better-auth';
3
+ import { adminClient, inferAdditionalFields } from 'better-auth/client/plugins';
4
+ export function getAuthPlugins(opts = {}) {
5
+ const { subscription = true } = opts;
6
+ return [
7
+ inferAdditionalFields({
8
+ user: {
9
+ polarCustomerId: {
10
+ type: 'string'
11
+ }
12
+ }
13
+ }),
14
+ adminClient(),
15
+ polarClient(),
16
+ stripeClient({ subscription })
17
+ ];
18
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,84 @@
1
+ import * as BetterAuth from 'better-auth';
2
+ import { registerAuthProvider } from '../registry';
3
+ import { getAuthConfig } from '../config';
4
+ import { getFrameworkContext } from '../framework';
5
+ // Create a single shared Better Auth client instance
6
+ let client = null;
7
+ function resolveClientFactory() {
8
+ return BetterAuth.Client ?? BetterAuth.default ?? BetterAuth;
9
+ }
10
+ function getClient() {
11
+ if (client)
12
+ return client;
13
+ const config = getAuthConfig();
14
+ const ctx = getFrameworkContext();
15
+ const Client = resolveClientFactory();
16
+ client = Client({
17
+ baseURL: config.baseUrl,
18
+ fetch: async (url, options = {}) => {
19
+ // Framework‑agnostic request wrapper
20
+ const baseHeaders = ctx.getRequestHeaders?.() || {};
21
+ let optionHeaders = {};
22
+ if (options.headers instanceof Headers) {
23
+ options.headers.forEach((value, key) => {
24
+ optionHeaders[key] = value;
25
+ });
26
+ }
27
+ else if (Array.isArray(options.headers)) {
28
+ for (const [k, v] of options.headers) {
29
+ optionHeaders[k] = v;
30
+ }
31
+ }
32
+ else if (typeof options.headers === 'object' && options.headers !== null) {
33
+ optionHeaders = options.headers;
34
+ }
35
+ const headers = {
36
+ ...baseHeaders,
37
+ ...optionHeaders
38
+ };
39
+ return fetch(url, { ...options, headers });
40
+ }
41
+ });
42
+ return client;
43
+ }
44
+ const BetterAuthProvider = {
45
+ async login(credentials) {
46
+ const client = getClient();
47
+ const result = await client.signIn(credentials);
48
+ if (result.error) {
49
+ throw new Error(result.error.message || 'Login failed');
50
+ }
51
+ return result.data;
52
+ },
53
+ async logout() {
54
+ const client = getClient();
55
+ await client.signOut();
56
+ const ctx = getFrameworkContext();
57
+ ctx.reloadApp?.();
58
+ },
59
+ async session() {
60
+ const client = getClient();
61
+ const result = await client.session();
62
+ if (result.error) {
63
+ return null;
64
+ }
65
+ return result.data;
66
+ },
67
+ async register(data) {
68
+ const client = getClient();
69
+ const result = await client.signUp(data);
70
+ if (result.error) {
71
+ throw new Error(result.error.message || 'Registration failed');
72
+ }
73
+ return result.data;
74
+ },
75
+ async refresh() {
76
+ const client = getClient();
77
+ const result = await client.refresh();
78
+ if (result.error) {
79
+ throw new Error(result.error.message || 'Session refresh failed');
80
+ }
81
+ return result.data;
82
+ }
83
+ };
84
+ registerAuthProvider('better-auth', BetterAuthProvider);
@@ -0,0 +1,4 @@
1
+ import type { AuthProvider } from './types';
2
+ export declare function registerAuthProvider(name: string, provider: AuthProvider): void;
3
+ export declare function setActiveAuthProvider(name: string): void;
4
+ export declare function getAuthProvider(): AuthProvider;
@@ -0,0 +1,17 @@
1
+ const providers = {};
2
+ let activeProvider = null;
3
+ export function registerAuthProvider(name, provider) {
4
+ providers[name] = provider;
5
+ }
6
+ export function setActiveAuthProvider(name) {
7
+ if (!providers[name]) {
8
+ throw new Error(`Auth provider "${name}" is not registered`);
9
+ }
10
+ activeProvider = name;
11
+ }
12
+ export function getAuthProvider() {
13
+ if (!activeProvider) {
14
+ throw new Error('No active auth provider has been set');
15
+ }
16
+ return providers[activeProvider];
17
+ }
@@ -0,0 +1,29 @@
1
+ export interface AuthSession {
2
+ user: {
3
+ id: string;
4
+ email?: string;
5
+ name?: string;
6
+ avatarUrl?: string;
7
+ [key: string]: any;
8
+ };
9
+ expiresAt?: string;
10
+ [key: string]: any;
11
+ }
12
+ export interface AuthCredentials {
13
+ email: string;
14
+ password: string;
15
+ [key: string]: any;
16
+ }
17
+ export interface AuthRegistration {
18
+ email: string;
19
+ password: string;
20
+ name?: string;
21
+ [key: string]: any;
22
+ }
23
+ export interface AuthProvider {
24
+ login(credentials: AuthCredentials): Promise<AuthSession>;
25
+ logout(): Promise<void>;
26
+ session(): Promise<AuthSession | null>;
27
+ register?(data: AuthRegistration): Promise<AuthSession>;
28
+ refresh?(): Promise<AuthSession>;
29
+ }
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,36 @@
1
+ import type { Subscription } from '@better-auth/stripe';
2
+ export type UseAuthOptions = {
3
+ /** An existing auth client. If provided, it's used as-is. */
4
+ client?: any;
5
+ /** Base URL for the auth client if creating one. */
6
+ baseURL?: string;
7
+ /** Optional headers to send with requests. */
8
+ headers?: Record<string, string>;
9
+ /** Payment provider: 'stripe' or 'polar'. If omitted, 'stripe' is assumed. */
10
+ payment?: 'stripe' | 'polar';
11
+ /** Optional app-level reload/redirect function (framework-specific). */
12
+ reload?: (opts: {
13
+ path?: string;
14
+ }) => Promise<void>;
15
+ };
16
+ export declare function useAuth(options?: UseAuthOptions): {
17
+ session: null;
18
+ user: null;
19
+ subscription: any;
20
+ subscriptions: Subscription[];
21
+ loggedIn: () => boolean;
22
+ activeStripeSubscription: () => Subscription | undefined;
23
+ activePolarSubscriptions: () => import("@polar-sh/sdk/models/components/customerstatesubscription.js").CustomerStateSubscription[] | undefined;
24
+ signIn: any;
25
+ signUp: any;
26
+ forgetPassword: any;
27
+ resetPassword: any;
28
+ sendVerificationEmail: any;
29
+ errorCodes: any;
30
+ signOut({ redirectTo }?: {
31
+ redirectTo?: string;
32
+ }): Promise<void>;
33
+ fetchSession: () => Promise<any>;
34
+ payment: "stripe" | "polar";
35
+ client: any;
36
+ };
@@ -0,0 +1,88 @@
1
+ import { createAuthClient } from 'better-auth/client';
2
+ import { getAuthPlugins } from './plugins';
3
+ export function useAuth(options = {}) {
4
+ const { client: providedClient, baseURL, headers, payment = 'stripe', reload } = options;
5
+ const client = providedClient || createAuthClient({
6
+ baseURL: baseURL || undefined,
7
+ fetchOptions: {
8
+ headers
9
+ },
10
+ plugins: getAuthPlugins({ subscription: true })
11
+ });
12
+ let session = null;
13
+ let user = null;
14
+ let subscriptions = [];
15
+ let polarState = null;
16
+ let sessionFetching = false;
17
+ const fetchSession = async () => {
18
+ if (sessionFetching)
19
+ return;
20
+ sessionFetching = true;
21
+ const { data } = await client.getSession();
22
+ session = data?.session || null;
23
+ const userDefaults = {
24
+ image: null,
25
+ role: null,
26
+ banReason: null,
27
+ banned: null,
28
+ banExpires: null,
29
+ stripeCustomerId: null
30
+ };
31
+ user = data?.user ? Object.assign({}, userDefaults, data.user) : null;
32
+ subscriptions = [];
33
+ if (user) {
34
+ if (payment == 'stripe') {
35
+ const { data: subscriptionData } = await client.subscription.list();
36
+ subscriptions = subscriptionData || [];
37
+ }
38
+ else if (payment == 'polar') {
39
+ const { data: customerState } = await client.customer.state();
40
+ polarState = customerState;
41
+ }
42
+ }
43
+ sessionFetching = false;
44
+ return data;
45
+ };
46
+ if (client?.$store?.listen) {
47
+ client.$store.listen('$sessionSignal', async (signal) => {
48
+ if (!signal)
49
+ return;
50
+ await fetchSession();
51
+ });
52
+ }
53
+ return {
54
+ session,
55
+ user,
56
+ subscription: client.subscription,
57
+ subscriptions,
58
+ loggedIn: () => !!session,
59
+ activeStripeSubscription: () => {
60
+ return subscriptions.find((sub) => sub.status === 'active' || sub.status === 'trialing');
61
+ },
62
+ activePolarSubscriptions: () => {
63
+ return polarState?.activeSubscriptions;
64
+ },
65
+ signIn: client.signIn,
66
+ signUp: client.signUp,
67
+ forgetPassword: client.forgetPassword,
68
+ resetPassword: client.resetPassword,
69
+ sendVerificationEmail: client.sendVerificationEmail,
70
+ errorCodes: client.$ERROR_CODES,
71
+ async signOut({ redirectTo } = {}) {
72
+ await client.signOut({
73
+ fetchOptions: {
74
+ onSuccess: async () => {
75
+ session = null;
76
+ user = null;
77
+ if (redirectTo && typeof reload === 'function') {
78
+ await reload({ path: redirectTo.toString() });
79
+ }
80
+ }
81
+ }
82
+ });
83
+ },
84
+ fetchSession,
85
+ payment,
86
+ client
87
+ };
88
+ }
package/package.json CHANGED
@@ -1,13 +1,29 @@
1
1
  {
2
2
  "name": "@meeovi/auth",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Authentication for M Framework.",
5
- "license": "ISC",
6
- "author": "",
7
- "type": "commonjs",
8
- "main": "index.ts",
5
+ "license": "MIT",
6
+ "author": "Meeovi",
7
+ "main": "dist/index.js",
8
+ "module": "dist/index.js",
9
+ "types": "dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "import": "./dist/index.js",
13
+ "types": "./dist/index.d.ts"
14
+ }
15
+ },
16
+ "keywords": [
17
+ "better-auth",
18
+ "authentication",
19
+ "auth",
20
+ "typescript",
21
+ "vue",
22
+ "m-framework"
23
+ ],
9
24
  "scripts": {
10
- "test": "echo \"Error: no test specified\" && exit 1"
25
+ "test": "echo \"Error: no test specified\" && exit 1",
26
+ "build": "tsc -p tsconfig.json"
11
27
  },
12
28
  "dependencies": {
13
29
  "@better-auth/cli": "^1.4.10",
@@ -19,12 +35,15 @@
19
35
  "@polar-sh/better-auth": "^1.6.3",
20
36
  "better-auth": "^1.4.12",
21
37
  "mjml": "^4.18.0",
38
+ "typescript": "^5.9.3",
22
39
  "uncrypto": "^0.1.3",
23
40
  "uuid": "^13.0.0",
24
41
  "vue": "^3.5.27",
25
42
  "vue-router": "^4.6.4"
26
43
  },
27
44
  "devDependencies": {
45
+ "@types/mjml": "^4.7.4",
46
+ "@types/node": "^25.0.9",
28
47
  "@types/uuid": "^10.0.0"
29
48
  }
30
49
  }
package/src/index.ts ADDED
@@ -0,0 +1,10 @@
1
+ export * from './useAuth'
2
+
3
+ export * from "better-auth";
4
+ export * from "@better-auth/cli";
5
+ export * from "@better-auth/oauth-provider";
6
+ export * from "@better-auth/passkey";
7
+ export * from "@better-auth/scim";
8
+ export * from "@better-auth/sso";
9
+ export * from "@better-auth/stripe";
10
+ export * as polarSh from "@polar-sh/better-auth";
@@ -0,0 +1,7 @@
1
+
2
+ export function normalizeTrailingSlash(path: string) {
3
+ if (path !== '/' && path.endsWith('/')) {
4
+ return path.replace(/\/+$/, '') || '/';
5
+ }
6
+ return null;
7
+ }
@@ -0,0 +1,8 @@
1
+
2
+ export function isSeller(user: any): boolean {
3
+ return !!(user && user.role === 'seller');
4
+ }
5
+
6
+ export function isSellerRoute(path: string): boolean {
7
+ return path.startsWith('/dashboard/');
8
+ }
@@ -0,0 +1,18 @@
1
+
2
+ export interface Session {
3
+ id: string;
4
+ date_created: string;
5
+ }
6
+
7
+ export function ensureSession(getCookie: (name: string) => any, setCookie: (name: string, value: string) => void, generateId: () => string) {
8
+ const session = getCookie('session');
9
+
10
+ if (!session) {
11
+ const newSession: Session = {
12
+ id: generateId(),
13
+ date_created: new Date().toISOString(),
14
+ };
15
+
16
+ setCookie('session', JSON.stringify(newSession));
17
+ }
18
+ }
package/src/plugins.ts CHANGED
@@ -7,7 +7,7 @@ export type AuthPluginOptions = {
7
7
  subscription?: boolean
8
8
  }
9
9
 
10
- export function getAuthPlugins(opts: AuthPluginOptions = {}) {
10
+ export function getAuthPlugins(opts: AuthPluginOptions = {}): any[] {
11
11
  const { subscription = true } = opts
12
12
 
13
13
  return [
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "compilerOptions": {
3
+ "declaration": true,
4
+ "emitDeclarationOnly": false,
5
+ "outDir": "dist",
6
+ "moduleResolution": "bundler",
7
+ "module": "ESNext",
8
+ "target": "ESNext",
9
+ "strict": true,
10
+ "jsx": "react-jsx",
11
+ "skipLibCheck": true,
12
+ "noEmitOnError": false
13
+ },
14
+ "include": ["src"]
15
+ }
package/index.ts DELETED
@@ -1 +0,0 @@
1
- export * from './src/useAuth'