Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
app.use(express.static('dist/client'));
app.use(cookieParser(secretString));
app.use(session({
secret: secretString,
resave: true,
secure: false,
saveUninitialized: true
}));
// setup passport
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(function(user, done) { done(null, user) });
passport.deserializeUser(function(user, done) { done(null, user) });
// connect authentication and api routes
app.use(passportRoutes);
app.use(apiRoutes);
app.use(fallback(path.join(__dirname, '../../dist/client/index.html')));
app.listen(PORT, (err) => {
if (err) throw err;
console.log(`The Express Server is Listening at port ${PORT} in ${NODE_ENV} mode`);
});
export default app;
function start() {
// Passport setup.
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((obj, done) => {
done(null, obj);
});
passport.use(new Strategy({
clientID: config.clientid,
clientSecret: config.secret,
callbackURL: "http://" + config.address + "/auth/discord/callback",
scope: ["identify", "guilds"]
}, (accessToken, refreshToken, profile, done) => {
process.nextTick(() => {
return done(null, profile);
app.use(expressSession(config.session))
app.use(compression())
app.use(cors({origin: ['*.shud.in', 'localhost:*', '127.0.0.1:*']}))
app.use(serveFavicon(path.join(__dirname, '..', 'public', 'static', 'images', 'favicon.ico')))
app.use(passport.initialize())
app.use(passport.session())
// GraphQL
app.use('/api', expressGraphQL({
schema,
// turn on graphiql for debugging if under dev mode
graphiql: global.__is_dev
}))
// authenticate
passport.serializeUser((user, done) => done(null, user))
passport.deserializeUser((user, done) => done(null, user))
passport.use(new TwitterStrategy({
consumerKey: config.TWITTER_CONSUMER_KEY,
consumerSecret: config.TWITTER_CONSUMER_SECRET,
callbackURL: config.domain + "/auth/twitter/callback"
},
(token, tokenSecret, profile, done) => {
User.findOrCreate({
id: profile.id,
name: profile.displayName,
login: profile.username,
avatar: profile.photos.length ? profile.photos[0].value : ''
}, (err, user) => {
if (err) { return done(err) }
done(null, user)
})
//middleware
//parse application/json
app.use(bodyParser.json({
limit: config.bodyLimit
}));
//passport config
app.use(passport.initialize());
let Account = require('./model/account');
passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'password'
},
Account.authenticate()
));
passport.serializeUser(Account.serializeUser());
passport.deserializeUser(Account.deserializeUser());
//api routes v1
app.use('/v1', routes);
// Base URL test endpoint to see if API is running
app.get('/', (req, res) => {
res.json({ message: 'Chat API is ALIVE!' })
});
/*||||||||||||||||SOCKET|||||||||||||||||||||||*/
//Listen for connection
const channelController = require('./controller/channel');
const messageController = require('./controller/message');
io.on('connection', function (client) {
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
// Favicon.
app.use(favicon(__dirname + '/public/src/img/favicon.ico'));
// Production provisions.
if (process.env.NODE_ENV === 'production') {
app.use(errorhandler({ dumpExceptions: true, showStack: true }));
app.use(protectJSON);
app.use(raven.middleware.express(process.env.SENTRY_URL));
}
// Authentication.
passport.deserializeUser(users.auth.local().deserialize);
passport.serializeUser(users.auth.local().serialize);
passport.use(users.auth.local().strategy);
app.use(passport.initialize());
app.use(passport.session());
app.get('/logout/', users.routes.logoutLocal);
app.post('/login/', users.routes.loginLocal(passport));
// Users API.
app.post('/api/users', users.routes.create());
app.get('/api/users', users.routes.list());
app.get('/api/users/:id', users.routes.get());
app.put('/api/users/:id', users.routes.update());
app.delete('/api/users/:id', users.routes.delete());
// Stats API.
app.get('/api/stats', stats.routes.list());
import passport from "passport";
import { Strategy as LocalStrategy } from "passport-local";
import { Strategy as TwitterStrategy } from "passport-twitter";
import { OAuth2Strategy as GoogleStrategy } from "passport-google-oauth";
import db from "./db.js";
import keysConf from "../../config/passportKeys.json";
import config from "../../config/config.json";
import User from "./controllers/user.js";
import to from "await-to-js";
passport.serializeUser((user, done) => done(null, user.data._id));
passport.deserializeUser(async (id, done) => {
let [err, user] = await to(db.User.findById(id).exec());
if (err) {
console.error(err);
}
user = new User(user);
return done(err, user);
});
/*
* Sign in using Email and Password.
*/
passport.use(
new LocalStrategy(
{
user.name = profile.displayName;
user.handle = profile.username;
user.image = profile._json.profile_image_url;
user.save(function(err) {
if (err) {
throw err;
}
done(null, user);
});
}
})
}
));
passport.serializeUser(function(user, done) {
done(null, user.uid);
});
passport.deserializeUser(function(uid, done) {
User.findOne({
uid: uid
}, function(err, user) {
done(err, user);
});
});
} else {
console.log('Running in no-auth mode.');
config.requireAuth = false;
}
}
public mountPackage (_express: Application): Application {
_express = _express.use(passport.initialize());
_express = _express.use(passport.session());
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findById(id, (err, user) => {
done(err, user);
});
});
this.mountLocalStrategies();
return _express;
}
import passport from 'passport';
import { Strategy as LocalStrategy } from 'passport-local';
import authService from '../v1/auth/service';
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((user, done) => {
done(null, user);
});
passport.use(
new LocalStrategy(
{
usernameField: 'id',
passwordField: 'password',
},
async (id, password, done) => {
let user;
try {
function AuthStrategy () {
/**
* Passport Serialization of logged
* User to Session from request
*/
passport.serializeUser(function (user, done) {
done(null, user._id)
})
/**
* Passport Deserialization of logged
* User by Session into request
*/
passport.deserializeUser(function (userId, done) {
User
.findById(userId)
.exec(done)
})
/**
* Register Local Strategy