Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ storage/*
npm-debug.log
!/storage/codes/default/
docker
.idea
package-lock.json
pa
30 changes: 30 additions & 0 deletions api/config/config.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ const config = {
event_key: 'somekey',
},
sendgridAPIKey: 'key',
facebook: {
client_id: 'YOUR APP ID',
client_secret: 'YOUR APP SECRET',
callback_url: '{API URL}/user/callback/facebook',
},
google: {
client_id: 'YOUR APP ID',
client_secret: 'YOUR APP SECRET',
callback_url: '{API URL}/user/callback/google',
},
},
production: {
app: {
Expand All @@ -24,6 +34,16 @@ const config = {
event_key: 'somekey',
},
sendgridAPIKey: 'key',
facebook: {
client_id: 'YOUR APP ID',
client_secret: 'YOUR APP SECRET',
callback_url: '{API URL}/user/callback/facebook',
},
google: {
client_id: 'YOUR APP ID',
client_secret: 'YOUR APP SECRET',
callback_url: '{API URL}/user/callback/google',
},
},
test: {
app: {
Expand All @@ -37,6 +57,16 @@ const config = {
event_key: 'somekey',
},
sendgridAPIKey: 'key',
facebook: {
client_id: 'YOUR APP ID',
client_secret: 'YOUR APP SECRET',
callback_url: '{API URL}/user/callback/facebook',
},
google: {
client_id: 'YOUR APP ID',
client_secret: 'YOUR APP SECRET',
callback_url: '{API URL}/user/callback/google',
},
},
};
const env = process.env.NODE_ENV || 'development';
Expand Down
79 changes: 79 additions & 0 deletions api/routes/userAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ const isLoggedIn = require('../middlewares/isLoggedIn');

const router = express.Router();

router.use((req, res, next) => {
req.session.socketId = req.query.socketId;
next();
});

router.post('/register', [
check('username')
.not().isEmpty().withMessage('Username cannot be empty')
Expand Down Expand Up @@ -238,4 +243,78 @@ router.get('/verify/:username/:tokenSource', async (req, res) => {
return res.status(400).redirect('https://code.pragyan.org');
});

router.get('/auth/google', (req, res, next) => {
passport.authenticate('google', { scope: 'https://www.googleapis.com/auth/plus.login' }, () => null)(req, res, next);
});

router.get('/callback/google', (req, res, next) => {
passport.authenticate('google', { failureRedirect: '/' }, (err, user, info) => {
if (err) {
return res.status(500).json({
type: 'Error',
error: err,
});
}
if (!user) {
return res.status(400).json({
type: 'Error',
error: info,
});
}
req.logIn(user, (error) => {
if (error) {
return res.status(500).json({
type: 'Error',
error: 'Internal server error',
});
}
res.cookie('userId', user.id);
socket.sendMessage(user.id,
{
isLoggedIn: true, username: user.username, email: user.email, country: 'IN',
},
'google');
return res.end();
});
return null;
})(req, res, next);
});

router.get('/auth/facebook', (req, res, next) => {
passport.authenticate('facebook', { scope: ['id', 'email', 'public_profile', 'user_location'] }, () => null)(req, res, next);
});

router.get('/callback/facebook', (req, res, next) => {
passport.authenticate('facebook', { failureRedirect: '/' }, (err, user, info) => {
if (err) {
return res.status(500).json({
type: 'Error',
error: 'Internal server error',
});
}
if (!user) {
return res.status(400).json({
type: 'Error',
error: info,
});
}
req.logIn(user, (error) => {
if (error) {
return res.status(500).json({
type: 'Error',
error: 'Internal server error',
});
}
res.cookie('userId', user.id);
socket.sendMessage(user.id,
{
isLoggedIn: true, username: user.username, email: user.email, country: user.country,
},
'facebook');
return res.end();
});
return null;
})(req, res, next);
});

module.exports = router;
75 changes: 75 additions & 0 deletions api/utils/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,47 @@ const LocalStrategy = require('passport-local').Strategy;
const bcrypt = require('bcrypt');
const Sequelize = require('sequelize');
const rp = require('request-promise');
const FacebookStrategy = require('passport-facebook').Strategy;
const GoogleStrategy = require('passport-google-oauth2').Strategy;
const codeStatus = require('../models').codestatus;
const git = require('../utils/gitHandlers');
const User = require('../models').user;
const config = require('../config/config.js');


const { Op } = Sequelize;

const registerSocial = (user, callback) => {
User.findOne(
{ email: user.email },
)
.then((existingUser) => {
if (existingUser) {
return callback(null, existingUser, 'Success');
}
const passwordHash = bcrypt.hash(user.id, 10);
return User.create({
username: user.email,
fullname: user.name,
email: user.email,
password: passwordHash,
country: 'IN',
})
.then((createdUser) => {
git.createUserDir(createdUser.username)
.then(() => {
codeStatus.create({
userId: createdUser.id,
latestSrcPath: `${git.getUserDir(createdUser.username)}/code.cpp`,
});
});
return callback(null, createdUser, 'Success');
})
.catch(err => callback(err));
})
.catch(err => callback(err));
};

module.exports = (passport) => {
passport.use(new LocalStrategy(
((email, password, done) => {
Expand Down Expand Up @@ -80,4 +114,45 @@ module.exports = (passport) => {
done(error, null);
});
});
passport.use(new FacebookStrategy(
{
clientID: config.facebook.client_id,
clientSecret: config.facebook.client_secret,
callbackURL: config.facebook.callback_url,
profileFields: ['id', 'displayName', 'email'],
passReqToCallback: true,
},
async (req, accessToken, refreshToken, profile, done) => {
const data = await JSON.stringify(profile);
registerSocial(
{
id: data.id,
name: data.name,
email: data.email,
country: data.location.country || 'IN',
},
done,
);
},
));
passport.use(new GoogleStrategy(
{
clientID: config.google.client_id,
clientSecret: config.google.client_secret,
callbackURL: config.google.callback_url,
passReqToCallback: true,
},
async (req, accessToken, refreshToken, profile, done) => {
const data = await JSON.stringify(profile);
registerSocial(
{
id: data.id,
name: data.name,
email: data.email,
country: data.country || 'IN',
},
done,
);
},
));
};
Loading