const util = require(
'util'
)
const crypto = require(
'crypto'
)
const express = require(
'express'
)
const bodyParser = require(
'body-parser'
)
const repo = require(
'./repository'
)
const app = express()
const scrypt = util.promisify(crypto.scrypt)
const port = process.env.PORT || 3000
app.use(bodyParser.urlencoded({ extended:
true
}))
app.get(
'/signin'
, (req, res) => {
res.send(`
<div>
<form method=
'POST'
>
<div>
<div>
<label id=
'email'
>Username</label>
</div>
<input type=
'text'
name=
'email'
placeholder=
'Email'
for
=
'email'
>
</div>
<div>
<div>
<label id=
'password'
>Password</label>
</div>
<input type=
'password'
name=
'password'
placeholder=
'Password'
for
=
'password'
>
</div>
<div>
<button>Sign In</button>
</div>
</form>
</div>
`)
})
app.post(
'/signin'
, async (req, res) => {
const { email, password } = req.body
const user = await repo.findBy({ email })
console.log(user)
if
(!user) {
return
res.send(
'User Not Exist'
)
}
const [hashed, salt] = user.password.split(
'.'
)
const hashedBuff = await scrypt(password, salt, 64)
console.log(hashed)
console.log(hashedBuff.toString(
'hex'
))
const isValid = hashed === hashedBuff.toString(
'hex'
)
if
(isValid) {
return
res.send(
'Sign In successfully'
)
}
return
res.send(
'Invalid Password'
)
})
app.listen(port, () => {
console.log(`Server start on port ${port}`)
})