Last active
October 12, 2018 10:16
-
-
Save kuznetsovandrey76/92c426b4c52787424976e6bd6043106d to your computer and use it in GitHub Desktop.
SERVER login
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "learn", | |
"version": "1.0.0", | |
"description": "server login", | |
"main": "server.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1", | |
"start": "node server.js" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"express": "*", | |
"express-handlebars": "*", | |
"crypto": "*", | |
"passport": "*", | |
"passport-vkontakte": "*", | |
"cookie-parser": "*", | |
"express-session": "*" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'), | |
crypto = require('crypto'), | |
bodyParser = require('body-parser'), | |
hbs = require('express-handlebars'), | |
passport = require('passport'), | |
authVK = require('passport-vkontakte').Strategy, | |
users = require('./users'), | |
app = express(); | |
app.engine('hbs', hbs({extname: 'hbs', defaultLayout: 'main', layoutsDir: './views/layouts/'})); | |
app.set('views', './views'); | |
app.set('view engine', 'hbs'); | |
app.use(require('cookie-parser')()); | |
app.use(require('express-session')({secret:'keyboard cat', resave: true, saveUninitialized: true})); | |
app.use(require('body-parser').urlencoded({extended: true})); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
passport.use(new authVK( | |
{ | |
clientID: 6498230, | |
clientSecret: 'Pn7rFqL7sPoY1kbgQyGL', | |
callbackURL: "http://localhost:3000/auth/vkontakte/callback" | |
}, | |
function myVerifyCallbackFn(accessToken, refreshToken, profile, done) { | |
var user = { | |
nickname: profile.username, // kuznetsovandrey76 | |
username: profile.displayName // Andrey Kuznetsov | |
} | |
// Объект записывается в req | |
return done(null, user); | |
} | |
)); | |
passport.serializeUser(function (user, done) { | |
done(null, JSON.stringify(user)); | |
}); | |
passport.deserializeUser(function (data, done) { | |
try { | |
done(null, JSON.parse(data)); | |
} catch (e) { | |
done(err) | |
} | |
}); | |
app.get('/auth/vkontakte/callback', | |
passport.authenticate('vkontakte', { failureRedirect: '/login' }), | |
function (req, res) { | |
// Successful authentication, redirect login. | |
res.render('login', {login: req.user.nickname}); | |
}); | |
app.get('/', function (req, res) { | |
res.render('home'); | |
}); | |
var info = { | |
login: "", | |
pass: "", | |
toString: function() { | |
return this.login + "=" + this.pass; | |
} | |
}; | |
app.post('/login', function(req, res) { | |
info.login = req.body.login; | |
info.pass = req.body.pass; | |
// crypto | |
// var secret = 'salt', | |
// info.pass = crypto.createHmac('sha256', secret) | |
// .update(req.body.pass) | |
// .digest('hex'); | |
}); | |
app.get('/login', function (req, res) { | |
var current_time = new Date().toString().slice(4, 24), | |
check_name = users.indexOf(info.toString()); | |
if(check_name != -1) { | |
// Вывод в логи | |
console.log("login:" + info.login + " sign in " + current_time); | |
res.render('login', {login: info.login, pass: info.pass}); | |
} else { | |
console.log('ERR'); | |
res.redirect('/'); | |
} | |
}); | |
app.listen(3000, function () { | |
console.log('Express запущен на http://localhost:3000'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var users = [ | |
"login=pass", | |
"andrey=123" | |
]; | |
module.exports = users; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
В приложении VK включаем - Open API
Адрес сайта:
http://localhost:3000
Базовый домен:
localhost