mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-23 20:13:08 +02:00
Refactor
This commit is contained in:
parent
0c4de8f1a9
commit
c60b83f0dd
2 changed files with 23 additions and 18 deletions
|
@ -49,13 +49,6 @@ endpoints.forEach(endpoint =>
|
||||||
app.post('/signup', require('./private/signup').default);
|
app.post('/signup', require('./private/signup').default);
|
||||||
app.post('/signin', require('./private/signin').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/github')(app);
|
||||||
require('./service/twitter')(app);
|
require('./service/twitter')(app);
|
||||||
|
|
||||||
|
|
|
@ -12,10 +12,19 @@ import config from '../../conf';
|
||||||
import signin from '../common/signin';
|
import signin from '../common/signin';
|
||||||
|
|
||||||
module.exports = (app: express.Application) => {
|
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> => {
|
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({
|
const user = await User.findOneAndUpdate({
|
||||||
token: res.locals.user
|
token: userToken
|
||||||
}, {
|
}, {
|
||||||
$set: {
|
$set: {
|
||||||
twitter: null
|
twitter: null
|
||||||
|
@ -50,9 +59,10 @@ module.exports = (app: express.Application) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/connect/twitter', async (req, res): Promise<any> => {
|
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();
|
const ctx = await twAuth.begin();
|
||||||
redis.set(res.locals.user, JSON.stringify(ctx));
|
redis.set(userToken, JSON.stringify(ctx));
|
||||||
res.redirect(ctx.url);
|
res.redirect(ctx.url);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -77,7 +87,9 @@ module.exports = (app: express.Application) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/tw/cb', (req, res): any => {
|
app.get('/tw/cb', (req, res): any => {
|
||||||
if (res.locals.user == null) {
|
const userToken = getUserToken(req);
|
||||||
|
|
||||||
|
if (userToken == null) {
|
||||||
// req.headers['cookie'] は常に string ですが、型定義の都合上
|
// req.headers['cookie'] は常に string ですが、型定義の都合上
|
||||||
// string | string[] になっているので string を明示しています
|
// string | string[] になっているので string を明示しています
|
||||||
const cookies = cookie.parse((req.headers['cookie'] as string || ''));
|
const cookies = cookie.parse((req.headers['cookie'] as string || ''));
|
||||||
|
@ -102,11 +114,11 @@ module.exports = (app: express.Application) => {
|
||||||
signin(res, user, true);
|
signin(res, user, true);
|
||||||
});
|
});
|
||||||
} else {
|
} 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 result = await twAuth.done(JSON.parse(ctx), req.query.oauth_verifier);
|
||||||
|
|
||||||
const user = await User.findOneAndUpdate({
|
const user = await User.findOneAndUpdate({
|
||||||
token: res.locals.user
|
token: userToken
|
||||||
}, {
|
}, {
|
||||||
$set: {
|
$set: {
|
||||||
twitter: {
|
twitter: {
|
||||||
|
|
Loading…
Reference in a new issue