{"id":34743324,"url":"https://github.com/daniellehuisman/koa-simple-oauth","last_synced_at":"2025-12-25T04:27:35.193Z","repository":{"id":27903907,"uuid":"115464033","full_name":"DanielleHuisman/koa-simple-oauth","owner":"DanielleHuisman","description":"Simple OAuth2 authentication middleware for Koa.","archived":false,"fork":false,"pushed_at":"2023-07-19T07:04:50.000Z","size":799,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-04-13T19:24:46.333Z","etag":null,"topics":["koa","nodejs","oauth2"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DanielleHuisman.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-12-27T00:09:08.000Z","updated_at":"2023-01-07T21:44:56.000Z","dependencies_parsed_at":"2023-01-14T07:42:43.357Z","dependency_job_id":null,"html_url":"https://github.com/DanielleHuisman/koa-simple-oauth","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/DanielleHuisman/koa-simple-oauth","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DanielleHuisman%2Fkoa-simple-oauth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DanielleHuisman%2Fkoa-simple-oauth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DanielleHuisman%2Fkoa-simple-oauth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DanielleHuisman%2Fkoa-simple-oauth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DanielleHuisman","download_url":"https://codeload.github.com/DanielleHuisman/koa-simple-oauth/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DanielleHuisman%2Fkoa-simple-oauth/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28019441,"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","status":"online","status_checked_at":"2025-12-25T02:00:05.988Z","response_time":58,"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":["koa","nodejs","oauth2"],"created_at":"2025-12-25T04:27:32.221Z","updated_at":"2025-12-25T04:27:35.187Z","avatar_url":"https://github.com/DanielleHuisman.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# koa-simple-oauth\n\n[![koa-simple-oauth](https://img.shields.io/npm/v/koa-simple-oauth.svg)](https://www.npmjs.com/package/koa-simple-oauth)\n\nSimple OAuth2 authentication middleware for Koa. Internally uses [simple-oauth2](https://github.com/lelylan/simple-oauth2) and [Node Fetch API](https://github.com/bitinn/node-fetch).\n\n## Requirements\n- [Koa Session middleware](https://github.com/koajs/session)\n- Something to mount middleware, examples:\n  - [Koa Router](https://github.com/alexmingoia/koa-router) *recommended*\n  - [Koa Mount](https://github.com/koajs/mount)\n  - [Koa Route](https://github.com/koajs/route)\n\n## Installation\n```bash\nyarn add koa-simple-oauth\n```\n\n## Usage\n### Requirements\n```javascript\nimport Koa from 'koa';\nimport session from 'koa-session';\nimport simpleOauth from 'koa-simple-oauth';\n\n// Initialize Koa\nconst app = new Koa();\n\n// Initialize Koa Session\napp.keys = ['secretSessionKey'];\nconst sessionConfig = {};\napp.use(session(sessionConfig, app));\n```\n\n### Configuration\n```javascript\nconst oauthConfig = {\n    // Client ID and secret for OAuth provier\n    clientId: 'abcdefgh1234',\n    clientSecret: '5678mnopqrst',\n\n    // Base URL for OAuth provider\n    url: 'https://oauth.example.com/api/v1',\n\n    // Redirect URL for this application, i.e. where you mounted the authorized middleware\n    redirectUrl: 'https://myapp.example.com/api/v1/oauth/authorized',\n\n    // User API URL and HTTP method\n    userUrl: 'https://oauth.example.com/api/v1/me',\n    userMethod: 'GET',\n\n    // Get user from API response or return an error\n    user: (data) =\u003e {\n        const user = data.user;\n        if (!user.isAdmin) {\n            return 'not_admin';\n        }\n        return user;\n    },\n\n    // These options are passed to simple-oauth2, see https://github.com/lelylan/simple-oauth2\n    oauthOptions: {},\n\n    // Default redirect URL on success (or set the redirect query parameter)\n    redirectSuccessUrl: 'https://myapp.example.com/login/success',\n\n    // Redirect URL on error (will add an error message as error query parameter by default, e.g. ?error=invalid_code_or_state)\n    redirectErrorUrl: 'https://myapp.example.com/login/error',\n\n    // Don't send an error query parameter to the error redirect URL (see above)\n    disableErrorReason: false,\n\n    // Called on successful API response (e.g. whoami endpoint)\n    onSuccess: (ctx, data, status = 200) =\u003e {\n        ctx.status = status;\n        ctx.body = typeof data === 'object' ? JSON.stringify(data) : data;\n    },\n\n    // Called on error API response (e.g. whoami endpoint)\n    onError: (ctx, status, message, err) =\u003e {\n        ctx.status = status;\n        ctx.body = `${message}: ${err.message}`;\n    },\n\n    // Called whenever on error occurs\n    logError: (err) =\u003e {\n        if (err.message !== 'Not logged in') {\n            console.error(err);\n        }\n    },\n\n    // Route configuration (only works if a router is provided)\n    routes: {\n        login: '/login',\n        authorized: '/authorized',\n        whoami: '/whoami',\n        logout: '/logout'\n    }\n};\n```\n\n### With Koa Router (recommended)\n```javascript\nimport Router from 'koa-router';\n\n// Initialize Koa Router\nconst router = new Router();\n\n// Initialize Koa Simple OAuth\n// Adds all required middleware to the router\nconst {isLoggedIn, requireLogin} = simpleOauth(oauthConfig, router);\n\n// Check if a user is logged in\nrouter.use(isLoggedIn);\nrouter.get('/admin', async (ctx) =\u003e {\n    if (ctx.state.isLoggedIn()) {\n        ctx.body = 'Logged in';\n    } else {\n        ctx.status = 403;\n        ctx.body = 'Not logged in';\n    }\n});\n\n// Check if the user is logged in using middleware\nrouter.get('/admin2', requireLogin, async (ctx) =\u003e {\n    ctx.body = 'Logged in';\n})\n\n// Add Koa Router middleware\napp.use(router.routes());\napp.use(router.allowedMethods());\n```\n\n### With Koa Mount\n```javascript\nimport mount from 'koa-mount';\n\n// Initialize Koa Simple OAuth\n// Returns an object with all required middleware\nconst oauthMiddleware = simpleOauth(oauthConfig);\nconst {login, authorized, whoami, logout} = oauthMiddleware;\n\n// Mount the OAuth middleware\napp.use(mount('/login', login));\napp.use(mount('/authorized', authorized));\napp.use(mount('/whoami', whoami));\napp.use(mount('/logout', logout));\n```\n\n### With Koa Route\n```javascript\nimport _ from 'koa-route';\n\n// Initialize Koa Simple OAuth\n// Passes all required middleware through the router function and returns the resulting middleware as an object\nconst oauthMiddleware = simpleOauth(oauthConfig, _);\n\n// Mount the OAuth middleware\nconst {login, authorized, whoami, logout} = oauthMiddleware;\napp.use(login);\napp.use(authorized);\napp.use(whoami);\napp.use(logout);\n\n// Or mount it less explicitly\nObject.values(oauthMiddleware).forEach((middleware) =\u003e {\n    app.use(middleware);\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaniellehuisman%2Fkoa-simple-oauth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdaniellehuisman%2Fkoa-simple-oauth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaniellehuisman%2Fkoa-simple-oauth/lists"}