{"id":28911295,"url":"https://github.com/srcbookdev/mocha-users-service-sdk","last_synced_at":"2026-04-18T02:32:46.648Z","repository":{"id":300132530,"uuid":"1005253758","full_name":"srcbookdev/mocha-users-service-sdk","owner":"srcbookdev","description":"An SDK for the Mocha Users Service, providing authentication and user management functionality for Mocha apps.","archived":false,"fork":false,"pushed_at":"2025-06-27T23:28:49.000Z","size":18,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-28T00:25:08.327Z","etag":null,"topics":["auth","hono","mocha","users"],"latest_commit_sha":null,"homepage":"https://getmocha.com","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/srcbookdev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-06-19T23:55:37.000Z","updated_at":"2025-06-27T23:28:52.000Z","dependencies_parsed_at":"2025-06-20T03:30:19.232Z","dependency_job_id":"464015c1-bb8a-4359-95a7-b81dbef7aef2","html_url":"https://github.com/srcbookdev/mocha-users-service-sdk","commit_stats":null,"previous_names":["srcbookdev/mocha-users-service-sdk"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/srcbookdev/mocha-users-service-sdk","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srcbookdev%2Fmocha-users-service-sdk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srcbookdev%2Fmocha-users-service-sdk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srcbookdev%2Fmocha-users-service-sdk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srcbookdev%2Fmocha-users-service-sdk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/srcbookdev","download_url":"https://codeload.github.com/srcbookdev/mocha-users-service-sdk/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/srcbookdev%2Fmocha-users-service-sdk/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31953782,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-18T00:39:45.007Z","status":"online","status_checked_at":"2026-04-18T02:00:07.018Z","response_time":103,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["auth","hono","mocha","users"],"created_at":"2025-06-21T19:02:13.826Z","updated_at":"2026-04-18T02:32:46.632Z","avatar_url":"https://github.com/srcbookdev.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @getmocha/users-service\n\nAn SDK for interacting with the Mocha Users Service, providing authentication and user management functionality for Hono and React applications.\n\n## Installation\n\n```bash\nnpm install @getmocha/users-service\n```\n\n## Features\n\n- Google OAuth authentication via the Mocha Users Service\n- Session management\n- User information retrieval\n- Hono middleware for protected routes\n- A React context provider and associated hook for managing user and authentication state in React apps.\n\n## Usage\n\n### Backend Authentication Flow\n\nUse the @getmocha/users-service/backend export for backend functionality and Hono support.\n\n1. **Get OAuth Redirect URL**\n\n```typescript\nimport { getOAuthRedirectUrl } from '@getmocha/users-service/backend';\n\n// In your API handler\napp.get('/api/oauth/google/redirect_url', async (c) =\u003e {\n  const redirectUrl = await getOAuthRedirectUrl('google', {\n    apiUrl: c.env.MOCHA_USERS_SERVICE_API_URL,\n    apiKey: c.env.MOCHA_USERS_SERVICE_API_KEY,\n  });\n\n  return c.json({ redirectUrl }, 200);\n});\n```\n\n2. **Exchange Code for Session Token**\n\n```typescript\nimport {\n  exchangeCodeForSessionToken,\n  MOCHA_SESSION_TOKEN_COOKIE_NAME,\n} from '@getmocha/users-service/backend';\nimport { setCookie } from 'hono/cookie';\n\n// In your API handler for the callback\napp.post('/api/sessions', async (c) =\u003e {\n  const { code } = await c.req.json();\n\n  const sessionToken = await exchangeCodeForSessionToken(code, {\n    apiUrl: c.env.MOCHA_USERS_SERVICE_API_URL,\n    apiKey: c.env.MOCHA_USERS_SERVICE_API_KEY,\n  });\n\n  // Set the session cookie\n  setCookie(c, MOCHA_SESSION_TOKEN_COOKIE_NAME, sessionToken, {\n    httpOnly: true,\n    path: '/',\n    sameSite: 'none',\n    secure: true,\n    maxAge: 60 * 24 * 60 * 60, // 60 days\n  });\n\n  return c.json({ success: true });\n});\n```\n\n3. **Protect Routes with Auth Middleware**\n\n```typescript\nimport { authMiddleware } from '@getmocha/users-service/backend';\nimport { Hono } from 'hono';\n\n// Create your Hono app\nconst app = new Hono();\n\n// Use the middleware to protect routes\napp.get('/api/protected-route', authMiddleware, async (c) =\u003e {\n  // The user is available in the context\n  const user = c.get('user');\n\n  return c.json({ message: `Hello, ${user.email}!` });\n});\n```\n\n4. **Get Current User**\n\n```typescript\nimport { authMiddleware } from '@getmocha/users-service/backend';\nimport { Hono } from 'hono';\n\n// Create your Hono app\nconst app = new Hono();\n\napp.get('/api/users/me', authMiddleware, async (c) =\u003e {\n  const user = c.get('user');\n  return c.json(user);\n});\n```\n\n5. **Logout**\n\n```typescript\nimport { deleteSession } from '@getmocha/users-service/backend';\nimport { Hono } from 'hono';\nimport { getCookie, setCookie } from 'hono/cookie';\n\n// Create your Hono app\nconst app = new Hono();\n\napp.get('/api/logout', async (c) =\u003e {\n  const sessionToken = getCookie(c, MOCHA_SESSION_TOKEN_COOKIE_NAME);\n\n  if (typeof sessionToken === 'string') {\n    await deleteSession(sessionToken, {\n      apiUrl: c.env.MOCHA_USERS_SERVICE_API_URL,\n      apiKey: c.env.MOCHA_USERS_SERVICE_API_KEY,\n    });\n  }\n\n  // Delete cookie by setting max age to 0 These params must match the ones\n  // used when setting the cookie, except max age (0) and the cookie value ('').\n  setCookie(c, MOCHA_SESSION_TOKEN_COOKIE_NAME, '', {\n    httpOnly: true,\n    path: '/',\n    sameSite: 'none',\n    secure: true,\n    maxAge: 0,\n  });\n\n  return c.json({ success: true }, 200);\n});\n```\n\n### Shared\n\nUse the @getmocha/users-service/shared export for functionality that can be used on the frontend or backend.\n\nFor example, the `MochaUser` TypeScript type.\n\n```typescript\nimport type { MochaUser } from '@getmocha/users-service/shared';\nimport { Hono } from 'hono';\n\ntype Variables = {\n  user?: MochaUser;\n};\n\nconst app = new Hono\u003c{ Bindings: Env; Variables: Variables }\u003e();\n```\n\n### Frontend React package\n\nUse the @getmocha/users-service/react export for a React context provider and hook that provides user and authentication management.\n\n```tsx\nimport { AuthProvider } from '@getmocha/users-service/react';\n\nexport default function App() {\n  return (\n    \u003cAuthProvider\u003e\n      \u003cRouter\u003e\n        \u003cRoutes\u003e\n          \u003cRoute path=\"/\" element={\u003cHomePage /\u003e} /\u003e\n          \u003cRoute path=\"/auth/callback\" element={\u003cAuthCallbackPage /\u003e} /\u003e\n        \u003c/Routes\u003e\n      \u003c/Router\u003e\n    \u003c/AuthProvider\u003e\n  );\n}\n```\n\nThen you can use the exported `useAuth` hook.\n\n```tsx\nconst {\n  user,\n  isPending,\n  isFetching,\n  fetchUser,\n  redirectToUrl,\n  exchangeCodeForSessionToken,\n  logout,\n} = useAuth();\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsrcbookdev%2Fmocha-users-service-sdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsrcbookdev%2Fmocha-users-service-sdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsrcbookdev%2Fmocha-users-service-sdk/lists"}