{"id":24564737,"url":"https://github.com/abzico/mpauthx","last_synced_at":"2025-03-17T01:44:21.113Z","repository":{"id":57303512,"uuid":"112272771","full_name":"abzico/mpauthx","owner":"abzico","description":"Token giver for Users logged in to WeChat Mini-program. Based on top of redis for fast token checking/access, and sqlite3 for flexible user db storage.","archived":false,"fork":false,"pushed_at":"2018-07-23T05:25:54.000Z","size":70,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-24T07:06:55.931Z","etag":null,"topics":["javascript","js","mini-program","miniprogram","mp","wechat","wechat-mini-program"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/abzico.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}},"created_at":"2017-11-28T02:04:36.000Z","updated_at":"2018-07-23T05:25:56.000Z","dependencies_parsed_at":"2022-09-18T18:19:53.013Z","dependency_job_id":null,"html_url":"https://github.com/abzico/mpauthx","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abzico%2Fmpauthx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abzico%2Fmpauthx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abzico%2Fmpauthx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abzico%2Fmpauthx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/abzico","download_url":"https://codeload.github.com/abzico/mpauthx/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243960444,"owners_count":20375101,"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":["javascript","js","mini-program","miniprogram","mp","wechat","wechat-mini-program"],"created_at":"2025-01-23T11:29:24.556Z","updated_at":"2025-03-17T01:44:21.093Z","avatar_url":"https://github.com/abzico.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mpauthx\n\nToken giver for Users logged in to WeChat Mini-program. Based on top of redis for fast token checking/access, and sqlite3 for flexible user db storage.\n\n# Features\n\n* Generate and assign an access token to each user for attached project (`sku`) supporting multiple devices\n* Provide functionality to initially and firstly register user via our user database (sqlite) via `authorize(code, encryptedData, iv)` function\n* Provide functionality to refresh access token only if user knows its own user id (either openId or unionId) via `refreshToken()` function\n* Each access token comes with configurabe TTL (time-to-live) thus when it's expired, such token will be deleted from redis db automatically.\n\n# How-To\n\nInstall `mpauthx` by executing the following\n\n```\nnpm install --save mpauthx\n```\n\nThen in code, you do this\n\n```javascript\nlet sqlite3DBInstance = ...; // create sqlite3 db instance here\n\nconst mpauthx = require('mpauthx')(\n\t'\u003cyour app-id here\u003e', // app-id\n  '\u003cyour app-secret here\u003e', // app-secret\n  '\u003cyour sku here\u003e', // sku\n\tsqlite3DBInstance, \t// your instance of sqlite3\n\tnull, // redis pass (if any), if none pass null\n\t259200 // TTL for token, in seconds\n);\n```\n\n\u003e See _Sqlite3 User Table Schema_ to have a proper sqlite3 table to work with this module.\n\nCall `mpauthx.authorize(code, encryptedData, iv)` whenever your end-point needs to authorize WeChat user and give user a token so user can save such token for subsequent API calls later in the future.\n\nCall `mpauthx.refreshToken(userId)` whenever you want to refresh token. If previously assigned token to such user exists, then it will be invalidated before generating and assigning new one. Client side should persist such token value and make use of it first to see if it is still not expired.\n\nas well\n\nCall `mpauthx.isTokenValid(token)` whenever you need to check whether such specified token is valid (thus exist in redis db) or not.\n\n## When You're Done\n\nCall `mpauthx.close()` to properly close redis client connection when you're done with your code.\n\n# Sqlite3 User Table Schema\n\nYour sqlite3 database needs to have `user` table with following schema\n\n```\nCREATE TABLE user(\n\topenId text primary key not null, \n\tcity text, \n\tcountry text, \n\tgender integer, \n\tlanguage text, \n\tnickName text, \n\tprovince text\n);\n```\n\n**Primarily** focus on `openId` as a requirement to have.\n\n## OpenId \u0026 UnionId\n\nAlso `openId` is umbrella word to represent either openid or unionid. If your app has been setup to work with unionid, then mpauthx will automatically use that instead of openid. This will allow you to peek database for users related to all apps across your company's WeChat Official Account.\n\n# API\n\n## Functions\n\n* `isTokenValid(token)` - check whether token is valid\n\n\tReturn `Promise` object.  \n\t`token` is `string` for specified token to check whether such token is valid or not. This means it's valid when it still exists and has exact match.\n\n* `authorize(code, encryptedData, iv)` - authorize WeChat user after logged in mini-program\n\n\tReturn `Promise` object. Success will contain success object in the following structure  \n\n\t```javascript\n\t{\n\t    status_code: \u003cnumber\u003e,\t// see core/constants.js or mpauthx.constants for all statuses\n\t\tstatus_message: \u003cstring\u003e,\n\t\tresponse: \u003cstring\u003e\t// returned generated token for such user\n\t}\n\t```\n\n\tOtherwise failure will contains `Error` object with `code` as additional property. See `core/constants.js` or `mpauthx.constants` for all status code.\n\n\t`code` can be acquired via [wx.login API](https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-login.html#wxloginobject).\n\n\t`encryptedData` and `iv` can be acquired via [wx.getUserInfo API](https://mp.weixin.qq.com/debug/wxadoc/dev/api/open.html#wxgetuserinfoobject).\n\n* `refreshToken(userId)` - request to refresh token for input user id (which represents either openid or unionid)\n\n    Return `Promise` object. Success will contain a new generated and assigned access token for such user.\n\n    ```javascript\n    {\n        status_code: \u003cnumber\u003e,\n        status_message: \u003csring\u003e,\n        response: \u003cstring\u003e // your new access token\n    }\n    ```\n\n    Otherwise failure will contains `Error` object.\n\n    `userId` as input should be known from client side as they should persist such value and always try to use such access token in API request before automatically detected by API if it needs to be re-generated.\n\n* `extractOpenId(token)` - extract openId part of specified token\n\n\tReturn openId part of specified token.\n    Note that openId is umbrella word to represent either openId or unionId. See OpenId \u0026 UnionId.\n\n* `extractSku(token)` - extract sku part of specified token\n\n  Return sku part of specified token.\n\n* `close()` - close redis connection\n\n## Properties\n\n* `constants` - expose constants mainly used for status code returned from API especially `authorize`.\n\n# Roadmap\n\n* [ ] Create a middleware for flexibly plug-in of non-redis DB checking for user record. Currently it's fixed to be used with redis.\n\n# License\n\n[Apache License 2.0](https://github.com/abzico/mpauthx/blob/master/LICENSE), [abzi.co](https://abzi.co)  \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabzico%2Fmpauthx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fabzico%2Fmpauthx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabzico%2Fmpauthx/lists"}