Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let mysql = require('mysql')
- , SHA256 = require('crypto-js/sha256')
- , config = require('./config')
- , connection = mysql.createConnection(config.db)
- class User {
- constructor() {
- this.logged = false
- this.loginAttempts = 0
- this.maxLoginAttempts = 3
- this.login = this.bind (this.login, this.onBeforeLogin, this.onAfterLogin)
- }
- bind (func, beforeFunc, afterFunc) {
- return function() {
- return beforeFuncn.call(this, arguments) ? func.call(this, arguments, afterFunc) : null
- }
- static get blackList() { return ['127.0.0.1']; }
- login (player, username, password, callback) {
- if (this.logged) return true
- connection.query('SELECT * FROM users WHERE username = ?, password = ?',[
- username.toString(),
- SHA256(password.toString()),
- ], function(err, result) {
- if (!err) { // ошибок нет, все окей
- // Запись с такой парой найдена?
- if (result.length) {
- this.logged = true
- return callback(username, this.logged, this.loginAttempts)
- } else {
- return callback(username, this.logged, ++this.loginAttempts)
- }
- } else {
- debug(`User::login, ошибка запроса к БД, причина: ${err}`)
- }
- })
- }
- beforeLogin (player) {
- return this.loginAttempts > this.maxLoginAttempts || User.blackList.indexOf(player.ip) >= 0 ? false : true
- }
- afterLogin (username, status, count) {
- if (status) {
- debug(`Пользовател ${username} авторизовался! ${count ? 'Его количество попыток входа = ' + count : ''}`)
- this.loginAttempts = 0
- } else {
- debug(`Ошибка авторизации. Количество попыток входа с логином ${username} = ${count.toString()}`)
- }
- return status
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement