<commit_before>import express from 'express';
import graphQLHTTP from 'express-graphql';
import {Schema} from './data/schema';
import writeSchema from './data/writeSchema';
import path from 'path';
import fallback from 'express-history-api-fallback';
import bodyParser from 'body-parser';
import session from 'express-session';
import BSD from './bsd';
import MG from './mail';
import demoData from './data/demo.json';
import models from './data/models'
import {fromGlobalId} from 'graphql-relay'
import passport from 'passport';
import LocalStrategy  from 'passport-local'
import SequelizeStoreFactory from 'connect-session-sequelize'

writeSchema();

const SequelizeStore = SequelizeStoreFactory(session.Store)
const Mailgun = new MG(process.env.MAILGUN_KEY, process.env.MAILGUN_DOMAIN);
const BSDClient = new BSD(process.env.BSD_HOST, process.env.BSD_API_ID, process.env.BSD_API_SECRET);
const port = process.env.APP_PORT;
const publicPath = path.resolve(__dirname, '../frontend/public');

passport.use('signup', new LocalStrategy(
  {
    usernameField: 'email',
    passwordField: 'password',
    passReqToCallback: true
  },
  async (req, email, password, done) => {
    let user = await models.User.findOne({
      where: {
        email: email
      }
    });

    if (!user) {
      let newUser = await models.User.create({
        email: email,
        password: password,
      });
      return done(null, newUser);
    }
    else if (!await user.verifyPassword(password)) {
      return done(null, false, { message: 'Incorrect password.' });
    }
    return done(null, user);
  }
));

passport.serializeUser(async (user, done) => {
  done(null, user.id);
});

passport.deserializeUser(async (id, done) => {
  let user = await models.User.findById(id);
  done(null, user);
});

const app = express();
const sessionStore = new SequelizeStore({
  db: models.sequelize,
})

app.use(express.static(publicPath))
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(session({
  secret: 'keyboard cat',
  store: sessionStore,
  domain: null
}));
app.use(passport.initialize());
app.use(passport.session());
app.use('/graphql', graphQLHTTP((request) => {
  return {
    rootValue: { session: request.user },
    schema: Schema
  }
}));

// this endpoint may be used for caching and serving available event types and their attributes to the event creation form
app.get('/events/types.json', async (req, res) => {
  let result = await BSDClient.getEventTypes();
  res.json(result);
});

app.get('/test', (req, res) => {
  throw new Error("Oh no");
  console.log(req)
  console.log(req.passport)
  console.log(req.isAuthenticated())
  console.log('user')
  console.log(req.user)
  res.send('done')
})

app.post('/signup',
  passport.authenticate('signup'),
  (req, res) => {
  console.log(req.isAuthenticated());
  console.log('here?')
  console.log(req.user)
  res.send('Success!')
})

app.post('/login', async (req, res) => {
  // Implement
/*  let person = fromGlobalId(req.body.id);
  req.session.regenerate(() => {
    req.session.personId = person.id<commit_msg>Remove test endpoint<commit_after>import express from 'express';
import graphQLHTTP from 'express-graphql';
import {Schema} from './data/schema';
import writeSchema from './data/writeSchema';
import path from 'path';
import fallback from 'express-history-api-fallback';
import bodyParser from 'body-parser';
import session from 'express-session';
import BSD from './bsd';
import MG from './mail';
import demoData from './data/demo.json';
import models from './data/models'
import {fromGlobalId} from 'graphql-relay'
import passport from 'passport';
import LocalStrategy  from 'passport-local'
import SequelizeStoreFactory from 'connect-session-sequelize'

writeSchema();

const SequelizeStore = SequelizeStoreFactory(session.Store)
const Mailgun = new MG(process.env.MAILGUN_KEY, process.env.MAILGUN_DOMAIN);
const BSDClient = new BSD(process.env.BSD_HOST, process.env.BSD_API_ID, process.env.BSD_API_SECRET);
const port = process.env.APP_PORT;
const publicPath = path.resolve(__dirname, '../frontend/public');

passport.use('signup', new LocalStrategy(
  {
    usernameField: 'email',
    passwordField: 'password',
    passReqToCallback: true
  },
  async (req, email, password, done) => {
    let user = await models.User.findOne({
      where: {
        email: email
      }
    });

    if (!user) {
      let newUser = await models.User.create({
        email: email,
        password: password,
      });
      return done(null, newUser);
    }
    else if (!await user.verifyPassword(password)) {
      return done(null, false, { message: 'Incorrect password.' });
    }
    return done(null, user);
  }
));

passport.serializeUser(async (user, done) => {
  done(null, user.id);
});

passport.deserializeUser(async (id, done) => {
  let user = await models.User.findById(id);
  done(null, user);
});

const app = express();
const sessionStore = new SequelizeStore({
  db: models.sequelize,
})

app.use(express.static(publicPath))
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(session({
  secret: 'keyboard cat',
  store: sessionStore,
  domain: null
}));
app.use(passport.initialize());
app.use(passport.session());
app.use('/graphql', graphQLHTTP((request) => {
  return {
    rootValue: { session: request.user },
    schema: Schema
  }
}));

// this endpoint may be used for caching and serving available event types and their attributes to the event creation form
app.get('/events/types.json', async (req, res) => {
  let result = await BSDClient.getEventTypes();
  res.json(result);
});

app.post('/signup',
  passport.authenticate('signup'),
  (req, res) => {
  console.log(req.isAuthenticated());
  console.log('here?')
  console.log(req.user)
  res.send('Success!')
})

app.post('/login', async (req, res) => {
  // Implement
/*  let person = fromGlobalId(req.body.id);
  req.session.regenerate(() => {
    req.session.personId = person.id
{'fraction_non_alphanumeric': 0.15397433761039828, 'fraction_numerical': 0.0, 'mean_word_length': 4.187554019014693, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '474397', 'n_tokens_mistral': 1921, 'n_tokens_neox': 1910, 'n_words': 468}