Instantly share code, notes, and snippets.

Embed
What would you like to do?
SERVER login
<input type="hidden" name="act" id="act" value="login">
<input type="text" name="login" id="login" value="" placeholder="login">
<input type="password" name="pass" id="pass" value="" placeholder="password">
<input type="submit" id="submit" value="Send">
<input type="submit" id="vk" value="vk">
<script>
$(document).ready(function() {
$('#vk').click(function() {
window.location = '/auth/vk'
});
$('#submit').click(function() {
var login = $('#login').val(),
pass = $('#pass').val();
$.ajax({
url: "/login",
method: "post",
data: {login: login, pass: pass},
beforeSend: function(jqXHR, settings) {
window.location = '/login'
},
success: function(data, textStatus, jqXHR){
// console.log(data);
}
});
});
});
</script>
<div>
login: {{login}}, password: {{pass}}
</div>
<script>
var login = '{{login}}',
pass = '{{pass}}';
document.cookie = "login=" + login;
document.cookie = "pass=" + pass;
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
{{{body}}}
</body>
</html>
{
"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": "*"
}
}
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');
});
var users = [
"login=pass",
"andrey=123"
];
module.exports = users;
@kuznetsovandrey76

This comment has been minimized.

Owner

kuznetsovandrey76 commented Oct 11, 2018

public/
views/
-- layouts/
   -- main.hbs
-- home.hbs
server.js

В приложении VK включаем - Open API
Адрес сайта: http://localhost:3000
Базовый домен: localhost

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment