diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2018-04-13 06:06:18 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2018-04-13 06:06:18 +0900 |
| commit | 3368fe855249f45bdf1e4c1e509d325d44e80fbe (patch) | |
| tree | 63c8bc61fb645b1d730b05120ab5117c0fdeee29 /src/server/api/private | |
| parent | wip (diff) | |
| download | sharkey-3368fe855249f45bdf1e4c1e509d325d44e80fbe.tar.gz sharkey-3368fe855249f45bdf1e4c1e509d325d44e80fbe.tar.bz2 sharkey-3368fe855249f45bdf1e4c1e509d325d44e80fbe.zip | |
wip
Diffstat (limited to 'src/server/api/private')
| -rw-r--r-- | src/server/api/private/signin.ts | 36 | ||||
| -rw-r--r-- | src/server/api/private/signup.ts | 20 |
2 files changed, 28 insertions, 28 deletions
diff --git a/src/server/api/private/signin.ts b/src/server/api/private/signin.ts index 665ee21ebd..55326deeaf 100644 --- a/src/server/api/private/signin.ts +++ b/src/server/api/private/signin.ts @@ -1,4 +1,4 @@ -import * as express from 'express'; +import * as Koa from 'koa'; import * as bcrypt from 'bcryptjs'; import * as speakeasy from 'speakeasy'; import User, { ILocalUser } from '../../../models/user'; @@ -7,26 +7,26 @@ import event from '../../../publishers/stream'; import signin from '../common/signin'; import config from '../../../config'; -export default async (req: express.Request, res: express.Response) => { - res.header('Access-Control-Allow-Origin', config.url); - res.header('Access-Control-Allow-Credentials', 'true'); +export default async (ctx: Koa.Context) => { + ctx.set('Access-Control-Allow-Origin', config.url); + ctx.set('Access-Control-Allow-Credentials', 'true'); - const username = req.body['username']; - const password = req.body['password']; - const token = req.body['token']; + const username = ctx.body['username']; + const password = ctx.body['password']; + const token = ctx.body['token']; if (typeof username != 'string') { - res.sendStatus(400); + ctx.status = 400; return; } if (typeof password != 'string') { - res.sendStatus(400); + ctx.status = 400; return; } if (token != null && typeof token != 'string') { - res.sendStatus(400); + ctx.status = 400; return; } @@ -37,12 +37,12 @@ export default async (req: express.Request, res: express.Response) => { }, { fields: { data: false, - 'profile': false + profile: false } }) as ILocalUser; if (user === null) { - res.status(404).send({ + ctx.throw(404, { error: 'user not found' }); return; @@ -60,17 +60,17 @@ export default async (req: express.Request, res: express.Response) => { }); if (verified) { - signin(res, user, false); + signin(ctx, user, false); } else { - res.status(400).send({ + ctx.throw(400, { error: 'invalid token' }); } } else { - signin(res, user, false); + signin(ctx, user, false); } } else { - res.status(400).send({ + ctx.throw(400, { error: 'incorrect password' }); } @@ -79,8 +79,8 @@ export default async (req: express.Request, res: express.Response) => { const record = await Signin.insert({ createdAt: new Date(), userId: user._id, - ip: req.ip, - headers: req.headers, + ip: ctx.ip, + headers: ctx.headers, success: same }); diff --git a/src/server/api/private/signup.ts b/src/server/api/private/signup.ts index f441e1b754..a4554be4ae 100644 --- a/src/server/api/private/signup.ts +++ b/src/server/api/private/signup.ts @@ -1,5 +1,5 @@ import * as uuid from 'uuid'; -import * as express from 'express'; +import * as Koa from 'koa'; import * as bcrypt from 'bcryptjs'; import { generate as generateKeypair } from '../../../crypto_key'; import recaptcha = require('recaptcha-promise'); @@ -33,30 +33,30 @@ const home = { ] }; -export default async (req: express.Request, res: express.Response) => { +export default async (ctx: Koa.Context) => { // Verify recaptcha // ただしテスト時はこの機構は障害となるため無効にする if (process.env.NODE_ENV !== 'test') { - const success = await recaptcha(req.body['g-recaptcha-response']); + const success = await recaptcha(ctx.body['g-recaptcha-response']); if (!success) { - res.status(400).send('recaptcha-failed'); + ctx.throw(400, 'recaptcha-failed'); return; } } - const username = req.body['username']; - const password = req.body['password']; + const username = ctx.body['username']; + const password = ctx.body['password']; // Validate username if (!validateUsername(username)) { - res.sendStatus(400); + ctx.status = 400; return; } // Validate password if (!validatePassword(password)) { - res.sendStatus(400); + ctx.status = 400; return; } @@ -71,7 +71,7 @@ export default async (req: express.Request, res: express.Response) => { // Check username already used if (usernameExist !== 0) { - res.sendStatus(400); + ctx.status = 400; return; } @@ -143,5 +143,5 @@ export default async (req: express.Request, res: express.Response) => { }); // Response - res.send(await pack(account)); + ctx.body = await pack(account); }; |