Skip to content

Commit

Permalink
Merge pull request #67 from alexwohlbruck/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
alexwohlbruck authored Mar 2, 2018
2 parents d7977e8 + 1dde1b6 commit 271af4e
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 38 deletions.
71 changes: 42 additions & 29 deletions app/config/passport.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
var keys = require.main.require('./app/config/keys');
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
var User = require.main.require('./app/models/user');
var google = require('googleapis');
var googleConfig = require.main.require('./app/config/google');
var OAuth2 = google.auth.OAuth2;
const keys = require.main.require('./app/config/keys');
const GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
const User = require.main.require('./app/models/user');
const strings = require.main.require('./app/config/strings');

module.exports = function(passport) {
passport.serializeUser(function(user, done) {
module.exports = passport => {
passport.serializeUser((user, done) => {
done(null, user);
});

passport.deserializeUser(function(user, done) {
User.findById(user._id).then(function(user) {
passport.deserializeUser((user, done) => {
User.findById(user._id).then(user => {
done(null, user);
});
});
Expand All @@ -22,36 +20,51 @@ module.exports = function(passport) {
callbackURL: "/auth/google/callback",
accessType: 'offline',
proxy: true,
includeGrantedScopes: true
includeGrantedScopes: true,
passReqToCallback: true
},
function(accessToken, refreshToken, profile, done) {
(req, accessToken, refreshToken, profile, done) => {

User.findOne({'google.id': profile.id}, function(err, user) {
User.findOne({'google.id': profile.id}, (err, user) => {

const name = {
first: profile.name.givenName,
last: profile.name.familyName
},
email = profile.emails[0].value,
photo = profile.photos[0] && !profile._json.image.isDefault
? profile.photos[0].value.replace("?sz=50", "?sz=200")
: strings.userPhotoUrl,

google = {
id: profile.id,
accessToken: User.encryptAccessToken(accessToken),
refreshToken
},
ip = req.clientIp;

if (err) return done(err);

if (!user) {
user = new User({
name: {
first: profile.name.givenName,
last: profile.name.familyName
},
photo: profile.photos[0] && !profile._json.image.isDefault ? profile.photos[0].value.replace("?sz=50", "?sz=200") : undefined,
email: profile.emails[0].value,
google: {
id: profile.id,
accessToken: User.encryptAccessToken(accessToken),
refreshToken: refreshToken
}
});
user.save(function(err) {

user = new User({name, photo, email, google, ip});
user.save(err => {
if (err) console.log(err);
return done(err, user);
});

} else {

User.findByIdAndUpdate(user._id, {
'google.accessToken': User.encryptAccessToken(accessToken),
'google.refreshToken': refreshToken
}).then(function(user) {
'google.refreshToken': refreshToken,
ip,
photo
})

.then(user => {
user.google.accessToken = User.decryptAccessToken(user.google.accessToken);

return done(err, user);
});
}
Expand Down
3 changes: 2 additions & 1 deletion app/config/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ module.exports = {
recipient: {
exists: "That person is already being facted."
},
invalidNumber: "That phone number is invalid!"
invalidNumber: "That phone number is invalid!",
userPhotoUrl: "https://cat-fact.herokuapp.com/img/res/avatars/user-face.png"
};
6 changes: 4 additions & 2 deletions app/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ var Schema = mongoose.Schema;
var crypto = require('crypto');

var keys = require.main.require('./app/config/keys');
var strings = require.main.require('./app/config/strings');

var UserSchema = new Schema({
name: {
first: {type: String, required: true},
last: {type: String, required: true}
},
email: {type: String, required: true, unique: true},
photo: {type: String},
photo: {type: String, default: strings.userPhotoUrl},
google: {
id: {type: String},
accessToken: {type: String},
Expand All @@ -24,7 +25,8 @@ var UserSchema = new Schema({
enum: ['light', 'dark'],
default: 'light'
}
}
},
ip: String
}, {
timestamps: true
});
Expand Down
5 changes: 5 additions & 0 deletions app/routes/auth.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,9 @@ router.get('/google/contacts/callback', (req, res, next) => {
});
});

router.get('/signout', (req, res) => {
req.logout();
res.status(200).send();
});

module.exports = router;
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"passport-google-oauth": "^1.0.0",
"path": "^0.12.7",
"request": "^2.83.0",
"request-ip": "^2.0.2",
"request-promise": "^4.2.2",
"socket.io": "^1.4.8",
"twitter": "^1.7.1"
Expand Down
8 changes: 8 additions & 0 deletions public/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ md-toolbar md-nav-bar .md-button.md-accent {
color: #fff;
}

.sign-out-button {
padding: 0 !important;
margin-right: 10px !important;
}

.md-icon-button-small {
transform: scale(.8);
}
Expand Down Expand Up @@ -62,6 +67,9 @@ message .row-reverse .message-bubble {
width: 40px;
border-radius: 50%;
}
.avatar.small {
width: 35px;
}

.row-reverse {
flex-direction: row-reverse !important;
Expand Down
22 changes: 22 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ <h3>
<md-tooltip md-direction="bottom">About Cat Facts</md-tooltip>
<md-icon class="material-icons grey">info</md-icon>
</md-button>

<md-menu md-position-mode="target-right target" ng-if="authenticatedUser">
<md-button ng-click="$mdOpenMenu($event)" aria-label="Profile" class="md-icon-button sign-out-button">
<md-tooltip md-direction="bottom">Profile</md-tooltip>

<img
ng-src="{{authenticatedUser.photo}}"
class="avatar small md-whiteframe-1dp"
alt="{{authenticatedUser.email}}"
/>
</md-button>

<md-menu-content>
<md-menu-item>
<md-button ng-click="ApiService.signOut()">
Sign out
</md-button>
</md-menu-item>
</md-menu-content>
</md-menu>


</div>

<md-nav-bar md-selected-nav-item="startingState" nav-bar-aria-label="Navigation" ng-if="$state.current.data.showInNavigation">
Expand Down
4 changes: 2 additions & 2 deletions public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ app.config(['$mdThemingProvider', function($mdThemingProvider) {
$mdThemingProvider.alwaysWatchTheme(true);
}]);

app.run(['$rootScope', '$state', '$window', '$location', '$mdToast', 'ApiService', '$mdMedia',
function($rootScope, $state, $window, $location, $mdToast, ApiService, $mdMedia) {
app.run(['$rootScope', '$state', '$window', '$location', '$mdToast', 'ApiService', 'AuthService', '$mdMedia',
function($rootScope, $state, $window, $location, $mdToast, ApiService, AuthService, $mdMedia) {

$rootScope.authenticatedUser = null;
$rootScope.$mdMedia = $mdMedia;
Expand Down
2 changes: 2 additions & 0 deletions public/js/controllers/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ app.controller('MainCtrl', ['$scope', '$rootScope', '$mdSidenav', '$mdToast', '$
}
};

$scope.ApiService = ApiService;

$rootScope.showToast = function(message) {
return $mdToast.show(
$mdToast.simple()
Expand Down
12 changes: 9 additions & 3 deletions public/js/services/api.service.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global angular */
var app = angular.module('catfacts');

app.service('ApiService', ['$rootScope', '$http', function($rootScope, $http) {
app.service('ApiService', ['$rootScope', '$http', '$location', function($rootScope, $http, $location) {

// User

Expand All @@ -12,6 +12,13 @@ app.service('ApiService', ['$rootScope', '$http', function($rootScope, $http) {
this.updateUserSettings = function(data) {
return $http.put('/users/me/settings', data).then(data => console.log(data), err => console.log(err));
};

this.signOut = function() {
return $http.get('/auth/signout').then(() => {
$rootScope.authenticatedUser = null;
$location.path('/');
});
};

// Fact

Expand All @@ -33,8 +40,7 @@ app.service('ApiService', ['$rootScope', '$http', function($rootScope, $http) {

this.getFact = function(amount) {
return $http.get('/facts/random', {
params: { amount
}
params: { amount }
});
};

Expand Down
2 changes: 1 addition & 1 deletion public/views/facts.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ <h4 class="white md-body-2 accent-links margin-0 center-text">
<table md-table ng-model="selected" md-progress="promise">
<thead md-head>
<tr md-row md-row-select="false">
<th md-column><span>Name</span></th>
<th md-column><span>Added by</span></th>
<th md-column><span>Fact</span></th>
<th md-column md-numeric style="padding-right: 90px !important">Votes</th>
</tr>
Expand Down
2 changes: 2 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const methodOverride = require('method-override');
const session = require('express-session');
const requestIp = require('request-ip');
const MongoStore = require('connect-mongo')(session);
const server = require('http').Server(app);
const io = require('socket.io').listen(server);
Expand All @@ -28,6 +29,7 @@ app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(methodOverride('X-HTTP-Method-Override'));
app.use(express.static(__dirname + '/public'));
app.use(requestIp.mw());

const mongoStore = new MongoStore({url: keys.database.url()});
const sessionMiddleware = session({
Expand Down

0 comments on commit 271af4e

Please sign in to comment.