-
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.
- Loading branch information
Showing
6 changed files
with
89 additions
and
65 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
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
Binary file not shown.
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,21 @@ | ||
const express = require("express") | ||
const db = require('../db') | ||
const router = express.Router() | ||
|
||
router.get('/get-posts', async (req, res) => { | ||
const posts = await db.getPosts() | ||
res.status(200).send(posts) | ||
}) | ||
|
||
router.post('/submit', async (req, res) => { | ||
const {email, message} = req.body; | ||
try { | ||
const result = await db.createPost({user_email: email, content: message}); | ||
res.status(200).send(result); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).send('Error creating post'); | ||
} | ||
}) | ||
|
||
module.exports = router |
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,42 @@ | ||
const express = require("express") | ||
const bcrypt = require('bcrypt') | ||
const db = require('../db') | ||
const router = express.Router() | ||
|
||
router.get('/get-user-info', (req, res) => { | ||
if (req.session.email) { | ||
res.status(200).send({email: req.session.email, username: req.session.username}) | ||
} else { | ||
res.status(401).json({ message: "用户未登录" }); | ||
} | ||
}) | ||
|
||
router.post('/register', async (req, res) => { | ||
const {email, username, password} = req.body | ||
const hashedPassword = await bcrypt.hash(password, 10) | ||
|
||
const result = await db.createUser({email, username, password: hashedPassword}) | ||
res.status(200).send(result) | ||
console.log("注册成功") | ||
}) | ||
|
||
router.post('/login', async (req, res) => { | ||
const {email, password} = req.body | ||
const user = await db.findUserByEmail(email) | ||
|
||
if (user && await bcrypt.compare(password, user.password)) { | ||
req.session.email = user.email | ||
req.session.username = user.username | ||
console.log(`${user.username}登录成功`) | ||
res.status(200).send({message: `${user.username}登录成功`}) | ||
} else { | ||
res.status(401).send({error: "Email or password is incorrect"}) | ||
} | ||
}) | ||
|
||
router.get('/logout', (req, res) => { | ||
req.session = null | ||
res.send({ message: '已成功注销' }) | ||
}) | ||
|
||
module.exports = router |
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