mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-11 03:24:16 +00:00
added useAuth
middleware
This commit is contained in:
parent
bd14963fa9
commit
b553e69a17
@ -0,0 +1,34 @@
|
|||||||
|
import jwt from "jsonwebtoken"
|
||||||
|
|
||||||
|
export default async function (req, res, next) {
|
||||||
|
// extract authentification header
|
||||||
|
let auth = req.headers.authorization
|
||||||
|
|
||||||
|
if (!auth) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
auth = auth.replace("Bearer ", "")
|
||||||
|
|
||||||
|
// check if authentification is valid
|
||||||
|
const validation = await comty.rest.session.validSession(auth).catch((error) => {
|
||||||
|
return {
|
||||||
|
valid: false,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!validation.valid) {
|
||||||
|
return res.status(401).json({ error: "Unauthorized" })
|
||||||
|
}
|
||||||
|
|
||||||
|
// decode authentification header
|
||||||
|
auth = jwt.decode(auth)
|
||||||
|
|
||||||
|
if (!auth) {
|
||||||
|
return res.status(401).json({ error: "Unauthorized" })
|
||||||
|
}
|
||||||
|
|
||||||
|
req.session = auth
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user