-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
27 changed files
with
1,559 additions
and
18 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//Incluindo o package mongoose | ||
const mongoose = require('mongoose'); | ||
|
||
//Realizando a configuração padrão para conexão ao banco de dados | ||
mongoose.Promise = global.Promise; | ||
mongoose.connect("mongodb://localhost/teste", { | ||
useNewUrlParser: true | ||
}).then(()=>{ | ||
console.log("Conectado ao banco de dados com sucesso!"); | ||
}).catch((err) => { | ||
console.log("Houve um erro: " + err); | ||
}); | ||
|
||
//Criando o Schema de usuario do calendar | ||
const usuarioSchema = mongoose.Schema({ | ||
name:{ | ||
type: String, | ||
require: true, | ||
}, | ||
surname:{ | ||
type: String, | ||
require: true, | ||
}, | ||
email:{ | ||
type: String, | ||
require: true, | ||
}, | ||
password:{ | ||
type: String, | ||
require: true, | ||
} | ||
}); | ||
|
||
//Criando uma coleçao para o meus usuarios | ||
mongoose.model('users', usuarioSchema); | ||
|
||
//Adicionando um novo usuário a esse banco de dados | ||
const gabriel = mongoose.model('users'); | ||
|
||
new gabriel({ | ||
name: "Gabriel", | ||
surname: "Fachini", | ||
email: "[email protected]", | ||
password: "12345" | ||
}).save().then(() => { | ||
console.log("Usuário cadastrado com sucesso"); | ||
}).catch((err)=>{ | ||
console.log("Houve um erro no cadastro do usuario: " + err); | ||
}) | ||
|
||
//Para acessar no banco de dados realize os seguintes comandos | ||
/* | ||
show dbs - show databases | ||
use (baco de dados seu) | ||
show collections | ||
db.[coleção escolhida].find().pretty(); | ||
*/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const localStrategy = require('passport-local').Strategy | ||
const mongoose = require('mongoose') | ||
const bcrypt = require('bcryptjs') | ||
|
||
//Model de usuário | ||
require('../models/User') | ||
const User = mongoose.model('User'); | ||
|
||
//Configurando sistema de autenticação | ||
module.exports = function(passport){ | ||
|
||
passport.use(new localStrategy({usernameField: 'email', passwordField: 'password'}, (email, password, done) => { | ||
User.findOne({email: email}).then((user)=>{ | ||
if(!user){ | ||
console.log("essa conta não existe") | ||
return done(null, false, {message: "Essa conta não existe"}) | ||
} | ||
|
||
console.log("entrou aqui na verificação") | ||
console.log(password) | ||
console.log(user) | ||
console.log(user.password) | ||
console.log(user.email); | ||
bcrypt.compare(password, user.password, (erro, batem)=>{ | ||
|
||
if(batem){ | ||
console.log("logado com sucesso"); | ||
return done(null, user) | ||
} else { | ||
console.log("senha incorreta") | ||
return done(null, false, {message: "Senha incorreta"}) | ||
} | ||
|
||
}) | ||
}) | ||
})) | ||
|
||
//Passando o id do usuario para uma sessão | ||
passport.serializeUser((user, done)=>{ | ||
console.log("entrou aqui") | ||
done(null, user.id) | ||
}) | ||
|
||
passport.deserializeUser((id, done)=>{ | ||
console.log("entrou aqui") | ||
User.findById(id, (err, user) =>{ | ||
done(err, user) | ||
}) | ||
}) | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//Incluindo as modulos instaladas | ||
const express = require('express'); | ||
const handlebars = require('express-handlebars'); | ||
const bodyParser = require('body-parser'); | ||
const mongoose = require('mongoose'); | ||
const app = express(); //Inicializando a aplicação | ||
const routes = require('./routes/routes'); | ||
const path = require('path'); | ||
const session = require('express-session'); | ||
const flash = require('connect-flash'); | ||
const passport = require('passport') | ||
require('./config/auth')(passport) | ||
//Configurações | ||
//Sessão | ||
app.use(session({ | ||
secret: "calendarsecret", | ||
resave: true, | ||
saveUninitialized: true | ||
})) | ||
//Configurando o passport (necessariamente embaixo da sessão) | ||
app.use(passport.initialize()) | ||
app.use(passport.session()) | ||
|
||
//Configurando o flash (Necessariamente abaixo da config da sessão) | ||
app.use(flash()); | ||
|
||
//Middleware | ||
//Declarando variáveis globais | ||
/*app.use((req, res, next) => { | ||
res.locals.success_msg = req.flash("sucess_msg"); | ||
res.locals.error_msg = req.flash("error_msg"); | ||
next(); | ||
});*/ | ||
|
||
//Configurando o body-parser | ||
app.use(bodyParser.urlencoded({ extended: true})); | ||
app.use(bodyParser.json()); | ||
|
||
//Handle-bars | ||
app.engine('handlebars', handlebars({defaultLayout: 'main'})); | ||
app.set('view engine', 'handlebars'); | ||
|
||
//Mongoose | ||
mongoose.Promise = global.Promise; | ||
mongoose.connect("mongodb://localhost/calendar", { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true | ||
}).then(() => { | ||
console.log('Conectado ao mongo!') | ||
}).catch((err)=>{ | ||
console.log('Houve um erro ao conectar-se ao mongodb: '+ err); | ||
}) | ||
|
||
//Linkando a pasta public (onde estão os arquivos css e js) | ||
app.use(express.static(path.join(__dirname, '/public'))); | ||
|
||
//Rotas | ||
//Utilizando o arquivo de rotas que foi definido | ||
app.use('/', routes); // Daria para adicionar um prefixo a todas essas rotas contidas em 'routes' | ||
|
||
//Outros | ||
const PORT = 3000; | ||
app.listen(PORT, () => { | ||
console.log("Servidor rodando!"); | ||
}); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//Importando a biblioteca do mongoDB | ||
const mongoose = require('mongoose'); | ||
const Schema = mongoose.Schema; | ||
|
||
//Importando a biblioteca bcryptjs | ||
//const bcrypt = require('bcryptjs'); | ||
|
||
//Definindo o Schema do usuário | ||
const userSchema = new Schema({ | ||
name: { | ||
type: String, | ||
require: true, | ||
}, | ||
surname: { | ||
type: String, | ||
require: true, | ||
}, | ||
email: { | ||
type: String, | ||
unique: true, | ||
required: true, | ||
lowercase: true, | ||
}, | ||
password: { | ||
type: String, | ||
required: true, | ||
}, | ||
createdAt: { | ||
type: Date, | ||
default: Date.now, | ||
}, | ||
}); | ||
|
||
//Aplicando a encriptação da senha | ||
/*userSchema.pre('save', async function (next) { | ||
const hash = await bcrypt.hash(this.password, 10); | ||
this.password = hash; | ||
next(); | ||
});*/ | ||
|
||
//Definindo o user e qual será seu schema | ||
const User = mongoose.model('User', userSchema); | ||
|
||
//Exportando o usuário | ||
module.exports = User; |
Oops, something went wrong.