basic passport structures

This commit is contained in:
bill bittner 2017-09-18 10:14:06 -07:00
parent 3325faf063
commit 0e9147d9d6
8 changed files with 82 additions and 40 deletions

View file

@ -21,18 +21,18 @@ module.exports = (sequelize, { STRING }) => {
},
{
freezeTableName: true,
instanceMethods: {
validPassword: function (password) {
return (password === this.password);
},
},
}
);
);
User.associate = db => {
User.hasMany(db.File);
User.hasOne(db.Certificate);
};
User.prototype.validPassword = (givenpassword, thispassword) => {
console.log(`${givenpassword} === ${thispassword}`);
return (givenpassword === thispassword);
};
return User;
};

View file

@ -32,6 +32,7 @@
"connect-multiparty": "^2.0.0",
"express": "^4.15.2",
"express-handlebars": "^3.0.0",
"express-session": "^1.15.5",
"helmet": "^3.8.1",
"mysql2": "^1.3.5",
"nodemon": "^1.11.0",

View file

@ -1,5 +1,6 @@
const PassportLocalStrategy = require('passport-local').Strategy;
const db = require('./models');
const db = require('../models');
const logger = require('winston');
module.exports = new PassportLocalStrategy(
{
@ -8,20 +9,21 @@ module.exports = new PassportLocalStrategy(
session : false,
passReqToCallback: true,
},
(username, password, done) => {
(req, username, password, done) => {
return db.User
.findOne({where: {channelName: username}})
.then(user => {
logger.debug('user', user.dataValues);
if (!user) {
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, user);
return done(null, user.dataValues);
})
.catch(error => {
return done(error);
});
},
}
);

View file

@ -1,32 +1,36 @@
const db = require('./models');
const db = require('../models');
const PassportLocalStrategy = require('passport-local').Strategy;
const lbryApi = require('../helpers/lbryApi.js');
const logger = require('winston');
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
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
},
(req, username, password, done) => {
console.log('inside local-signup');
// create the channel and retrieve the metadata
lbryApi.createChannel(username)
.then(channelInfo => {
// define an object that contains all the user data
const userData = {
channelName: username,
channelId : channelInfo.claim_Id,
password : password,
email : req.body.email.trim(),
channelName : username,
channelClaimId: channelInfo.claim_id,
password : password,
email : 'test email', // req.body.email.trim(),
};
return db.User.create(userData);
})
.then(user => {
logger.debug('User record was created successfully');
return done(null);
})
.catch(error => {
logger.debug(error);
return done(error);
});
},
}
);

View file

@ -2,9 +2,23 @@ const errorHandlers = require('../helpers/errorHandlers.js');
const db = require('../models');
const { postToStats, getStatsSummary, getTrendingClaims, getRecentClaims } = require('../controllers/statsController.js');
const passport = require('passport');
const { deAuthenticate } = require('../auth/authentication.js');
// const { deAuthenticate } = require('../auth/authentication.js');
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
app.get('/login', (req, res) => {
res.status(200).render('login');
@ -12,19 +26,14 @@ module.exports = (app) => {
app.get('/signup', (req, res) => {
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
// app.get('/users/:name', isAuthenticated, (req, res) => {
// res.status(200).render('profile');
// });
app.get('/logout', deAuthenticate, (req, res) => {
res.status(200).render('/');
});
// app.get('/logout', deAuthenticate, (req, res) => {
// res.status(200).render('/');
// });
// route to show 'about' page for spee.ch
app.get('/about', (req, res) => {
// get and render the content

View file

@ -13,6 +13,7 @@ const PORT = 3000; // set port
const app = express(); // create an Express application
const db = require('./models'); // require our models for syncing
const passport = require('passport');
const session = require('express-session');
// configure logging
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}`);
next();
});
// initialize passport
app.use(session({ secret: 'cats' }));
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
const localSignupStrategy = require('./passport/local-signup.js');
const localLoginStrategy = require('./passport/local-login.js');

View file

@ -1,11 +1,20 @@
<div class="wrapper">
{{> topBar}}
<div class="full">
{{#if isAuthenticated}}
{{> profile }}
{{else}}
{{> loginForm}}
{{/if}}
</div>
{{> footer}}
</div>
{{> topBar}}
<div class="full">
<h2>Log In</h2>
<form id="login-form" action="/login" method="post">
<div>
<label>Username:</label>
<input type="text" name="username"/>
</div>
<div>
<label>Password:</label>
<input type="password" name="password"/>
</div>
<div>
<input type="submit" value="Log In"/>
</div>
</form>
</div>
{{> footer}}
</div>

View file

@ -1,7 +1,8 @@
<div class="wrapper">
{{> topBar}}
<div class="full">
<form action="/login" method="post">
<h2>Sign up</h2>
<form action="/signup" method="post">
<div>
<label>Username:</label>
<input type="text" name="username"/>