{"id":21618634,"url":"https://github.com/masfana/masfana-mongodb-example-auth","last_synced_at":"2025-07-14T01:03:36.890Z","repository":{"id":256017448,"uuid":"854122577","full_name":"MasFana/masfana-mongodb-example-auth","owner":"MasFana","description":"Hono Authentication Example App using masfana-mongodb-api-sdk, Cloudflare, and Cloudflare Workers","archived":false,"fork":false,"pushed_at":"2024-09-08T13:34:18.000Z","size":36,"stargazers_count":7,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-09T22:43:42.587Z","etag":null,"topics":["cloudflare","honojs","mongodb"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MasFana.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2024-09-08T13:01:34.000Z","updated_at":"2025-02-02T12:37:38.000Z","dependencies_parsed_at":null,"dependency_job_id":"59d57bf5-0a39-4c59-afdd-fd0035849ff3","html_url":"https://github.com/MasFana/masfana-mongodb-example-auth","commit_stats":null,"previous_names":["masfana/masfana-mongodb-example-auth"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/MasFana/masfana-mongodb-example-auth","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MasFana%2Fmasfana-mongodb-example-auth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MasFana%2Fmasfana-mongodb-example-auth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MasFana%2Fmasfana-mongodb-example-auth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MasFana%2Fmasfana-mongodb-example-auth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MasFana","download_url":"https://codeload.github.com/MasFana/masfana-mongodb-example-auth/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MasFana%2Fmasfana-mongodb-example-auth/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265228803,"owners_count":23731084,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["cloudflare","honojs","mongodb"],"created_at":"2024-11-24T23:06:15.773Z","updated_at":"2025-07-14T01:03:36.840Z","avatar_url":"https://github.com/MasFana.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# Hono Authentication Example App using `masfana-mongodb-api-sdk`, Cloudflare, and Cloudflare Workers\n\nThis project is an example of a lightweight authentication system built using the following technologies:\n\n-   **Hono Framework**: A fast web framework for the Edge.\n-   [masfana-mongodb-api-sdk](https://www.npmjs.com/package/masfana-mongodb-api-sdk?activeTab=readme): A MongoDB API SDK for handling MongoDB operations.\n-   **Cloudflare Workers**: Serverless execution environment for running apps at the Edge.\n-   **Hono Sessions**: Middleware to manage user sessions stored as cookies.\n\n## Features\n\n-   User registration and login with credentials stored in MongoDB.\n-   User sessions using cookies, with session expiration.\n-   Simple protected route example requiring authentication.\n-   Logout functionality to clear user sessions.\n-   Deployed on Cloudflare Workers for edge performance.\n\n## Prerequisites\n\nBefore running the application, you will need:\n\n1.  **Cloudflare Workers Account**: Set up and configure Cloudflare Workers.\n2.  **MongoDB API Key**: Create an API key and set up the `masfana-mongodb-api-sdk` with your MongoDB instance.\n3.  **Hono Framework**: This is used to create the web application.\n\n## Getting Started\nInstallation\n**1. Clone the repository**:\n```bash\ngit clone \u003crepository-url\u003e\ncd \u003cproject-directory\u003e\n```\n**2. Install dependencies**:\n\nIf you're using a package manager like `npm` or `yarn`, install the necessary dependencies:\n```bash\nnpm install hono masfana-mongodb-api-sdk hono-sessions\n```\n**3. Set up MongoDB connection**:\n\nIn your application, replace the MongoDB connection details with your own:\n```javascript\nconst client = new MongoDBAPI\u003cUser\u003e({\n  MONGO_API_URL: \"your-mongo-api-url\",\n  MONGO_API_KEY: \"your-mongo-api-key\",\n  DATABASE: \"your-database\",\n  COLLECTION: \"your-collection\",\n  DATA_SOURCE: \"your-data-source\",\n});\n```\n**4. Deploy to Cloudflare Workers**:\n\nYou'll need to configure your Cloudflare Workers environment. Follow the Cloudflare Workers documentation for deployment.\n\n### Project Structure\n\n-   `index.ts`: This file contains the main application logic, including session management, user registration, login, logout, and protected routes.\n-   `MongoDBAPI`: This is the MongoDB client used to handle CRUD operations with the MongoDB database.\n\n### Routes\n\n1.  **Registration Route** (`POST /register`):\n    \n    -   Allows users to register by providing a username and password.\n    -   Stores user credentials in the MongoDB database.\n2.  **Login Route** (`POST /login`):\n    \n    -   Verifies user credentials against the MongoDB database.\n    -   If successful, a session is created for the user, storing their ID in a session cookie.\n3.  **Logout Route** (`GET /logout`):\n    \n    -   Clears the session and logs the user out.\n4.  **Protected Route** (`GET /protected`):\n    \n    -   Only accessible to authenticated users with an active session.\n    -   Returns a personalized message based on the session data.\n5.  **Home Route** (`GET /`):\n    \n    -   Displays basic user information and login/registration forms.\n    -   Accessible to both authenticated and non-authenticated users.\n\n### Security\n\n-   **Session Management**: Sessions are managed using the `hono-sessions` library, with cookies securely stored and marked as `HTTP-only`.\n-   **Encryption Key**: Ensure you replace the encryption key with a secure, random string.\n\n### Example Usage\n\nOnce the app is deployed, users can:\n\n1.  Register a new account by entering a username and password.\n2.  Log in using their credentials, which will create a session.\n3.  Access protected content by visiting the protected route, available only after logging in.\n4.  Log out, which will clear their session and log them out of the app.\n\n## Deployment\n\nTo deploy this application on Cloudflare Workers:\n\n1.  Set up a Cloudflare Workers environment and install Wrangler (`npm install -g wrangler`).\n    \n2.  Deploy the application using:\n```bash\nwrangler publish\n```\n3.  Your application will be deployed at your Cloudflare Workers URL, accessible globally.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmasfana%2Fmasfana-mongodb-example-auth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmasfana%2Fmasfana-mongodb-example-auth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmasfana%2Fmasfana-mongodb-example-auth/lists"}