add user api
This commit is contained in:
		
							parent
							
								
									ffb99a61d7
								
							
						
					
					
						commit
						f785e05d4b
					
				| @ -21,6 +21,7 @@ const Router = require('koa-router'); | ||||
| const koaBody = require('koa-body'); | ||||
| const koaStatic = require('koa-static'); | ||||
| const cors = require('kcors'); | ||||
| const logger = require('koa-morgan'); | ||||
| 
 | ||||
| // Koa Main Application
 | ||||
| const app = new koa(); | ||||
| @ -32,16 +33,19 @@ const server = app.listen(3000, () => { | ||||
| // Root Router
 | ||||
| const router = new Router(); | ||||
| 
 | ||||
| // set app keys 
 | ||||
| app.keys = ['44b4fa5cb8a394294361']; | ||||
| 
 | ||||
| // enable logger
 | ||||
| app.use(logger('combined')); | ||||
| // enable body parser
 | ||||
| app.use(koaBody({ | ||||
|     multipart: true, | ||||
|     // upload file size 10mb
 | ||||
|     maxFieldSize: 10 * 1024 * 1024 | ||||
| })); | ||||
| // app.use(koaBody({
 | ||||
| //     multipart: true,
 | ||||
| //     // upload file size 10mb
 | ||||
| //     maxFieldSize: 10 * 1024 * 1024
 | ||||
| // }));
 | ||||
| // enable cors
 | ||||
| app.use(cors()); | ||||
| // enable static file 
 | ||||
| app.use(koaStatic(path.resolve(__dirname, 'public'))); | ||||
| // enable session
 | ||||
| app.use(session({ | ||||
|     key: 'koa:sess', | ||||
| @ -50,6 +54,9 @@ app.use(session({ | ||||
|     httpOnly: true, | ||||
|     signed: true | ||||
| }, app)); | ||||
| // enable static file 
 | ||||
| app.use(koaStatic(path.resolve(__dirname, 'public'))); | ||||
| // enable router
 | ||||
| app.use(router.routes()); | ||||
| app.use(router.allowedMethods()); | ||||
| 
 | ||||
|  | ||||
| @ -1,20 +1,37 @@ | ||||
| var crypto = require('crypto'); | ||||
| 
 | ||||
| /** | ||||
|  *  | ||||
|  * @param {number} len  | ||||
|  */ | ||||
| var random = (len = 32) => { | ||||
|     var buf = crypto.randomBytes(len); | ||||
|     return buf.toString("hex"); | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  *  | ||||
|  * @param {string} str  | ||||
|  */ | ||||
| var sha256 = (str) => { | ||||
|     return crypto.createHash("sha256").update(str).digest('base64'); | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  *  | ||||
|  * @param {string} str  | ||||
|  */ | ||||
| var genPassHash = (str) => { | ||||
|     var hash = random(16); | ||||
|     var pass = sha256(str + hash); | ||||
|     return `$${hash}$${pass}`; | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  *  | ||||
|  * @param {string} plain  | ||||
|  * @param {string} hash  | ||||
|  */ | ||||
| var comparePass = (plain, hash) => { | ||||
|     var match = hash.match(/^\$(.+?)\$(.+)$/); | ||||
|     if (match == null || match.length < 3 || !match[1] || !match[2]) return false; | ||||
|  | ||||
| @ -8,6 +8,7 @@ | ||||
|     "kcors": "2", | ||||
|     "koa": "^2.2.0", | ||||
|     "koa-body": "^2.0.1", | ||||
|     "koa-morgan": "^1.0.1", | ||||
|     "koa-router": "^7.2.0", | ||||
|     "koa-session": "^5.0.0", | ||||
|     "koa-static": "^3.0.0", | ||||
|  | ||||
| @ -1,8 +1,61 @@ | ||||
| const Router = require('koa-router'); | ||||
| const router = new Router(); | ||||
| const koaBody = require('koa-body'); | ||||
| 
 | ||||
| router.get('/', (c, n) => { | ||||
|     c.body = "API Root"; | ||||
| // import tools 
 | ||||
| const mysql = require('../../libs/mysql_pool.js'); | ||||
| const config = require('../../config.json'); | ||||
| 
 | ||||
| // routes 
 | ||||
| const user_api = require('./user.js'); | ||||
| 
 | ||||
| // api response handler
 | ||||
| router.use(async(c, n) => { | ||||
|     c.db = await mysql.getConn(); | ||||
|     c.syncQuery = (query, param = null) => { | ||||
|         return new Promise((resolve, reject) => { | ||||
|             if (param != null) { | ||||
|                 c.db.query(query, param, (err, row) => { | ||||
|                     if (err) return reject(err); | ||||
|                     return resolve(row) | ||||
|                 }); | ||||
|             } else { | ||||
|                 c.db.query(query, (err, row) => { | ||||
|                     if (err) return reject(err); | ||||
|                     return resolve(row) | ||||
|                 }); | ||||
|             } | ||||
|         }) | ||||
|     } | ||||
|     await n(); | ||||
|     if ('db' in c && typeof c.db == 'object' && 'release' in c.db && typeof c.db.release == 'function') c.db.release(); | ||||
|     switch (typeof c.body) { | ||||
|         case 'undefined': | ||||
|             c.body = { errorCode: 'ERR9999', status: 0 }; | ||||
|         case 'string': | ||||
|             c.body = { errorCode: c.body, status: 0 }; | ||||
|         default: | ||||
|             c.body = { | ||||
|                 data: c.body, | ||||
|                 status: 1 | ||||
|             } | ||||
|     } | ||||
| }) | ||||
| 
 | ||||
| 
 | ||||
| // enable bodyParser
 | ||||
| router.all('*', koaBody({ | ||||
|     multipart: true, | ||||
|     // upload file size 10mb
 | ||||
|     maxFieldSize: 10 * 1024 * 1024 | ||||
| }), async(c, n) => { await n(); }) | ||||
| 
 | ||||
| router | ||||
|     .get('/', async(c, n) => { | ||||
|         c.body = { | ||||
|             msg: 'API Endpoint' | ||||
|         }; | ||||
|     }) | ||||
|     .use('/user', user_api.routes()) | ||||
| 
 | ||||
| module.exports = router; | ||||
							
								
								
									
										30
									
								
								server-api/route/api/user.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								server-api/route/api/user.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,30 @@ | ||||
| const Router = require('koa-router'); | ||||
| const router = new Router(); | ||||
| 
 | ||||
| // import tools 
 | ||||
| const crypto = require('../../libs/crypto.js'); | ||||
| 
 | ||||
| router | ||||
|     .post('/login', async(c, n) => { | ||||
|         let arr = c.request.body; | ||||
|         if (!arr.data) return c.body = 'ERR0000'; | ||||
|         if (!arr.data.account) return c.body = 'ERR0001'; | ||||
|         if (!arr.data.password) return c.body = 'ERR0002'; | ||||
|         try { | ||||
|             let user = await c.syncQuery('select `uid`,`account`,`password`,`name`,`email` from ??.?? where `account` = ?', ['lora', 'user', arr.data.account]) | ||||
| 
 | ||||
|             if (user.length == 0) return c.body = 'user not exists'; | ||||
| 
 | ||||
|             if (!crypto.comparePass(arr.data.password, user[0].password)) return c.body = 'password not match'; | ||||
| 
 | ||||
|             delete user[0].password; | ||||
| 
 | ||||
|             c.body = { | ||||
|                 record: user | ||||
|             } | ||||
|         } catch (err) { | ||||
|             return c.body = 'DB Query Error'; | ||||
|         } | ||||
|     }) | ||||
| 
 | ||||
| module.exports = router; | ||||
| @ -13,6 +13,10 @@ any-promise@^1.0.0, any-promise@^1.1.0: | ||||
|   version "1.3.0" | ||||
|   resolved "https://registry.yarnpkg.com/any-promise/-/any-promise-1.3.0.tgz#abc6afeedcea52e809cdc0376aed3ce39635d17f" | ||||
| 
 | ||||
| basic-auth@~1.1.0: | ||||
|   version "1.1.0" | ||||
|   resolved "https://registry.yarnpkg.com/basic-auth/-/basic-auth-1.1.0.tgz#45221ee429f7ee1e5035be3f51533f1cdfd29884" | ||||
| 
 | ||||
| bignumber.js@3.1.2: | ||||
|   version "3.1.2" | ||||
|   resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-3.1.2.tgz#f3bdb99ad5268a15fc1f0bed2fb018e2693fe236" | ||||
| @ -57,7 +61,7 @@ crc@^3.4.4: | ||||
|   version "3.4.4" | ||||
|   resolved "https://registry.yarnpkg.com/crc/-/crc-3.4.4.tgz#9da1e980e3bd44fc5c93bf5ab3da3378d85e466b" | ||||
| 
 | ||||
| debug@*, debug@^2.2.0, debug@^2.6.0: | ||||
| debug@*, debug@2.6.8, debug@^2.2.0, debug@^2.6.0: | ||||
|   version "2.6.8" | ||||
|   resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.8.tgz#e731531ca2ede27d188222427da17821d68ff4fc" | ||||
|   dependencies: | ||||
| @ -175,6 +179,12 @@ koa-is-json@^1.0.0: | ||||
|   version "1.0.0" | ||||
|   resolved "https://registry.yarnpkg.com/koa-is-json/-/koa-is-json-1.0.0.tgz#273c07edcdcb8df6a2c1ab7d59ee76491451ec14" | ||||
| 
 | ||||
| koa-morgan@^1.0.1: | ||||
|   version "1.0.1" | ||||
|   resolved "https://registry.yarnpkg.com/koa-morgan/-/koa-morgan-1.0.1.tgz#08052e0ce0d839d3c43178b90a5bb3424bef1f99" | ||||
|   dependencies: | ||||
|     morgan "^1.6.1" | ||||
| 
 | ||||
| koa-router@^7.2.0: | ||||
|   version "7.2.0" | ||||
|   resolved "https://registry.yarnpkg.com/koa-router/-/koa-router-7.2.0.tgz#cb35ba94fee2614d39736b7fa5381681b7aaf78c" | ||||
| @ -256,6 +266,16 @@ mime-types@^2.0.7, mime-types@~2.1.11, mime-types@~2.1.15: | ||||
|   dependencies: | ||||
|     mime-db "~1.27.0" | ||||
| 
 | ||||
| morgan@^1.6.1: | ||||
|   version "1.8.2" | ||||
|   resolved "https://registry.yarnpkg.com/morgan/-/morgan-1.8.2.tgz#784ac7734e4a453a9c6e6e8680a9329275c8b687" | ||||
|   dependencies: | ||||
|     basic-auth "~1.1.0" | ||||
|     debug "2.6.8" | ||||
|     depd "~1.1.0" | ||||
|     on-finished "~2.3.0" | ||||
|     on-headers "~1.0.1" | ||||
| 
 | ||||
| ms@2.0.0: | ||||
|   version "2.0.0" | ||||
|   resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8" | ||||
| @ -284,12 +304,16 @@ object-assign@^4.0.1: | ||||
|   version "4.1.1" | ||||
|   resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863" | ||||
| 
 | ||||
| on-finished@^2.1.0: | ||||
| on-finished@^2.1.0, on-finished@~2.3.0: | ||||
|   version "2.3.0" | ||||
|   resolved "https://registry.yarnpkg.com/on-finished/-/on-finished-2.3.0.tgz#20f1336481b083cd75337992a16971aa2d906947" | ||||
|   dependencies: | ||||
|     ee-first "1.1.1" | ||||
| 
 | ||||
| on-headers@~1.0.1: | ||||
|   version "1.0.1" | ||||
|   resolved "https://registry.yarnpkg.com/on-headers/-/on-headers-1.0.1.tgz#928f5d0f470d49342651ea6794b0857c100693f7" | ||||
| 
 | ||||
| only@0.0.2: | ||||
|   version "0.0.2" | ||||
|   resolved "https://registry.yarnpkg.com/only/-/only-0.0.2.tgz#2afde84d03e50b9a8edc444e30610a70295edfb4" | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user