-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
delete like endpoint, return liked posts
- Loading branch information
Showing
12 changed files
with
87 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import { Post } from '@codersquare/shared'; | ||
|
||
export interface PostDao { | ||
listPosts(): Promise<Post[]>; | ||
listPosts(userId?: string): Promise<Post[]>; | ||
createPost(post: Post): Promise<void>; | ||
getPost(id: string): Promise<Post | undefined>; | ||
getPost(id: string, userId?: string): Promise<Post | undefined>; | ||
deletePost(id: string): Promise<void>; | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,31 @@ | ||
import { verifyJwt } from '../auth'; | ||
import { db } from '../datastore'; | ||
import { ExpressHandler } from '../types'; | ||
import { ExpressHandler, JwtObject } from '../types'; | ||
|
||
export const authMiddleware: ExpressHandler<any, any> = async (req, res, next) => { | ||
export const jwtParseMiddleware: ExpressHandler<any, any> = async (req, res, next) => { | ||
const token = req.headers.authorization?.split(' ')[1]; | ||
if (!token) { | ||
return res.sendStatus(401); | ||
return next(); | ||
} | ||
|
||
let payload: JwtObject; | ||
try { | ||
const payload = verifyJwt(token); | ||
const user = await db.getUserById(payload.userId); | ||
if (!user) { | ||
return res.status(401).send({ error: 'User not found' }); | ||
} | ||
res.locals.userId = user.id; | ||
next(); | ||
payload = verifyJwt(token); | ||
} catch { | ||
return res.status(401).send({ error: 'Bad token' }); | ||
} | ||
|
||
const user = await db.getUserById(payload.userId); | ||
if (!user) { | ||
return res.status(401).send({ error: 'User not found' }); | ||
} | ||
res.locals.userId = user.id; | ||
return next(); | ||
}; | ||
|
||
export const enforceJwtMiddleware: ExpressHandler<any, any> = async (_, res, next) => { | ||
if (!res.locals.userId) { | ||
return res.sendStatus(401); | ||
} | ||
return next(); | ||
}; |
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
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