从 '../database.js' 导入连接 从“bcrypt”导入 bcrypt,{哈希} 从“快递”进口快递 从“jsonwebtoken”导入 jsonwebtoken 从 '../secret.js' 导入秘密
const authRouter = express.Router()
authRouter.post('/signup',async(req,res)=>
{
let {fullname,password,email,profile_pic}=req.body;
//问题出在这里 密码=等待 bcrypt.hash(密码,10)
connection.query("insert into users(fullname,email,password,profile_pic)values(?,?,?,?)",
[fullname,email,password,profile_pic],(err,result)=>{
if(err){
res.json({
error:err.message,
success:0
})
}
else if(result){
res.json({
email,
fullname,
profile_pic
})
}
})
})
authRouter.post('/login',(req,res)=>{
let{email,password}=req.body
connection.query("select *from users where email = ?",[email],async(err,result)=>{
if(err){
res.json({
err:err.message,
success:0
})
}
else{
if(result.length>0){
const ans = await bcrypt.compare(password, result[0].password)
if(ans){
delete result[0].password
const token = await jsonwebtoken.sign({...result[0]},secret,{expiresIn:'10h'})
res.json({
...result[0],token
})
}
else{
res.json({
error:"incorrect password",
success:0
})
}
}
else{
res.json({
error:"user not found",
success:0
})
}
}
})
})
导出默认 authRouter