-
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
1 parent
bcd419c
commit e3009a0
Showing
3 changed files
with
83 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import Method from '../../../enum/method'; | ||
import Status from '../../../enum/status'; | ||
|
||
import { Request, Response } from '../../../util/handler'; | ||
import { User } from '../../../interfaces/user'; | ||
|
||
import { verify } from 'jsonwebtoken'; | ||
|
||
import Database from '../../../util/database'; | ||
|
||
interface ZleedJWT { | ||
id: bigint; | ||
email: string; | ||
username: string; | ||
iat: number; | ||
exp: number; | ||
} | ||
|
||
export default function handler(req: Request, res: Response<User>) { | ||
const jwtToken = req.getHeader('Authorization'); | ||
|
||
if (!jwtToken) { | ||
return res.error(Status.UNAUTHORIZED, 'error.auth.noToken'); | ||
} | ||
|
||
const tokenSplit = jwtToken.split(' '); | ||
|
||
if ( | ||
!(tokenSplit.length > 1) || | ||
jwtToken.split(' ')[0].toLowerCase() !== 'bearer' | ||
) { | ||
return res.error(Status.UNAUTHORIZED, 'error.auth.invalidToken'); | ||
} | ||
|
||
const token = tokenSplit[1]; | ||
|
||
verify(token, process.env.JWT_SECRET!, async (err, decoded) => { | ||
if (err) { | ||
return res.error(Status.UNAUTHORIZED, 'error.auth.invalidToken'); | ||
} | ||
|
||
const data = decoded as ZleedJWT; | ||
|
||
data.id = BigInt(data.id); | ||
|
||
if (req.method === Method.GET) { | ||
const user = await Database.getUser(data.id); | ||
|
||
res.status(Status.OK).json({ | ||
id: user.id, | ||
username: user.username, | ||
displayName: user.displayName, | ||
email: user.email, | ||
avatar: user.avatar, | ||
banner: user.banner, | ||
badges: user.badges, | ||
connections: user.connections, | ||
streamTitle: user.streamTitle, | ||
streamKeys: user.streamKeys, | ||
lastLive: user.lastLive, | ||
createdAt: user.createdAt, | ||
updatedAt: user.updatedAt | ||
}); | ||
} | ||
}); | ||
} |
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,15 @@ | ||
import { User } from '../models'; | ||
|
||
class Database { | ||
static async getUser( | ||
id: string | number | bigint | boolean | ||
): Promise<any | undefined> { | ||
const user = await User.findOne({ id: BigInt(id) }); | ||
|
||
if (!user) return undefined; | ||
|
||
return user; | ||
} | ||
} | ||
|
||
export default Database; |
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