-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthRouth.js
54 lines (42 loc) · 1.11 KB
/
authRouth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const express = require('express')
const authRouther = express.Router()
const passport = require('passport')
const user = require('./schema')
const bcrypt = require('bcrypt')
authRouther.post('/login',(req,res)=>{
user.find({username:req.body.username})
.then((data)=>{
const result = data.find(e=> bcrypt.compare(req.body.password,e.password))
res.send(result)
})
})
authRouther.get('/home',(req,res)=>{
if(req.user){
res.send(200)
}else{
res.send(404)
}
})
authRouther.get('/login',(req,res)=>{
res.send('You\'re in the login page')
})
authRouther.post('/register',(req,res)=>{
// using the user model to create new user in the mongodb database
let password = req.body.password;
bcrypt.hash(password,10).then( (hash)=> {
user.create({
username:req.body.username,
email:req.body.email,
password:hash,
age:req.body.age
})
})
.then(data=> {
res.send(data)
})
.catch(err=> {
res.sendStatus(406,err.message)
console.log(err.message)
})
})
module.exports = authRouther