Skip to content

Commit

Permalink
Merge pull request #45 from placidrod/master
Browse files Browse the repository at this point in the history
Fix authentication
  • Loading branch information
ScottRudiger authored Jul 4, 2017
2 parents 64620bc + 0af41ee commit 3149d7a
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 40 deletions.
6 changes: 4 additions & 2 deletions public/src/components/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export default class App extends React.Component {
currentAlbum: null,
currentPhoto: 0,
currentAlbumIndex: 5,
currentUser: {}, // change
currentUser: {
albums: []
}, // change
displayUser: {} // change
};
}
Expand Down Expand Up @@ -105,7 +107,7 @@ export default class App extends React.Component {
render() {
return (
<div>
<Navbar addPhoto={this.addPhoto.bind(this)}/>
<Navbar addPhoto={this.addPhoto.bind(this)} currentUser={this.state.currentUser}/>
<div className="container-fluid">
<this.renderPage
currentAlbum={this.state.currentAlbum}
Expand Down
4 changes: 2 additions & 2 deletions public/src/components/navbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
// import Bootstrap from 'bootstrap';
import PhotoForm from './photoform.jsx';

const Navbar = ({addPhoto}) => {
const Navbar = ({addPhoto, currentUser}) => {
return (

<nav className="navbar navbar-default navbar-fixed-top">
Expand All @@ -24,7 +24,7 @@ const Navbar = ({addPhoto}) => {
<li className="dropdown">
<a className="dropdown-toggle" href="#" data-toggle="dropdown">Upload <span className="caret"></span></a>
<div className="dropdown-menu">
<PhotoForm addPhoto={addPhoto}/>
<PhotoForm addPhoto={addPhoto} currentUser={currentUser}/>
</div>
</li>

Expand Down
14 changes: 11 additions & 3 deletions public/src/components/photoform.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import React from 'react';

const PhotoForm = ({addPhoto}) => {
const PhotoForm = ({addPhoto, currentUser}) => {
let photo;
let name;
let desc;
let newName;
let albums;

if(currentUser.albums.length) {
albums = currentUser.albums.map((album) => {
return <option value={album.name}>{album.name}</option>
});
} else {
albums = <option value="All Photos">All Photos</option>;
}

$('select#name').on('change', function(e) {
e.preventDefault();
Expand Down Expand Up @@ -40,8 +49,7 @@ const PhotoForm = ({addPhoto}) => {
<div className="form-group">
<select id="name" className="form-control" name="name">
<option value="">Select Album</option>
<option value="personal">Personal</option>
<option value="office">Office</option>
{albums}
<option value="__newalbum">Create New Album</option>
</select>
</div>
Expand Down
4 changes: 1 addition & 3 deletions server/app.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
const express = require('express');
// const router = require('./routes.js');
const cors = require('cors');
const path = require('path');
const session = require('express-session');
const requestHandler = require('./lib/request-handler.js');
const bodyParser = require('body-parser');


const app = express();


Expand Down Expand Up @@ -34,7 +32,7 @@ app.use(session({
app.get('/', authenticate);

app.get('/user/:username', requestHandler.getUser);
// app.get('/user/albums/:username', requestHandler.getUser);

app.post('/user/upload', requestHandler.handleUploadPhoto);


Expand Down
17 changes: 0 additions & 17 deletions server/db/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,6 @@ const userSchema = new Schema({
friends: [],
});



userSchema.pre('save', function (next) {
const cipher = Promise.promisify(bcrypt.hash);
if (!this.profilePic) {
this.profilePic = 'http://www.lovemarks.com/wp-content/uploads/profile-avatars/default-avatar-tech-guy.png';
}

return cipher(this.password, null, null).bind(this)
.then(function (hash) {
this.password = hash;
next();
});
});



const User = mongoose.model('User', userSchema);

User.comparePassword = function (candidatePassword, savedPassword, cb) {
Expand Down
37 changes: 24 additions & 13 deletions server/lib/request-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const multer = require('multer');
const cloudinaryApi = require('./cloudinaryApi.js');
const _ = require('underscore');
const path = require('path');
const bcrypt = require('bcrypt-nodejs');
const Promise = require('bluebird');

const upload = multer().single('photo');
const requestHandler = {};
Expand Down Expand Up @@ -176,19 +178,28 @@ requestHandler.sendLogin = function (req, res) {


function createUser(user, req, res) {
User.create(
user,
(error, user) => {// eslint-disable-line
if (error) {
res.status(500).redirect('/signup');
} else {
req.session.regenerate(() => {
req.session.username = req.body.username;
res.redirect('/');
});
}
} // eslint-disable-line
);
const cipher = Promise.promisify(bcrypt.hash);
if (!user.profilePic) {
user.profilePic = 'http://www.lovemarks.com/wp-content/uploads/profile-avatars/default-avatar-tech-guy.png';
}
cipher(user.password, null, null)
.then(function (hash) {
user.password = hash;
// console.log(hash);
User.create(
user,
(error, user) => {// eslint-disable-line
if (error) {
res.status(500).redirect('/signup');
} else {
req.session.regenerate(() => {
req.session.username = req.body.username;
res.redirect('/');
});
}
} // eslint-disable-line
);
});
}

requestHandler.handleSignup = function (req, res) {
Expand Down

0 comments on commit 3149d7a

Please sign in to comment.