Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SELESAI #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .env
Empty file.
62 changes: 62 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
const passport = require('passport');
const jwt = require('jsonwebtoken');
const session = require('express-session');

var index = require('./routes/index');
var users = require('./routes/users');
var login = require('./routes/login')

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
cookie: { secure: true }
}))
app.use(express.static(path.join(__dirname, 'public')));
app.use(passport.initialize());

let token = jwt.sign({foo: 'bar'}, 'This shit is token secret')

app.use('/', index);
app.use('/users', users);
app.use('/login', login)

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});



module.exports = app;
127 changes: 127 additions & 0 deletions authentication.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy
const FacebookStrategy = require('passport-facebook').Strategy
const TwitterStrategy = require('passport-twitter').Strategy
var GoogleStrategy = require('passport-google-oauth20').Strategy
var GitHubStrategy = require('passport-github').Strategy;

const jwt = require('jsonwebtoken');

let User = require('./models/user');



passport.use(new LocalStrategy(
function(username, password, done) {
User.findOne({username: username}, function(err, user) {
if(err) {done(err)}
if(!user) {done("User not found")}
if(password !== user.password) {
done("Wrong password")
}
let token = jwt.sign({user: user}, 'ThisShitisFuckingToken')
done(null, user)
})
}
))

passport.use(new FacebookStrategy({
clientID: '190233801464815',
clientSecret: '2fed281ec8d8b6aba6a06b9d771a4c60',
callbackURL: "http://localhost:3000/login/facebook",
},
function(accessToken, refreshToken, profile, cb) {
// console.log(profile);
User.findOne({ username: profile.displayName }, function (err, user) {
if(err) {
cb(err)
}
if(user) {
cb(null, "User already have access")
}
User.create({username: profile.displayName}, function(err) {
if(err) {
cb(err)
}
cb(null, "Success create user")
})
});
}
))

passport.use(new TwitterStrategy({
consumerKey: 'J4ZwEVtil3OJeDAGfJaPiBfue',
consumerSecret: 'LymPrw6MzHq0gK5Q1eWxa6NCUF18I6pGrVrqFKUlSOSIWcOCXE',
callbackURL: "http://localhost:3000/login/twitter"
},
function(accessToken, refreshToken, profile, cb) {
console.log(profile);
User.findOne({ username: profile.username }, function (err, user) {
if(err) {
cb(err)
}
if(user) {
cb(null, "User already have access")
}
User.create({username: profile.username}, function(err) {
if(err) {
cb(err)
}
cb(null, "Success create user")
})
});
}
));

passport.use(new GoogleStrategy({
clientID: '117416411129-hkhd9phcp59vnvdfba6vv7dq17fbv459.apps.googleusercontent.com',
clientSecret: 'Pf4134WeGZZG9Qm4Phq6kw6C',
callbackURL: "http://localhost:3000/login/google/callback"
},
function(accessToken, refreshToken, profile, cb) {
console.log(profile);
User.findOne({ username: profile.emails.value }, function (err, user) {
if(err) {
cb(err)
}
if(user) {
cb(null, "User already have access")
}
User.create({username: profile.emails.value}, function(err) {
if(err) {
cb(err)
}
cb(null, "Success create user")
})
});
}
));

passport.use(new GitHubStrategy({
clientID: 'f67835b8b07f702a952e',
clientSecret: '13064e087deca7771a2261afd42617ef0e411eb3',
callbackURL: "http://localhost:3000/login/github/callback"
},
function(accessToken, refreshToken, profile, cb) {
User.findOne({ username: profile.blog }, function (err, user) {
if(err) {
cb(err)
}
if(user) {
cb(null, "User already have access")
}
User.create({username: profile.blog }, function(err) {
if(err) {
cb(err)
}
cb(null, "Success create user")
})
});
}
));



passport.serializeUser(function(user, cb) {
cb(null, user);
});
90 changes: 90 additions & 0 deletions bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('api-oauth-thirdparty:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
17 changes: 17 additions & 0 deletions controllers/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
let User = require('../models/user')


register = function(req, res, next) {
let dataInput = new User({
username: req.body.username,
password: req.body.password
})
dataInput.save(function(err) {
if(err) res.send(err)
else {
res.send("Data has been created")
}
})
}

module.exports = {register: register}
10 changes: 10 additions & 0 deletions db/mongo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var mongoose = require('mongoose');
var mongoDB = 'mongodb://127.0.0.1/oauth_party_db';
mongoose.Promise = global.Promise;
mongoose.connect(mongoDB);
var db = mongoose.connection;

//Bind connection to error event (to get notification of connection errors)
db.on('error', console.error.bind(console, 'MongoDB connection error:'));

module.exports = db
13 changes: 13 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const mongoose = require('mongoose');
const db = require('../db/mongo');

let userSchema = new mongoose.Schema({
username: {type: String, unique: true},
password: String,
}, {
timestamps: true
})

let User = mongoose.model('User', userSchema)

module.exports = User
28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "api-oauth-thirdparty",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "~1.16.0",
"cookie-parser": "~1.4.3",
"debug": "~2.6.0",
"express": "~4.14.1",
"express-session": "^1.15.1",
"github": "^9.1.0",
"jade": "~1.11.0",
"jsonwebtoken": "^7.3.0",
"mongoose": "^4.8.5",
"morgan": "~1.7.0",
"passport": "^0.3.2",
"passport-facebook": "^2.1.1",
"passport-github": "^1.1.0",
"passport-google-oauth": "^1.0.0",
"passport-google-oauth20": "^1.0.0",
"passport-local": "^1.0.0",
"passport-twitter": "^1.0.4",
"serve-favicon": "~2.3.2"
}
}
8 changes: 8 additions & 0 deletions public/stylesheets/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}

a {
color: #00B7FF;
}
9 changes: 9 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});

module.exports = router;
Loading