mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 18:44:16 +00:00
added injectedAuth lib
This commit is contained in:
parent
0f0156df84
commit
31e27f83aa
29
packages/server/lib/injectedAuth/index.js
Normal file
29
packages/server/lib/injectedAuth/index.js
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import AuthToken from "../../classes/AuthToken"
|
||||||
|
import { User } from "../../db_models"
|
||||||
|
|
||||||
|
export default async (obj, token) => {
|
||||||
|
const validation = await AuthToken.validate(token)
|
||||||
|
|
||||||
|
if (!validation.valid) {
|
||||||
|
if (validation.error) {
|
||||||
|
throw new Error(`Server error`)
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`Invalid token`)
|
||||||
|
}
|
||||||
|
|
||||||
|
let userData = await User.findById(validation.data.user_id).lean()
|
||||||
|
|
||||||
|
if (!userData) {
|
||||||
|
throw new Error(`User not found`)
|
||||||
|
}
|
||||||
|
|
||||||
|
userData._id = userData._id.toString()
|
||||||
|
|
||||||
|
obj.user = userData
|
||||||
|
obj.token = token
|
||||||
|
obj.session = validation.data
|
||||||
|
obj.user = userData
|
||||||
|
|
||||||
|
return obj
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user