icydraw/src/server/index.ts

120 lines
2.7 KiB
TypeScript
Raw Normal View History

2022-04-02 13:45:07 +00:00
import express, { RequestHandler } from 'express';
import session from 'express-session';
import passport from 'passport';
import * as redis from 'redis';
import * as icynetstrat from 'passport-icynet';
import connectRedis from 'connect-redis';
import http from 'http';
import { join } from 'path';
import { Server, Socket } from 'socket.io';
2022-04-03 16:05:41 +00:00
import { IcyNetUser } from '../common/types/user';
2022-04-02 13:45:07 +00:00
import { Canvas } from './object/canvas';
import { CanvasRecord, Placement } from '../common/types/canvas';
import { config } from './config';
2022-04-03 16:05:41 +00:00
import { Game } from './object/game';
2022-04-02 13:45:07 +00:00
const RedisStore = connectRedis(session);
2022-04-02 14:23:36 +00:00
const redisClient = config.redis?.enabled ? redis.createClient() : undefined;
2022-04-02 13:45:07 +00:00
const sessionMiddleware = session({
secret: config.server.sessionSecret,
resave: false,
saveUninitialized: false,
cookie: { secure: process.env.NODE_ENV === 'production', sameSite: 'strict' },
2022-04-02 14:23:36 +00:00
store: config.redis?.enabled
? new RedisStore({ client: redisClient })
: undefined,
2022-04-02 13:45:07 +00:00
});
// todo: store less info in session
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((obj: IcyNetUser, done) => {
done(null, obj);
});
passport.use(
new icynetstrat.Strategy(
{
clientID: config.auth.clientID,
clientSecret: config.auth.clientSecret,
callbackURL: config.auth.callbackURL,
scope: [],
},
function (
accessToken: string,
refreshToken: string,
profile: any,
done: Function,
) {
process.nextTick(function () {
return done(null, profile);
});
},
),
);
const app = express();
if (process.env.NODE_ENV === 'production') {
app.enable('trust proxy');
}
const server = http.createServer(app);
const io = new Server(server);
const checkAuth: RequestHandler = (req, res, next) => {
if (req.isAuthenticated()) {
return next();
}
res.send('not logged in :(');
};
app.use(sessionMiddleware);
app.use(passport.initialize());
app.use(passport.session());
app.get(
'/login',
passport.authenticate('icynet', { scope: [] }),
(req, res) => {},
);
app.get(
'/callback',
passport.authenticate('icynet', { failureRedirect: '/?login=false' }),
(req, res) => {
res.redirect('/?login=true');
}, // auth success
);
app.get('/logout', (req, res) => {
req.logout();
res.redirect('/');
});
app.get('/info', checkAuth, (req, res) => {
res.json(req.user);
});
2022-04-02 14:46:41 +00:00
app.use('/canvas.png', (req, res) =>
res.sendFile(join(__dirname, '..', '..', 'canvas.png')),
);
2022-04-02 13:45:07 +00:00
app.use(express.static(join(__dirname, '..', 'public')));
///
2022-04-03 16:05:41 +00:00
const game = new Game(io, sessionMiddleware);
2022-04-02 13:45:07 +00:00
2022-04-03 16:05:41 +00:00
game.initialize().then(() =>
2022-04-03 12:49:45 +00:00
server.listen(config.server.port, config.server.bind, () => {
2022-04-02 13:45:07 +00:00
console.log(`Listening at http://localhost:${config.server.port}/`);
}),
);