{"id":16883177,"url":"https://github.com/wilsonwu/express-oauth2-demo","last_synced_at":"2025-08-01T15:36:47.962Z","repository":{"id":151601350,"uuid":"118559193","full_name":"wilsonwu/express-oauth2-demo","owner":"wilsonwu","description":"Full OAuth2 demo with express 4 framework (http://expressjs.com/) and oauth2-server (https://github.com/oauthjs/node-oauth2-server) for nodejs.","archived":false,"fork":false,"pushed_at":"2023-02-06T03:39:24.000Z","size":25,"stargazers_count":10,"open_issues_count":1,"forks_count":7,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-25T08:11:14.558Z","etag":null,"topics":["express","jwt","nodejs","oauth","oauth2-demo","oauth2-server"],"latest_commit_sha":null,"homepage":"","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/wilsonwu.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}},"created_at":"2018-01-23T04:48:06.000Z","updated_at":"2024-12-10T14:39:52.000Z","dependencies_parsed_at":"2023-04-07T19:54:39.037Z","dependency_job_id":null,"html_url":"https://github.com/wilsonwu/express-oauth2-demo","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wilsonwu%2Fexpress-oauth2-demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wilsonwu%2Fexpress-oauth2-demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wilsonwu%2Fexpress-oauth2-demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wilsonwu%2Fexpress-oauth2-demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wilsonwu","download_url":"https://codeload.github.com/wilsonwu/express-oauth2-demo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248390432,"owners_count":21095793,"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":["express","jwt","nodejs","oauth","oauth2-demo","oauth2-server"],"created_at":"2024-10-13T16:10:40.843Z","updated_at":"2025-04-11T11:51:37.598Z","avatar_url":"https://github.com/wilsonwu.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# express-oauth2-demo\nFull OAuth2 demo with express 4 framework (http://expressjs.com/) and oauth2-server (https://github.com/oauthjs/node-oauth2-server) for nodejs.\n\n# Background\nI want to create a simple API service for my website, but some data update APIs need auth, I looked for oauth2 lite solution, but all of demos are not good. So I create one base on oauth2-server 2.4 (https://www.npmjs.com/package/node-oauth2-server) (seems 3.0 has lots of problems, at least for me).\n\n# Steps\n## 1. Install all packages\nAfter clone the code, run:\n```\nnpm install\n```\n## 2. Check models\nI create 4 models for this demo, MySql, Microsoft SQL Server, Data in Memory and JWT, by default it will use memory model, and except JWT model other 3 models' token are ramdomly.\n\n### Memory Model\n**File path:** ```modules/memoryModel.js```  \n**Description:** By default this demo use this model, so you don't need to change any code, you can run the demo (in follow steps), for this model, all data store in memory, if you restart the site, all things will disappare.\n### JWT Model\n**File path:** ```modules/jwtModel.js```  \n**Description:** This model could help you integrate Json Web Token (JWT, for more JWT related information please visit: https://jwt.io/), because the client and user should be integrated with a type of database, so in this demo it use MySql, so please run the ```modules/mysql.sql``` script to create related database, tables and data, and go to ```modules/sqlPool.js``` to change your database connection information in ```sqlPool```, after that change one line code in file: ```modules/oauth.js```, replace ```memoryModel``` to ```jwtModel```, and go to ```app.js``` to change JWT secret: ```global.jwtSecret```, and JWT expired time: ```global.jwtExpiresIn```, after that you can run it.\n### MS SQL Server Model\n**File path:** ```modules/mssqlModel.js```  \n**Description:** Because I use Microsoft Azure, so I use SQL Server Database on Azure, the data table creation sql file at: ```modules/mssql.sql```, just 3 tables need, after this, you can change one line code in file: ```modules/oauth.js```, replace ```memoryModel``` to ```mssqlModel```, and go to ```app.js``` to change your database connection information in ```global.conn```, after that, creaete data in client table and user table, after that all things work as the memory model, you can run it.\n### MySQL Model\n**File path:** ```modules/mysqlModel.js```  \n**Description:** I use a self-deploy MySQL Database, the data table creation sql file at: ```modules/mysql.sql```, database, 3 tables and two data created, after this, you can change one line code in file: ```modules/oauth.js```, replace ```memoryModel``` to ```mysqlModel```, and go to ```modules/sqlPool.js``` to change your database connection information in ```sqlPool```, after that you can run it.\n\n## 3. Start\nAfter you follow the step 2 to change model that you want, you can run:\n```\nnpm start\n```\nVisit: http://127.0.0.1:3000, you can see the default home page of express 4.\n## 4. Check information\nThe first step of oauth2 is make sure you have client and user, if you are using memory model you can go to the file: ```modules/memoryModel.js``` to check the client and user information, by default:\n```\nclientId: wilsonwu\nclientSecret: lookingforjob\nusername: iwilsonwu\npassword: architect\n```\nIf you are using mssql model, you can create your client and user information in you database tables.\n## 5. Get Token\nAfter your client and user information ready, use Postman to send below request:\n```\nPOST /auth/token HTTP/1.1\nHost: 127.0.0.1:3000\nContent-Type: application/x-www-form-urlencoded\nCache-Control: no-cache\n\ngrant_type=password\u0026client_id=wilsonwu\u0026client_secret=lookingforjob\u0026username=iwilsonwu\u0026password=architect\n```\nResponse:\nBy default the ramdom token:\n```js\n{\n    \"token_type\": \"bearer\",\n    \"access_token\": \"16848b43898dba304f33d78a6f9671ddf96d9c04\",\n    \"expires_in\": 1209600,\n    \"refresh_token\": \"ac3b257a3459ac0a92f905d20a61593fdccdb151\"\n}\n```\nOr for JWT:\n```js\n{\n    \"token_type\": \"bearer\",\n    \"access_token\": \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjbGllbnRJZCI6Im5hbWVhcGkiLCJ1c2VySWQiOjEsImlhdCI6MTU0OTk4ODM0NywiZXhwIjoxNTUxMjg0MzQ3fQ.aBLwpH0SEwk3HaVuWb_bDxx9nvpknpghH5jHyrTNkVA\",\n    \"expires_in\": 1209600,\n    \"refresh_token\": \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjbGllbnRJZCI6Im5hbWVhcGkiLCJ1c2VySWQiOjEsImlhdCI6MTU0OTk4ODM0NywiZXhwIjoxNTUxMjg0MzQ3fQ.aBLwpH0SEwk3HaVuWb_bDxx9nvpknpghH5jHyrTNkVA\"\n}\n```\nNow you get the token, then run:\n```\nGET /users/profile/ HTTP/1.1\nHost: 127.0.0.1:3000\nAuthorization: Bearer 16848b43898dba304f33d78a6f9671ddf96d9c04\nCache-Control: no-cache\n```\nResponse:\n```\nWilson Wu is a Software Architect!\n```\nIf you use wrong token:\n```\nGET /users/profile/ HTTP/1.1\nHost: 127.0.0.1:3000\nAuthorization: Bearer xxxxxx\nCache-Control: no-cache\n```\nResponse:\n```js\n{\n    \"code\": 401,\n    \"error\": \"invalid_token\",\n    \"error_description\": \"The access token provided is invalid.\"\n}\n```\n# All things done! Now you can follow the ```/auth/token``` and ```/users/profile``` code to extend your site to support oauth2\n# Enjoy your Express 4 + OAuth2 demo!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwilsonwu%2Fexpress-oauth2-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwilsonwu%2Fexpress-oauth2-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwilsonwu%2Fexpress-oauth2-demo/lists"}