Permalink
Browse files

twitter oauth added

  • Loading branch information...
1 parent 6d7e711 commit d7c2c98eee53a215f4b226c6e51d424c337f5a68 @Hitman666 committed Jan 12, 2015
@@ -5,17 +5,21 @@ var mongoose = require('mongoose'),
var UserSchema = new Schema({
name: String,
email: String,
- username: String,
+ username: {
+ type: String,
+ trim: true,
+ unique: true
+ },
password: String,
provider: String,
providerId: String,
- providerData: {}
+ providerData: {},
+ todos: {}
});
UserSchema.pre('save',
function(next) {
if (this.password) {
- //this.salt = new Buffer(crypto.randomBytes(16).toString('base64'), 'base64');
var md5 = crypto.createHash('md5');
this.password = md5.update(this.password).digest('hex');
}
@@ -24,10 +28,6 @@ UserSchema.pre('save',
}
);
-// UserSchema.methods.hashPassword = function(password) {
-// return crypto.pbkdf2Sync(password, this.salt, 10000, 64).toString('base64');
-// };
-
UserSchema.methods.authenticate = function(password) {
var md5 = crypto.createHash('md5');
md5 = md5.update(password).digest('hex');
@@ -57,9 +57,9 @@ UserSchema.statics.findUniqueUsername = function(username, suffix, callback) {
);
};
-UserSchema.set('toJSON', {
- getters: true,
- virtuals: true
-});
+// UserSchema.set('toJSON', {
+// getters: true,
+// virtuals: true
+// });
mongoose.model('User', UserSchema);
@@ -32,4 +32,13 @@ module.exports = function(app) {
successRedirect: '/',
scope:['email']
}));
+
+ app.get('/oauth/twitter', passport.authenticate('twitter', {
+ failureRedirect: '/signin'
+ }));
+
+ app.get('/oauth/twitter/callback', passport.authenticate('twitter', {
+ failureRedirect: '/signin',
+ successRedirect: '/'
+ }));
};
View
@@ -4,6 +4,9 @@
<title>
<%=title %>
</title>
+
+ <link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet">
+ <link href="http://getbootstrap.com/examples/signin/signin.css" rel="stylesheet">
</head>
<body>
<% for(var i in messages) { %>
@@ -12,29 +15,26 @@
</div>
<% } %>
- <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="Login" />
- </div>
- </form>
-
- <a href="/oauth/facebook">Login with Facebook</a>
+ <div class="container">
+ <form class="form-signin" action="/login" method="post">
+ <h2 class="form-signin-heading text-center">Open, says me</h2>
+
+ <label for="inputUsername" class="sr-only">Username</label>
+ <input type="email" id="inputUsername" class="form-control" placeholder="Username" required autofocus>
+
+ <label for="inputPassword" class="sr-only">Password</label>
+ <input type="password" id="inputPassword" class="form-control" placeholder="Password" required>
+
+ <button class="btn btn-lg btn-primary btn-block" type="submit">Login</button>
+ </form>
- <div
- class="fb-like"
- data-share="true"
- data-width="450"
- data-show-faces="true">
+ <div class="row">
+ <div class="center-block text-center">
+ <a href="/oauth/facebook">Login with Facebook</a> |
+ <a href="/oauth/twitter">Login with Twitter</a>
+ </div>
+ </div>
+ </div>
</div>
</body>
</html>
@@ -4,31 +4,30 @@
<title>
<%=title %>
</title>
+
+ <link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet">
+ <link href="http://getbootstrap.com/examples/signin/signin.css" rel="stylesheet">
</head>
<body>
<% for(var i in messages) { %>
<div class="flash"><%= messages[i] %></div>
<% } %>
- <form action="/register" method="post">
- <div>
- <label>Name:</label>
- <input type="text" name="firstName" />
- </div>
- <div>
- <label>Email:</label>
- <input type="text" name="email" />
- </div>
- <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="Register" />
- </div>
- </form>
+
+ <div class="container">
+ <form class="form-signin" action="/register" method="post">
+ <h2 class="form-signin-heading text-center">Register</h2>
+
+ <label for="name" class="sr-only">Username</label>
+ <input type="text" id="name" name="name" class="form-control" placeholder="Name" required autofocus>
+
+ <label for="email" class="sr-only">E-mail</label>
+ <input type="email" id="email" name="email" class="form-control" placeholder="E-mail" required>
+
+ <label for="password" class="sr-only">Password</label>
+ <input type="password" id="password" class="form-control" placeholder="Password" required>
+
+ <button class="btn btn-lg btn-primary btn-block" type="submit">Register</button>
+ </form>
+ </div>
</body>
</html>
@@ -2,8 +2,13 @@ module.exports = {
port: '1337',
db: 'mongodb://localhost/todos',
facebook: {
- clientID: '534090410026600',
- clientSecret: '6ca861523c836cb9affbad6ca6b7ffdb',
+ clientID: '513828288756645',
+ clientSecret: '2d7cc991efddb864e9af61f307980b9a',
callbackURL: 'http://localhost:1337/oauth/facebook/callback'
+ },
+ twitter: {
+ clientID: 'yFntGKkvMZkDKL47XGtzLNdRA',
+ clientSecret: 'EAiPTjPYLX5nrkpRtxYQflbWpRTqqLwwBHRLh7WpdQ1P69Tre6',
+ callbackURL: 'http://localhost:1337/oauth/twitter/callback'
}
};
View
@@ -20,4 +20,5 @@ module.exports = function() {
require('./strategies/local.js')();
require('./strategies/facebook.js')();
+ require('./strategies/twitter.js')();
};
@@ -0,0 +1,28 @@
+var passport = require('passport'),
+ url = require('url'),
+ TwitterStrategy = require('passport-twitter').Strategy,
+ config = require('../config'),
+ users = require('../../app/controllers/users.server.controller');
+
+module.exports = function() {
+ passport.use(new TwitterStrategy({
+ consumerKey: config.twitter.clientID,
+ consumerSecret: config.twitter.clientSecret,
+ callbackURL: config.twitter.callbackURL,
+ passReqToCallback: true
+ },
+ function(req, token, tokenSecret, profile, done) {
+ var providerData = profile._json;
+ providerData.token = token;
+ providerData.tokenSecret = tokenSecret;
+ var providerUserProfile = {
+ fullName: profile.displayName,
+ username: profile.username,
+ provider: 'twitter',
+ providerId: profile.id,
+ providerData: providerData
+ };
+
+ users.saveOAuthUserProfile(req, providerUserProfile, done);
+ }));
+};
View
@@ -27,6 +27,7 @@
"express": "^4.10.7",
"mongoose": "^3.8.21",
"passport": "^0.2.1",
- "passport-facebook": "^1.0.3"
+ "passport-facebook": "^1.0.3",
+ "passport-twitter": "^1.0.2"
}
}
View
@@ -1,5 +1,4 @@
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
-console.log(process.env.NODE_ENV);
var config = require('./config/config'),
mongoose = require('./config/mongoose'),
@@ -13,4 +12,4 @@ var db = mongoose(),
app.listen(config.port);
module.exports = app;
-console.log('Server running at http://localhost:' + config.port);
+console.log(process.env.NODE_ENV + ' server running at http://localhost:' + config.port);

0 comments on commit d7c2c98

Please sign in to comment.