Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(document).ready(function(){
- $('#btn-login').click(function() {
- var email = $('#loginEmail').val();
- var password = $('#loginPassword').val();
- var emailValidation = new RegExp(/^([w-]+(?:.[w-]+)*)@((?:[w-]+.)*w[w-]{0,66}).([a-z]{2,6}(?:.[a-z]{2})?)$/i);
- if(!email | !password) {
- $('#loginLabel').text('Email and password are required.');
- }
- else if(!emailValidation.test(email)) {
- $('#loginLabel').text('Email format is invalid.');
- }
- else {
- var userData = {
- username: email,
- password: password
- };
- var request = $.ajax({
- type: 'POST',
- url: '/login',
- data: userData
- });
- request.done(function (response, textStatus, jqXHR) {
- window.location.href = '/profile';
- }).fail(function (jqXHR, exception) {
- $('#loginLabel').text('Invalid email and/or password.');
- });
- }
- });
- });
- activateUserAccount(payload, res) {
- const token = payload.token;
- signUpService.validateEmailToken(token)
- .then(isVerified => {
- if(isVerified[0]) {
- const userData = JSON.stringify({
- username: isVerified[1],
- password: isVerified[2]
- });
- const options = {
- hostname: 'localhost',
- port: 3000,
- path: '/login',
- agent: false,
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- }
- };
- const req = http.request(options, (response) => {
- let responseString = '';
- response.on('data', data => {
- responseString += data;
- });
- response.on('end', (data) => {
- res.redirect('profile');
- });
- });
- req.write(userData);
- req.end();
- }
- else {
- res.redirect('error-page' + '?status=errit');
- }
- })
- .catch(err => console.log(err));
- }
- const expiryDate = new Date(Date.now() + 60 * 60 * 1000);
- app.use(session({
- secret: 'XeGcW4Vb23',
- resave: false,
- saveUninitialized: false,
- httpOnly: true,
- expires: expiryDate
- }));
- app.use(passport.initialize());
- app.use(passport.session());
- passport.serializeUser((user, done) => {
- done(null, user);
- });
- passport.deserializeUser((user, done) => {
- done(null, user);
- });
- const restrictedArea = (req, res, next) => {
- if (req.isAuthenticated()) {
- next();
- } else {
- res.redirect('/login');
- }
- };
- passport.use(new LocalStrategy(
- (username, password, done) => {
- console.log("strategy username");
- console.log(username);
- console.log("strategy password");
- console.log(password);
- console.log("strategy done");
- console.log(done);
- databaseManagement.selectUser(username, password)
- .then(user => _.isEmpty(user)
- ? done(null, false)
- : done(null, { username: username, password: password }));
- }
- ));
- app.post('/login', (req, res, next) => {
- console.log("REQ BODY");
- console.log(req.body);
- passport.authenticate('local', (err, user, info) => {
- console.log("REQ err");
- console.log(err);
- console.log("REQ user");
- console.log(user);
- console.log("REQ info");
- console.log(info);
- if(err) { return res.status(500).json(err);}
- if(!user) { return res.status(401).json(info);}
- req.logIn(user,(err) => {
- if (err) { return next(err); }
- return res.json({detail: info});
- });
- })(req, res, next);
- });
- app.get('/profile', restrictedArea, (req, res) => {
- profile.renderProfile(res);
- });
- REQ BODY
- { username: 'someemail@gmail.com', password: 'Potato123' }
- strategy username
- someemail@gmail.com
- strategy password
- Potato123
- strategy done
- [Function: verified]
- REQ err
- null
- REQ user
- { username: 'someemail@gmail.com', password: 'Potato123' }
- REQ info
- undefined
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement