(this: FastifyRequest)
| 72 | (req.accessDeniedMessage = { type: 'info', content }); |
| 73 | |
| 74 | async function getAuthedUser(this: FastifyRequest): Promise<AuthResult> { |
| 75 | const tokenCookie = this.cookies.jwt_access_token; |
| 76 | if (!tokenCookie) return { message: TOKEN_REQUIRED }; |
| 77 | |
| 78 | const unsignedToken = this.unsignCookie(tokenCookie); |
| 79 | if (!unsignedToken.valid) return { message: TOKEN_REQUIRED }; |
| 80 | |
| 81 | const jwtAccessToken = unsignedToken.value; |
| 82 | |
| 83 | try { |
| 84 | jwt.verify(jwtAccessToken, JWT_SECRET); |
| 85 | } catch { |
| 86 | return { message: TOKEN_INVALID }; |
| 87 | } |
| 88 | |
| 89 | const { accessToken } = jwt.decode(jwtAccessToken) as { |
| 90 | accessToken: Token; |
| 91 | }; |
| 92 | |
| 93 | if (isExpired(accessToken)) return { message: TOKEN_EXPIRED }; |
| 94 | // We're using token.userId since it's possible for the user record to be |
| 95 | // malformed and for prisma to throw while trying to find the user. |
| 96 | fastify.Sentry?.setUser({ |
| 97 | id: accessToken.userId |
| 98 | }); |
| 99 | |
| 100 | const user = await fastify.prisma.user.findUnique({ |
| 101 | where: { id: accessToken.userId } |
| 102 | }); |
| 103 | |
| 104 | return user ? { user } : { message: TOKEN_INVALID }; |
| 105 | } |
| 106 | |
| 107 | fastify.decorateRequest('getAuthedUser', getAuthedUser); |
| 108 |
nothing calls this directly
no test coverage detected