basic passport structures
This commit is contained in:
parent
3325faf063
commit
0e9147d9d6
8 changed files with 82 additions and 40 deletions
|
@ -21,18 +21,18 @@ module.exports = (sequelize, { STRING }) => {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
freezeTableName: true,
|
freezeTableName: true,
|
||||||
instanceMethods: {
|
|
||||||
validPassword: function (password) {
|
|
||||||
return (password === this.password);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
User.associate = db => {
|
User.associate = db => {
|
||||||
User.hasMany(db.File);
|
User.hasMany(db.File);
|
||||||
User.hasOne(db.Certificate);
|
User.hasOne(db.Certificate);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
User.prototype.validPassword = (givenpassword, thispassword) => {
|
||||||
|
console.log(`${givenpassword} === ${thispassword}`);
|
||||||
|
return (givenpassword === thispassword);
|
||||||
|
};
|
||||||
|
|
||||||
return User;
|
return User;
|
||||||
};
|
};
|
||||||
|
|
|
@ -32,6 +32,7 @@
|
||||||
"connect-multiparty": "^2.0.0",
|
"connect-multiparty": "^2.0.0",
|
||||||
"express": "^4.15.2",
|
"express": "^4.15.2",
|
||||||
"express-handlebars": "^3.0.0",
|
"express-handlebars": "^3.0.0",
|
||||||
|
"express-session": "^1.15.5",
|
||||||
"helmet": "^3.8.1",
|
"helmet": "^3.8.1",
|
||||||
"mysql2": "^1.3.5",
|
"mysql2": "^1.3.5",
|
||||||
"nodemon": "^1.11.0",
|
"nodemon": "^1.11.0",
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
const PassportLocalStrategy = require('passport-local').Strategy;
|
const PassportLocalStrategy = require('passport-local').Strategy;
|
||||||
const db = require('./models');
|
const db = require('../models');
|
||||||
|
const logger = require('winston');
|
||||||
|
|
||||||
module.exports = new PassportLocalStrategy(
|
module.exports = new PassportLocalStrategy(
|
||||||
{
|
{
|
||||||
|
@ -8,20 +9,21 @@ module.exports = new PassportLocalStrategy(
|
||||||
session : false,
|
session : false,
|
||||||
passReqToCallback: true,
|
passReqToCallback: true,
|
||||||
},
|
},
|
||||||
(username, password, done) => {
|
(req, username, password, done) => {
|
||||||
return db.User
|
return db.User
|
||||||
.findOne({where: {channelName: username}})
|
.findOne({where: {channelName: username}})
|
||||||
.then(user => {
|
.then(user => {
|
||||||
|
logger.debug('user', user.dataValues);
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return done(null, false, {message: 'Incorrect username or password.'});
|
return done(null, false, {message: 'Incorrect username or password.'});
|
||||||
}
|
}
|
||||||
if (!user.validPassword(password)) {
|
if (!user.validPassword(password, user.password)) {
|
||||||
return done(null, false, {message: 'Incorrect username or password.'});
|
return done(null, false, {message: 'Incorrect username or password.'});
|
||||||
}
|
}
|
||||||
return done(null, user);
|
return done(null, user.dataValues);
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
return done(error);
|
return done(error);
|
||||||
});
|
});
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,32 +1,36 @@
|
||||||
const db = require('./models');
|
const db = require('../models');
|
||||||
const PassportLocalStrategy = require('passport-local').Strategy;
|
const PassportLocalStrategy = require('passport-local').Strategy;
|
||||||
const lbryApi = require('../helpers/lbryApi.js');
|
const lbryApi = require('../helpers/lbryApi.js');
|
||||||
|
const logger = require('winston');
|
||||||
|
|
||||||
module.exports = new PassportLocalStrategy(
|
module.exports = new PassportLocalStrategy(
|
||||||
{
|
{
|
||||||
usernameField : 'email', // sets the custom name of parameters in the POST body message
|
usernameField : 'username', // sets the custom name of parameters in the POST body message
|
||||||
passwordField : 'password', // sets the custom name of parameters in the POST body message
|
passwordField : 'password', // sets the custom name of parameters in the POST body message
|
||||||
session : false, // set to false because we will use token approach to auth
|
session : false, // set to false because we will use token approach to auth
|
||||||
passReqToCallback: true, // we want to be able to read the post body message parameters in the callback
|
passReqToCallback: true, // we want to be able to read the post body message parameters in the callback
|
||||||
},
|
},
|
||||||
(req, username, password, done) => {
|
(req, username, password, done) => {
|
||||||
|
console.log('inside local-signup');
|
||||||
// create the channel and retrieve the metadata
|
// create the channel and retrieve the metadata
|
||||||
lbryApi.createChannel(username)
|
lbryApi.createChannel(username)
|
||||||
.then(channelInfo => {
|
.then(channelInfo => {
|
||||||
// define an object that contains all the user data
|
// define an object that contains all the user data
|
||||||
const userData = {
|
const userData = {
|
||||||
channelName: username,
|
channelName : username,
|
||||||
channelId : channelInfo.claim_Id,
|
channelClaimId: channelInfo.claim_id,
|
||||||
password : password,
|
password : password,
|
||||||
email : req.body.email.trim(),
|
email : 'test email', // req.body.email.trim(),
|
||||||
};
|
};
|
||||||
return db.User.create(userData);
|
return db.User.create(userData);
|
||||||
})
|
})
|
||||||
.then(user => {
|
.then(user => {
|
||||||
|
logger.debug('User record was created successfully');
|
||||||
return done(null);
|
return done(null);
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
|
logger.debug(error);
|
||||||
return done(error);
|
return done(error);
|
||||||
});
|
});
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
|
|
|
@ -2,9 +2,23 @@ const errorHandlers = require('../helpers/errorHandlers.js');
|
||||||
const db = require('../models');
|
const db = require('../models');
|
||||||
const { postToStats, getStatsSummary, getTrendingClaims, getRecentClaims } = require('../controllers/statsController.js');
|
const { postToStats, getStatsSummary, getTrendingClaims, getRecentClaims } = require('../controllers/statsController.js');
|
||||||
const passport = require('passport');
|
const passport = require('passport');
|
||||||
const { deAuthenticate } = require('../auth/authentication.js');
|
// const { deAuthenticate } = require('../auth/authentication.js');
|
||||||
|
|
||||||
module.exports = (app) => {
|
module.exports = (app) => {
|
||||||
|
// route for auth
|
||||||
|
app.post('/signup', passport.authenticate('local-signup'), (req, res) => {
|
||||||
|
console.log('redirecting to user channel');
|
||||||
|
// If this function gets called, authentication was successful.
|
||||||
|
// `req.user` contains the authenticated user.
|
||||||
|
res.redirect('/@' + req.user.channelName);
|
||||||
|
});
|
||||||
|
app.post('/login', passport.authenticate('local-login'), (req, res) => {
|
||||||
|
console.log('redirecting to user channel');
|
||||||
|
// If this function gets called, authentication was successful.
|
||||||
|
// `req.user` contains the authenticated user.
|
||||||
|
res.redirect('/@' + req.user.channelName);
|
||||||
|
});
|
||||||
|
|
||||||
// route to display login page
|
// route to display login page
|
||||||
app.get('/login', (req, res) => {
|
app.get('/login', (req, res) => {
|
||||||
res.status(200).render('login');
|
res.status(200).render('login');
|
||||||
|
@ -12,19 +26,14 @@ module.exports = (app) => {
|
||||||
app.get('/signup', (req, res) => {
|
app.get('/signup', (req, res) => {
|
||||||
res.status(200).render('signup');
|
res.status(200).render('signup');
|
||||||
});
|
});
|
||||||
// route for auth
|
|
||||||
app.post('/login', passport.authenticate('local-login'), (req, res) => {
|
|
||||||
// If this function gets called, authentication was successful.
|
|
||||||
// `req.user` contains the authenticated user.
|
|
||||||
res.redirect('/@' + req.user.username);
|
|
||||||
});
|
|
||||||
// route to display login page
|
// route to display login page
|
||||||
// app.get('/users/:name', isAuthenticated, (req, res) => {
|
// app.get('/users/:name', isAuthenticated, (req, res) => {
|
||||||
// res.status(200).render('profile');
|
// res.status(200).render('profile');
|
||||||
// });
|
// });
|
||||||
app.get('/logout', deAuthenticate, (req, res) => {
|
// app.get('/logout', deAuthenticate, (req, res) => {
|
||||||
res.status(200).render('/');
|
// res.status(200).render('/');
|
||||||
});
|
// });
|
||||||
|
|
||||||
// route to show 'about' page for spee.ch
|
// route to show 'about' page for spee.ch
|
||||||
app.get('/about', (req, res) => {
|
app.get('/about', (req, res) => {
|
||||||
// get and render the content
|
// get and render the content
|
||||||
|
|
18
speech.js
18
speech.js
|
@ -13,6 +13,7 @@ const PORT = 3000; // set port
|
||||||
const app = express(); // create an Express application
|
const app = express(); // create an Express application
|
||||||
const db = require('./models'); // require our models for syncing
|
const db = require('./models'); // require our models for syncing
|
||||||
const passport = require('passport');
|
const passport = require('passport');
|
||||||
|
const session = require('express-session');
|
||||||
|
|
||||||
// configure logging
|
// configure logging
|
||||||
const logLevel = config.get('Logging.LogLevel');
|
const logLevel = config.get('Logging.LogLevel');
|
||||||
|
@ -31,9 +32,24 @@ app.use((req, res, next) => { // custom logging middleware to log all incomming
|
||||||
logger.verbose(`Request on ${req.originalUrl} from ${req.ip}`);
|
logger.verbose(`Request on ${req.originalUrl} from ${req.ip}`);
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
// initialize passport
|
// initialize passport
|
||||||
|
app.use(session({ secret: 'cats' }));
|
||||||
app.use(passport.initialize());
|
app.use(passport.initialize());
|
||||||
|
app.use(passport.session());
|
||||||
|
passport.serializeUser(function (user, done) {
|
||||||
|
done(null, user.id);
|
||||||
|
});
|
||||||
|
|
||||||
|
passport.deserializeUser(function (id, done) {
|
||||||
|
db.User.findOne({ where: { id } })
|
||||||
|
.then(user => {
|
||||||
|
done(null, user.dataValues);
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
logger.error('sequelize error', error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// Load passport strategies
|
// Load passport strategies
|
||||||
const localSignupStrategy = require('./passport/local-signup.js');
|
const localSignupStrategy = require('./passport/local-signup.js');
|
||||||
const localLoginStrategy = require('./passport/local-login.js');
|
const localLoginStrategy = require('./passport/local-login.js');
|
||||||
|
|
|
@ -1,11 +1,20 @@
|
||||||
<div class="wrapper">
|
<div class="wrapper">
|
||||||
{{> topBar}}
|
{{> topBar}}
|
||||||
<div class="full">
|
<div class="full">
|
||||||
{{#if isAuthenticated}}
|
<h2>Log In</h2>
|
||||||
{{> profile }}
|
<form id="login-form" action="/login" method="post">
|
||||||
{{else}}
|
<div>
|
||||||
{{> loginForm}}
|
<label>Username:</label>
|
||||||
{{/if}}
|
<input type="text" name="username"/>
|
||||||
</div>
|
</div>
|
||||||
{{> footer}}
|
<div>
|
||||||
</div>
|
<label>Password:</label>
|
||||||
|
<input type="password" name="password"/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<input type="submit" value="Log In"/>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
{{> footer}}
|
||||||
|
</div>
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
<div class="wrapper">
|
<div class="wrapper">
|
||||||
{{> topBar}}
|
{{> topBar}}
|
||||||
<div class="full">
|
<div class="full">
|
||||||
<form action="/login" method="post">
|
<h2>Sign up</h2>
|
||||||
|
<form action="/signup" method="post">
|
||||||
<div>
|
<div>
|
||||||
<label>Username:</label>
|
<label>Username:</label>
|
||||||
<input type="text" name="username"/>
|
<input type="text" name="username"/>
|
Loading…
Add table
Reference in a new issue