This commit is contained in:
syuilo 2017-12-10 17:50:02 +09:00
parent 0c4de8f1a9
commit c60b83f0dd
2 changed files with 23 additions and 18 deletions

View File

@ -49,13 +49,6 @@ endpoints.forEach(endpoint =>
app.post('/signup', require('./private/signup').default);
app.post('/signin', require('./private/signin').default);
app.use((req, res, next) => {
// req.headers['cookie'] は常に string ですが、型定義の都合上
// string | string[] になっているので string を明示しています
res.locals.user = ((req.headers['cookie'] as string || '').match(/i=(!\w+)/) || [null, null])[1];
next();
});
require('./service/github')(app);
require('./service/twitter')(app);

View File

@ -12,10 +12,19 @@ import config from '../../conf';
import signin from '../common/signin';
module.exports = (app: express.Application) => {
function getUserToken(req) {
// req.headers['cookie'] は常に string ですが、型定義の都合上
// string | string[] になっているので string を明示しています
return ((req.headers['cookie'] as string || '').match(/i=(!\w+)/) || [null, null])[1];
}
app.get('/disconnect/twitter', async (req, res): Promise<any> => {
if (res.locals.user == null) return res.send('plz signin');
const userToken = getUserToken(req);
if (userToken == null) return res.send('plz signin');
const user = await User.findOneAndUpdate({
token: res.locals.user
token: userToken
}, {
$set: {
twitter: null
@ -50,9 +59,10 @@ module.exports = (app: express.Application) => {
});
app.get('/connect/twitter', async (req, res): Promise<any> => {
if (res.locals.user == null) return res.send('plz signin');
const userToken = getUserToken(req);
if (userToken == null) return res.send('plz signin');
const ctx = await twAuth.begin();
redis.set(res.locals.user, JSON.stringify(ctx));
redis.set(userToken, JSON.stringify(ctx));
res.redirect(ctx.url);
});
@ -77,7 +87,9 @@ module.exports = (app: express.Application) => {
});
app.get('/tw/cb', (req, res): any => {
if (res.locals.user == null) {
const userToken = getUserToken(req);
if (userToken == null) {
// req.headers['cookie'] は常に string ですが、型定義の都合上
// string | string[] になっているので string を明示しています
const cookies = cookie.parse((req.headers['cookie'] as string || ''));
@ -102,11 +114,11 @@ module.exports = (app: express.Application) => {
signin(res, user, true);
});
} else {
redis.get(res.locals.user, async (_, ctx) => {
redis.get(userToken, async (_, ctx) => {
const result = await twAuth.done(JSON.parse(ctx), req.query.oauth_verifier);
const user = await User.findOneAndUpdate({
token: res.locals.user
token: userToken
}, {
$set: {
twitter: {