Browse Source

revert to telegram id as unique key

feature/telegramLogin
Nareshkumar Rao 3 years ago
parent
commit
fb4300b001
  1. 47
      app.js

47
app.js

@ -57,44 +57,35 @@ const Contact = sequelize.define('Contact', {
Contact.sync(); Contact.sync();
const User = sequelize.define('User', { const User = sequelize.define('User', {
email: {
telegram: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: false, allowNull: false,
unique: true, unique: true,
}, },
name: {
type: DataTypes.STRING,
},
hash: { hash: {
type: STRING, type: STRING,
}, },
phoneNumber: {
type: DataTypes.STRING,
},
verification: { verification: {
type: DataTypes.STRING, type: DataTypes.STRING,
}, },
org: {
type: DataTypes.STRING,
},
}); });
User.sync().then(() => { User.sync().then(() => {
if(!isProduction){
User.create({ User.create({
email: "admin@msolidariti.org",
name: "Demo",
telegram: "admin",
hash: bcrypt.hashSync("test", 10), hash: bcrypt.hashSync("test", 10),
phoneNumber: "123",
}).catch(e => { }).catch(e => {
console.log("Couldn't create demo account. Probably exists."); console.log("Couldn't create demo account. Probably exists.");
}); });
}
}); });
function authUser(email, password, done) {
function authUser(telegram, password, done) {
User.findOne({ User.findOne({
where: { where: {
email: email
telegram: telegram
} }
}).then(user => { }).then(user => {
if (!user) { if (!user) {
@ -115,11 +106,11 @@ function refreshVerification(user, done) {
}); });
} }
function createQRCode(email, done) {
function createQRCode(telegram, done) {
User.findOne({ User.findOne({
where: { where: {
email: email
telegram: telegram
} }
}).then(user => { }).then(user => {
refreshVerification(user, result => { refreshVerification(user, result => {
@ -145,13 +136,11 @@ function checkVerification(id, done) {
}); });
} }
function createUser(email, password, name, phoneNumber, done) {
function createUser(telegram, password, done) {
hash = bcrypt.hashSync(password, 10); hash = bcrypt.hashSync(password, 10);
User.create({ User.create({
email: email,
name: name,
telegram: telegram,
hash: hash, hash: hash,
phoneNumber: phoneNumber,
}).then(user => { }).then(user => {
if (!user) { if (!user) {
done(false, "Could not create user"); done(false, "Could not create user");
@ -167,8 +156,8 @@ function createUser(email, password, name, phoneNumber, done) {
}); });
} }
function addContact(userEmail, withUserID, done) {
User.findOne({ where: { email: userEmail } }).then(user => {
function addContact(telegram, withUserID, done) {
User.findOne({ where: { telegram: telegram } }).then(user => {
Contact.create({ user: user.id, with: withUserID }) Contact.create({ user: user.id, with: withUserID })
.then(res => { .then(res => {
done(true, "Successfully added contact"); done(true, "Successfully added contact");
@ -199,12 +188,12 @@ app.use(cors({ credentials: true, origin: true, secure: true }));
app.use(express.json()) app.use(express.json())
app.post('/login', (req, res) => { app.post('/login', (req, res) => {
reqEmail = req.body.email.toLowerCase();
const auth = authUser(reqEmail, req.body.password, (success, msg) => {
reqTelegram = req.body.telegram.toLowerCase();
const auth = authUser(reqTelegram, req.body.password, (success, msg) => {
if (success) { if (success) {
req.session.regenerate(() => { req.session.regenerate(() => {
cookieExpiry = getCookieExpiry(); cookieExpiry = getCookieExpiry();
req.session.user = reqEmail;
req.session.user = reqTelegram;
res.send({ authorized: success, message: msg }) res.send({ authorized: success, message: msg })
}); });
} else { } else {
@ -214,11 +203,11 @@ app.post('/login', (req, res) => {
}); });
app.post('/create', (req, res) => { app.post('/create', (req, res) => {
reqEmail = req.body.email.toLowerCase();
reqTelegram = req.body.telegram.toLowerCase();
if (req.session.verified) { if (req.session.verified) {
createUser(reqEmail, req.body.password, req.body.name, req.body.phoneNumber, (success, msg) => {
createUser(reqTelegram, req.body.password, (success, msg) => {
cookieExpiry = getCookieExpiry(); cookieExpiry = getCookieExpiry();
req.session.user = reqEmail;
req.session.user = reqTelegram;
if (success) { if (success) {
addContact(req.session.user, req.session.verifiedBy, (sucesss, msg) => { addContact(req.session.user, req.session.verifiedBy, (sucesss, msg) => {
res.send({ success: success, message: msg }); res.send({ success: success, message: msg });

Loading…
Cancel
Save