import * as mongoSessionStore from 'connect-mongo' ; import * as cors from 'cors' ; import * as express from 'express' ; import * as session from 'express-session' ; import * as mongoose from 'mongoose' ; import api from './api' ; import { setupGoogle } from './google-auth' ; import { setupPasswordless } from './passwordless-auth' ; require ( 'dotenv' ) . config ( ) ; const options = { useNewUrlParser : true , useCreateIndex : true , useFindAndModify : false , useUnifiedTopology : true } ; mongoose . connect ( process . env . MONGO_URL_TEST , options ) ; const server = express ( ) ; server . use ( cors ( { origin : process . env . URL_APP , methods : 'GET,HEAD,PUT,PATCH,POST,DELETE' , credentials : true } ) ) ; server . use ( express . json ( ) ) ; const MongoStore = mongoSessionStore ( session ) ; const sessionOptions = { name : process . env . SESSION_NAME , secret : process . env . SESSION_SECRET , resave : false , saveUninitialized : false , store : new MongoStore ( { mongooseConnection : mongoose . connection , ttl : 14 * 24 * 60 * 60 , autoRemove : 'interval' , autoRemoveInterval : 1440 } ) , cookie : { httpOnly : true , maxAge : 14 * 24 * 60 * 60 * 1000 , secure : false } } ; const sessionMiddleware = session ( sessionOptions ) ; server . use ( sessionMiddleware ) ; setupGoogle ( { server } ) ; setupPasswordless ( { server } ) ; api ( server ) ; server . get ( '*' , ( _ , res ) => { res . sendStatus ( 403 ) ; } ) ; server . listen ( process . env . PORT_API , ( ) => { console . log ( `> Ready on ${ process . env . URL_API }` ) ; } ) ;