@allem-sdk/auth 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ahmed Allem
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,85 @@
1
+ <p align="center">
2
+ <img src="https://raw.githubusercontent.com/kingofmit/allem-sdk/main/.github/AllemSDK.png" alt="Allem SDK" />
3
+ </p>
4
+
5
+ <p align="center">
6
+ <a href="https://github.com/kingofmit/allem-sdk/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="License" /></a>
7
+ <img src="https://img.shields.io/badge/react-18+-61dafb" alt="React 18+" />
8
+ <img src="https://img.shields.io/badge/typescript-strict-blue" alt="TypeScript" />
9
+ </p>
10
+
11
+ # @allem-sdk/auth
12
+
13
+ Authentication helpers for React. Provider-agnostic via an adapter interface — works with any auth backend.
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install @allem-sdk/auth
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ```tsx
24
+ import { AuthProvider, useAuth, ProtectedRoute } from "@allem-sdk/auth";
25
+
26
+ // Define an adapter for your auth provider
27
+ const myAuthAdapter = {
28
+ getSession: async () => {
29
+ const res = await fetch("/api/auth/session");
30
+ if (!res.ok) return null;
31
+ return res.json();
32
+ },
33
+ signIn: async (credentials) => {
34
+ const res = await fetch("/api/auth/signin", {
35
+ method: "POST",
36
+ body: JSON.stringify(credentials),
37
+ });
38
+ return res.json();
39
+ },
40
+ signOut: async () => {
41
+ await fetch("/api/auth/signout", { method: "POST" });
42
+ },
43
+ };
44
+
45
+ function App() {
46
+ return (
47
+ <AuthProvider adapter={myAuthAdapter}>
48
+ <ProtectedRoute
49
+ fallback={<LoginPage />}
50
+ loadingFallback={<Spinner />}
51
+ >
52
+ <Dashboard />
53
+ </ProtectedRoute>
54
+ </AuthProvider>
55
+ );
56
+ }
57
+
58
+ function Dashboard() {
59
+ const { user, status, signOut } = useAuth();
60
+ return <button onClick={signOut}>Sign out, {user?.name}</button>;
61
+ }
62
+ ```
63
+
64
+ ## Exports
65
+
66
+ | Export | Type | Description |
67
+ |--------|------|-------------|
68
+ | `AuthProvider` | Component | Context provider with automatic session management |
69
+ | `useAuth` | Hook | Returns `user`, `status`, `isAuthenticated`, `isLoading`, `signIn`, `signOut` |
70
+ | `useSession` | Hook | Returns `session` object and `update` function |
71
+ | `ProtectedRoute` | Component | Renders children only when authenticated, with fallback support |
72
+
73
+ ## Part of [Allem SDK](https://github.com/kingofmit/allem-sdk)
74
+
75
+ This package can be used standalone or as part of the full SDK. Install `allem-sdk` to get all packages in one install.
76
+
77
+ ## Support
78
+
79
+ If you find Allem SDK useful, consider supporting its development:
80
+
81
+ <a href="https://buymeacoffee.com/kingofmit" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" height="50" /></a>
82
+
83
+ ## License
84
+
85
+ [MIT](https://github.com/kingofmit/allem-sdk/blob/main/LICENSE) - [Ahmed Allem](https://kingallem.com)
@@ -0,0 +1,51 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { ReactNode } from 'react';
3
+
4
+ interface AuthUser {
5
+ id: string;
6
+ email?: string;
7
+ name?: string;
8
+ image?: string;
9
+ [key: string]: unknown;
10
+ }
11
+ interface AuthSession {
12
+ user: AuthUser | null;
13
+ token: string | null;
14
+ expiresAt: number | null;
15
+ }
16
+ interface AuthAdapter {
17
+ getSession: () => Promise<AuthSession>;
18
+ signIn: (credentials: Record<string, string>) => Promise<AuthSession>;
19
+ signOut: () => Promise<void>;
20
+ }
21
+ type AuthStatus = "loading" | "authenticated" | "unauthenticated";
22
+ interface AuthProviderProps {
23
+ adapter: AuthAdapter;
24
+ children: ReactNode;
25
+ }
26
+ declare function AuthProvider({ adapter, children }: AuthProviderProps): react_jsx_runtime.JSX.Element;
27
+
28
+ interface UseAuthReturn {
29
+ user: AuthUser | null;
30
+ status: AuthStatus;
31
+ isAuthenticated: boolean;
32
+ isLoading: boolean;
33
+ signIn: (credentials: Record<string, string>) => Promise<void>;
34
+ signOut: () => Promise<void>;
35
+ }
36
+ declare function useAuth(): UseAuthReturn;
37
+
38
+ interface UseSessionReturn {
39
+ session: AuthSession | null;
40
+ update: () => Promise<void>;
41
+ }
42
+ declare function useSession(): UseSessionReturn;
43
+
44
+ interface ProtectedRouteProps {
45
+ children: ReactNode;
46
+ fallback?: ReactNode;
47
+ loadingFallback?: ReactNode;
48
+ }
49
+ declare function ProtectedRoute({ children, fallback, loadingFallback, }: ProtectedRouteProps): react_jsx_runtime.JSX.Element;
50
+
51
+ export { type AuthAdapter, AuthProvider, type AuthProviderProps, type AuthSession, type AuthStatus, type AuthUser, ProtectedRoute, type ProtectedRouteProps, type UseAuthReturn, type UseSessionReturn, useAuth, useSession };
@@ -0,0 +1,51 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { ReactNode } from 'react';
3
+
4
+ interface AuthUser {
5
+ id: string;
6
+ email?: string;
7
+ name?: string;
8
+ image?: string;
9
+ [key: string]: unknown;
10
+ }
11
+ interface AuthSession {
12
+ user: AuthUser | null;
13
+ token: string | null;
14
+ expiresAt: number | null;
15
+ }
16
+ interface AuthAdapter {
17
+ getSession: () => Promise<AuthSession>;
18
+ signIn: (credentials: Record<string, string>) => Promise<AuthSession>;
19
+ signOut: () => Promise<void>;
20
+ }
21
+ type AuthStatus = "loading" | "authenticated" | "unauthenticated";
22
+ interface AuthProviderProps {
23
+ adapter: AuthAdapter;
24
+ children: ReactNode;
25
+ }
26
+ declare function AuthProvider({ adapter, children }: AuthProviderProps): react_jsx_runtime.JSX.Element;
27
+
28
+ interface UseAuthReturn {
29
+ user: AuthUser | null;
30
+ status: AuthStatus;
31
+ isAuthenticated: boolean;
32
+ isLoading: boolean;
33
+ signIn: (credentials: Record<string, string>) => Promise<void>;
34
+ signOut: () => Promise<void>;
35
+ }
36
+ declare function useAuth(): UseAuthReturn;
37
+
38
+ interface UseSessionReturn {
39
+ session: AuthSession | null;
40
+ update: () => Promise<void>;
41
+ }
42
+ declare function useSession(): UseSessionReturn;
43
+
44
+ interface ProtectedRouteProps {
45
+ children: ReactNode;
46
+ fallback?: ReactNode;
47
+ loadingFallback?: ReactNode;
48
+ }
49
+ declare function ProtectedRoute({ children, fallback, loadingFallback, }: ProtectedRouteProps): react_jsx_runtime.JSX.Element;
50
+
51
+ export { type AuthAdapter, AuthProvider, type AuthProviderProps, type AuthSession, type AuthStatus, type AuthUser, ProtectedRoute, type ProtectedRouteProps, type UseAuthReturn, type UseSessionReturn, useAuth, useSession };
package/dist/index.js ADDED
@@ -0,0 +1,120 @@
1
+ "use client";
2
+ "use strict";
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/index.ts
22
+ var index_exports = {};
23
+ __export(index_exports, {
24
+ AuthProvider: () => AuthProvider,
25
+ ProtectedRoute: () => ProtectedRoute,
26
+ useAuth: () => useAuth,
27
+ useSession: () => useSession
28
+ });
29
+ module.exports = __toCommonJS(index_exports);
30
+
31
+ // src/AuthProvider.tsx
32
+ var import_react = require("react");
33
+ var import_jsx_runtime = require("react/jsx-runtime");
34
+ var AuthContext = (0, import_react.createContext)(null);
35
+ function AuthProvider({ adapter, children }) {
36
+ const [session, setSession] = (0, import_react.useState)(null);
37
+ const [status, setStatus] = (0, import_react.useState)("loading");
38
+ const refresh = (0, import_react.useCallback)(async () => {
39
+ try {
40
+ const s = await adapter.getSession();
41
+ setSession(s);
42
+ setStatus(s.user ? "authenticated" : "unauthenticated");
43
+ } catch {
44
+ setSession(null);
45
+ setStatus("unauthenticated");
46
+ }
47
+ }, [adapter]);
48
+ (0, import_react.useEffect)(() => {
49
+ refresh();
50
+ }, [refresh]);
51
+ const signIn = (0, import_react.useCallback)(
52
+ async (credentials) => {
53
+ setStatus("loading");
54
+ const s = await adapter.signIn(credentials);
55
+ setSession(s);
56
+ setStatus(s.user ? "authenticated" : "unauthenticated");
57
+ },
58
+ [adapter]
59
+ );
60
+ const signOut = (0, import_react.useCallback)(async () => {
61
+ await adapter.signOut();
62
+ setSession(null);
63
+ setStatus("unauthenticated");
64
+ }, [adapter]);
65
+ const value = {
66
+ user: session?.user ?? null,
67
+ session,
68
+ status,
69
+ signIn,
70
+ signOut,
71
+ refresh
72
+ };
73
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AuthContext.Provider, { value, children });
74
+ }
75
+ function useAuthContext() {
76
+ const ctx = (0, import_react.useContext)(AuthContext);
77
+ if (!ctx) {
78
+ throw new Error("useAuth must be used within an <AuthProvider>");
79
+ }
80
+ return ctx;
81
+ }
82
+
83
+ // src/useAuth.ts
84
+ function useAuth() {
85
+ const { user, status, signIn, signOut } = useAuthContext();
86
+ return {
87
+ user,
88
+ status,
89
+ isAuthenticated: status === "authenticated",
90
+ isLoading: status === "loading",
91
+ signIn,
92
+ signOut
93
+ };
94
+ }
95
+
96
+ // src/useSession.ts
97
+ function useSession() {
98
+ const { session, refresh } = useAuthContext();
99
+ return { session, update: refresh };
100
+ }
101
+
102
+ // src/ProtectedRoute.tsx
103
+ var import_jsx_runtime2 = require("react/jsx-runtime");
104
+ function ProtectedRoute({
105
+ children,
106
+ fallback = null,
107
+ loadingFallback = null
108
+ }) {
109
+ const { status } = useAuthContext();
110
+ if (status === "loading") return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: loadingFallback });
111
+ if (status === "unauthenticated") return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: fallback });
112
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children });
113
+ }
114
+ // Annotate the CommonJS export names for ESM import in node:
115
+ 0 && (module.exports = {
116
+ AuthProvider,
117
+ ProtectedRoute,
118
+ useAuth,
119
+ useSession
120
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,91 @@
1
+ "use client";
2
+
3
+ // src/AuthProvider.tsx
4
+ import { createContext, useContext, useState, useCallback, useEffect } from "react";
5
+ import { jsx } from "react/jsx-runtime";
6
+ var AuthContext = createContext(null);
7
+ function AuthProvider({ adapter, children }) {
8
+ const [session, setSession] = useState(null);
9
+ const [status, setStatus] = useState("loading");
10
+ const refresh = useCallback(async () => {
11
+ try {
12
+ const s = await adapter.getSession();
13
+ setSession(s);
14
+ setStatus(s.user ? "authenticated" : "unauthenticated");
15
+ } catch {
16
+ setSession(null);
17
+ setStatus("unauthenticated");
18
+ }
19
+ }, [adapter]);
20
+ useEffect(() => {
21
+ refresh();
22
+ }, [refresh]);
23
+ const signIn = useCallback(
24
+ async (credentials) => {
25
+ setStatus("loading");
26
+ const s = await adapter.signIn(credentials);
27
+ setSession(s);
28
+ setStatus(s.user ? "authenticated" : "unauthenticated");
29
+ },
30
+ [adapter]
31
+ );
32
+ const signOut = useCallback(async () => {
33
+ await adapter.signOut();
34
+ setSession(null);
35
+ setStatus("unauthenticated");
36
+ }, [adapter]);
37
+ const value = {
38
+ user: session?.user ?? null,
39
+ session,
40
+ status,
41
+ signIn,
42
+ signOut,
43
+ refresh
44
+ };
45
+ return /* @__PURE__ */ jsx(AuthContext.Provider, { value, children });
46
+ }
47
+ function useAuthContext() {
48
+ const ctx = useContext(AuthContext);
49
+ if (!ctx) {
50
+ throw new Error("useAuth must be used within an <AuthProvider>");
51
+ }
52
+ return ctx;
53
+ }
54
+
55
+ // src/useAuth.ts
56
+ function useAuth() {
57
+ const { user, status, signIn, signOut } = useAuthContext();
58
+ return {
59
+ user,
60
+ status,
61
+ isAuthenticated: status === "authenticated",
62
+ isLoading: status === "loading",
63
+ signIn,
64
+ signOut
65
+ };
66
+ }
67
+
68
+ // src/useSession.ts
69
+ function useSession() {
70
+ const { session, refresh } = useAuthContext();
71
+ return { session, update: refresh };
72
+ }
73
+
74
+ // src/ProtectedRoute.tsx
75
+ import { Fragment, jsx as jsx2 } from "react/jsx-runtime";
76
+ function ProtectedRoute({
77
+ children,
78
+ fallback = null,
79
+ loadingFallback = null
80
+ }) {
81
+ const { status } = useAuthContext();
82
+ if (status === "loading") return /* @__PURE__ */ jsx2(Fragment, { children: loadingFallback });
83
+ if (status === "unauthenticated") return /* @__PURE__ */ jsx2(Fragment, { children: fallback });
84
+ return /* @__PURE__ */ jsx2(Fragment, { children });
85
+ }
86
+ export {
87
+ AuthProvider,
88
+ ProtectedRoute,
89
+ useAuth,
90
+ useSession
91
+ };
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@allem-sdk/auth",
3
+ "version": "0.1.0",
4
+ "description": "Authentication helpers for React — session, protected routes, social login",
5
+ "license": "MIT",
6
+ "author": "Ahmed Allem",
7
+ "main": "./dist/index.js",
8
+ "module": "./dist/index.mjs",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.mjs",
14
+ "require": "./dist/index.js"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "sideEffects": false,
21
+ "devDependencies": {
22
+ "@testing-library/react": "^16.0.0",
23
+ "@types/react": "^19.0.0",
24
+ "jsdom": "^25.0.0",
25
+ "react": "^19.0.0",
26
+ "react-dom": "^19.0.0",
27
+ "tsup": "^8.4.0",
28
+ "typescript": "^5.8.0",
29
+ "vitest": "^3.0.0"
30
+ },
31
+ "peerDependencies": {
32
+ "react": ">=18.0.0"
33
+ },
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "https://github.com/kingofmit/allem-sdk.git",
37
+ "directory": "packages/auth"
38
+ },
39
+ "keywords": [
40
+ "allem-sdk",
41
+ "react",
42
+ "hooks",
43
+ "auth",
44
+ "authentication",
45
+ "session",
46
+ "protected-route"
47
+ ],
48
+ "publishConfig": {
49
+ "access": "public"
50
+ },
51
+ "scripts": {
52
+ "build": "tsup",
53
+ "dev": "tsup --watch",
54
+ "clean": "rm -rf dist",
55
+ "test": "vitest run"
56
+ }
57
+ }