Last active
September 20, 2020 04:43
-
-
Save disaada/d42aeda7e4c52b8bb1d1e7b9997a7833 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
app.post('/signin', (req, res) => { | |
/* step 1 : mengambil data email dan password yang telah diisi oleh user */ | |
const { email, password } = req.body | |
/* step 2 : mengambil data user di db berdasarkan email yang dimasukkan user */ | |
Model.user.findOne({ where: { email } }) | |
.then((data) => { | |
/* data dari db didapat, bandingkan password yg diinput dengan yg ada di db */ | |
if (data && (bcrypt.compareSync(password, data.password))) { | |
/* buat token menggunakan jwt.sign(data, 'secretkey', waktu_expired) */ | |
const token = jwt.sign({ data }, 'secret', { expiresIn: '1h' }) | |
/* mengirimkan respon ke client berupa pesan */ | |
res.json({ data, token, message: 'signin success!' }) | |
} /* kalau perbandingan password tidak cocok, kirim pesan peringatan */ | |
else res.json({ message: 'invalid email or password!' }) | |
}) | |
.catch((e) => { | |
res.json(e) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment