@main12/auth-login 0.2.2 → 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.
@@ -0,0 +1,24 @@
1
+ import type React from 'react';
2
+ export interface AuthPagesProps {
3
+ /** The slug segments from the catch-all route, e.g. ['login'] or ['forgot-password'] */
4
+ slug?: string[];
5
+ /** Where to redirect after successful login / set-password */
6
+ redirectTo?: string;
7
+ /** Optional logo component */
8
+ logo?: React.ReactNode;
9
+ /** Custom handler for password login (email+password). If omitted, uses default fetch to /api/users/login */
10
+ onPasswordLogin?: (credentials: {
11
+ email: string;
12
+ password: string;
13
+ }) => Promise<void>;
14
+ /** Custom handler for signup. If omitted, uses default fetch to /api/auth/signup */
15
+ onSignup?: (data: {
16
+ name: string;
17
+ email: string;
18
+ }) => Promise<void>;
19
+ /** Base path prefix, defaults to '/auth'. Used to compute sibling URLs. */
20
+ basePath?: string;
21
+ /** Show Google OAuth buttons. If omitted, reads from plugin config. */
22
+ showGoogleOAuth?: boolean;
23
+ }
24
+ export default function AuthPages({ slug, redirectTo, logo, onPasswordLogin, onSignup, basePath, showGoogleOAuth, }: AuthPagesProps): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,86 @@
1
+ 'use client';
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ import LoginPage from './pages/LoginPage.js';
4
+ import SignupPage from './pages/SignupPage.js';
5
+ import ForgotPasswordPage from './pages/ForgotPasswordPage.js';
6
+ import VerifyOtpPage from './pages/VerifyOtpPage.js';
7
+ import SetPasswordPage from './pages/SetPasswordPage.js';
8
+ const defaultPasswordLogin = async ({ email, password })=>{
9
+ const res = await fetch('/api/users/login', {
10
+ method: 'POST',
11
+ headers: {
12
+ 'Content-Type': 'application/json'
13
+ },
14
+ body: JSON.stringify({
15
+ email,
16
+ password
17
+ })
18
+ });
19
+ if (!res.ok) {
20
+ const err = await res.json();
21
+ throw new Error(err.errors?.[0]?.message || 'Login failed');
22
+ }
23
+ };
24
+ const defaultSignup = async ({ name, email })=>{
25
+ const res = await fetch('/api/auth/signup', {
26
+ method: 'POST',
27
+ headers: {
28
+ 'Content-Type': 'application/json'
29
+ },
30
+ body: JSON.stringify({
31
+ name,
32
+ email
33
+ })
34
+ });
35
+ if (!res.ok) {
36
+ const err = await res.json();
37
+ throw new Error(err.message || 'Signup failed');
38
+ }
39
+ };
40
+ export default function AuthPages({ slug, redirectTo = '/admin', logo, onPasswordLogin = defaultPasswordLogin, onSignup = defaultSignup, basePath = '/auth', showGoogleOAuth }) {
41
+ const page = slug?.[0] ?? 'login';
42
+ const base = basePath.replace(/\/$/, '');
43
+ const shared = {
44
+ logo,
45
+ ...showGoogleOAuth !== undefined ? {
46
+ showGoogleOAuth
47
+ } : {}
48
+ };
49
+ switch(page){
50
+ case 'login':
51
+ return /*#__PURE__*/ _jsx(LoginPage, {
52
+ onPasswordLogin: onPasswordLogin,
53
+ redirectTo: redirectTo,
54
+ signupUrl: `${base}/signup`,
55
+ ...shared
56
+ });
57
+ case 'signup':
58
+ return /*#__PURE__*/ _jsx(SignupPage, {
59
+ onSignup: onSignup,
60
+ loginUrl: `${base}/login`,
61
+ ...shared
62
+ });
63
+ case 'forgot-password':
64
+ return /*#__PURE__*/ _jsx(ForgotPasswordPage, {
65
+ loginUrl: `${base}/login`,
66
+ ...shared
67
+ });
68
+ case 'verify-otp':
69
+ return /*#__PURE__*/ _jsx(VerifyOtpPage, {
70
+ loginUrl: `${base}/login`,
71
+ ...shared
72
+ });
73
+ case 'set-password':
74
+ return /*#__PURE__*/ _jsx(SetPasswordPage, {
75
+ redirectTo: redirectTo,
76
+ ...shared
77
+ });
78
+ default:
79
+ return /*#__PURE__*/ _jsx(LoginPage, {
80
+ onPasswordLogin: onPasswordLogin,
81
+ redirectTo: redirectTo,
82
+ signupUrl: `${base}/signup`,
83
+ ...shared
84
+ });
85
+ }
86
+ }
package/dist/config.d.ts CHANGED
@@ -9,6 +9,8 @@ export declare const pluginConfig: {
9
9
  logoUrl?: string;
10
10
  Logo?: React.ComponentType;
11
11
  googleOAuthEnabled: boolean;
12
+ routeRedirects: boolean;
13
+ authBasePath: string;
12
14
  };
13
15
  /** Call this client-side to mirror the server plugin config. */
14
16
  export declare function initClientConfig(opts: {
package/dist/config.js CHANGED
@@ -1,6 +1,8 @@
1
1
  export const pluginConfig = {
2
2
  style: 'tailwind',
3
- googleOAuthEnabled: true
3
+ googleOAuthEnabled: false,
4
+ routeRedirects: false,
5
+ authBasePath: '/auth'
4
6
  };
5
7
  /** Call this client-side to mirror the server plugin config. */ export function initClientConfig(opts) {
6
8
  if (opts.style) pluginConfig.style = opts.style;
@@ -10,6 +10,8 @@ export { default as SignupPage } from '../components/pages/SignupPage';
10
10
  export { default as ForgotPasswordPage } from '../components/pages/ForgotPasswordPage';
11
11
  export { default as VerifyOtpPage } from '../components/pages/VerifyOtpPage';
12
12
  export { default as SetPasswordPage } from '../components/pages/SetPasswordPage';
13
+ export { default as AuthPages } from '../components/AuthPages';
14
+ export type { AuthPagesProps } from '../components/AuthPages';
13
15
  export { AuthLayout } from '../components/AuthLayout';
14
16
  export { PoweredBy } from '../components/PoweredBy';
15
17
  export type { LoginStep, OTPPurpose, CheckEmailResponse, SendOtpResponse, VerifyOtpResponse, SetPasswordResponse, SignupResponse, PasswordStrengthResult, } from '../auth/domain/types';
@@ -19,6 +19,8 @@ export { default as SignupPage } from '../components/pages/SignupPage.js';
19
19
  export { default as ForgotPasswordPage } from '../components/pages/ForgotPasswordPage.js';
20
20
  export { default as VerifyOtpPage } from '../components/pages/VerifyOtpPage.js';
21
21
  export { default as SetPasswordPage } from '../components/pages/SetPasswordPage.js';
22
+ // Catch-all auth pages (single-file setup)
23
+ export { default as AuthPages } from '../components/AuthPages.js';
22
24
  // Layout & shared components
23
25
  export { AuthLayout } from '../components/AuthLayout.js';
24
26
  export { PoweredBy } from '../components/PoweredBy.js';
package/dist/index.d.ts CHANGED
@@ -27,6 +27,19 @@ export interface AuthLoginPluginOptions {
27
27
  * - `{ google: { enabled: false } }` → temporarily disable without losing config
28
28
  */
29
29
  providers?: AuthProvidersConfig;
30
+ /**
31
+ * Enable automatic route redirects from bare paths (e.g. /login)
32
+ * to the catch-all auth path (e.g. /auth/login).
33
+ *
34
+ * - `true` → enable redirects (consumer must re-export proxy from their proxy.ts)
35
+ * - `false` → disable redirects (consumer manages their own routes)
36
+ * - `{ basePath: '/auth' }` → enable with custom base path
37
+ *
38
+ * @default false
39
+ */
40
+ routeRedirects?: boolean | {
41
+ basePath?: string;
42
+ };
30
43
  }
31
44
  export declare const authLoginPlugin: (options?: AuthLoginPluginOptions) => (config: Config) => Config;
32
45
  export { pluginConfig };
package/dist/index.js CHANGED
@@ -51,6 +51,13 @@ export const authLoginPlugin = (options = {})=>(config)=>{
51
51
  pluginConfig.Logo = options.logo;
52
52
  }
53
53
  pluginConfig.googleOAuthEnabled = googleConfig.enabled;
54
+ // Route redirects config
55
+ if (options.routeRedirects) {
56
+ pluginConfig.routeRedirects = true;
57
+ if (typeof options.routeRedirects === 'object' && options.routeRedirects.basePath) {
58
+ pluginConfig.authBasePath = options.routeRedirects.basePath;
59
+ }
60
+ }
54
61
  // Register auth API endpoints
55
62
  config.endpoints = [
56
63
  ...config.endpoints || [],
@@ -0,0 +1,31 @@
1
+ import { NextResponse, type NextRequest } from 'next/server';
2
+ export interface AuthProxyOptions {
3
+ /** Base path where AuthPages catch-all is mounted. Defaults to '/auth' */
4
+ basePath?: string;
5
+ /** Routes to redirect. Defaults to all auth routes. */
6
+ routes?: string[];
7
+ }
8
+ /**
9
+ * Creates a proxy that redirects bare auth routes (e.g. /login)
10
+ * to the catch-all path (e.g. /auth/login).
11
+ *
12
+ * Usage in your app's proxy.ts (Next.js 16+):
13
+ * ```ts
14
+ * // One-liner — just re-export:
15
+ * export { proxy, config } from '@main12/auth-login/proxy'
16
+ * ```
17
+ * Or compose with your own logic:
18
+ * ```ts
19
+ * import { createAuthProxy } from '@main12/auth-login/proxy'
20
+ * const authProxy = createAuthProxy({ basePath: '/auth' })
21
+ * export function proxy(req) { return authProxy(req) }
22
+ * ```
23
+ *
24
+ * The basePath defaults to the value set in the plugin options
25
+ * (`routeRedirects: { basePath: '/auth' }`), or '/auth' if not configured.
26
+ */
27
+ export declare function createAuthProxy({ basePath, routes }?: AuthProxyOptions): (request: NextRequest) => NextResponse<unknown>;
28
+ export declare const proxy: (request: NextRequest) => NextResponse<unknown>;
29
+ export declare const config: {
30
+ matcher: string[];
31
+ };
package/dist/proxy.js ADDED
@@ -0,0 +1,62 @@
1
+ import { NextResponse } from 'next/server';
2
+ import { pluginConfig } from './config.js';
3
+ const AUTH_ROUTES = [
4
+ 'login',
5
+ 'signup',
6
+ 'forgot-password',
7
+ 'verify-otp',
8
+ 'set-password'
9
+ ];
10
+ /**
11
+ * Creates a proxy that redirects bare auth routes (e.g. /login)
12
+ * to the catch-all path (e.g. /auth/login).
13
+ *
14
+ * Usage in your app's proxy.ts (Next.js 16+):
15
+ * ```ts
16
+ * // One-liner — just re-export:
17
+ * export { proxy, config } from '@main12/auth-login/proxy'
18
+ * ```
19
+ * Or compose with your own logic:
20
+ * ```ts
21
+ * import { createAuthProxy } from '@main12/auth-login/proxy'
22
+ * const authProxy = createAuthProxy({ basePath: '/auth' })
23
+ * export function proxy(req) { return authProxy(req) }
24
+ * ```
25
+ *
26
+ * The basePath defaults to the value set in the plugin options
27
+ * (`routeRedirects: { basePath: '/auth' }`), or '/auth' if not configured.
28
+ */ export function createAuthProxy({ basePath, routes = AUTH_ROUTES } = {}) {
29
+ const base = (basePath ?? pluginConfig.authBasePath).replace(/\/$/, '');
30
+ const routeSet = new Set(routes);
31
+ return function proxy(request) {
32
+ // If routeRedirects is disabled in plugin config, pass through
33
+ if (!pluginConfig.routeRedirects && !basePath) {
34
+ return NextResponse.next();
35
+ }
36
+ const { pathname } = request.nextUrl;
37
+ // Always redirect /admin/login to the plugin login page
38
+ if (pathname === '/admin/login' || pathname === '/admin/login/') {
39
+ const url = request.nextUrl.clone();
40
+ url.pathname = `${base}/login`;
41
+ return NextResponse.redirect(url);
42
+ }
43
+ const segment = pathname.replace(/^\//, '').replace(/\/$/, '');
44
+ if (routeSet.has(segment) && !pathname.startsWith(`${base}/`)) {
45
+ const url = request.nextUrl.clone();
46
+ url.pathname = `${base}/${segment}`;
47
+ return NextResponse.redirect(url);
48
+ }
49
+ return NextResponse.next();
50
+ };
51
+ }
52
+ export const proxy = createAuthProxy();
53
+ export const config = {
54
+ matcher: [
55
+ '/admin/login',
56
+ '/login',
57
+ '/signup',
58
+ '/forgot-password',
59
+ '/verify-otp',
60
+ '/set-password'
61
+ ]
62
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@main12/auth-login",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "Reusable Payload CMS auth plugin — login, signup, OTP, forgot password, branded emails, Powered by Main12",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -19,6 +19,11 @@
19
19
  "import": "./dist/exports/rsc.js",
20
20
  "types": "./dist/exports/rsc.d.ts",
21
21
  "default": "./dist/exports/rsc.js"
22
+ },
23
+ "./proxy": {
24
+ "import": "./dist/proxy.js",
25
+ "types": "./dist/proxy.d.ts",
26
+ "default": "./dist/proxy.js"
22
27
  }
23
28
  },
24
29
  "main": "./dist/index.js",