| 4 | const PUSH_SERVICE_KEY = process.env.PUSH_SERVICE_KEY; |
| 5 | |
| 6 | export default async function handler( |
| 7 | request: NextApiRequest, |
| 8 | response: NextApiResponse |
| 9 | ) { |
| 10 | try { |
| 11 | const { push_token } = JSON.parse(request.body); |
| 12 | if (!push_token) { |
| 13 | throw 'missing push token'; |
| 14 | } |
| 15 | if (!PUSH_SERVICE_KEY) { |
| 16 | throw 'missing push service key'; |
| 17 | } |
| 18 | if (push_token !== PUSH_SERVICE_KEY) { |
| 19 | throw 'invalid push token'; |
| 20 | } |
| 21 | |
| 22 | const token = await Session.token(request); |
| 23 | if (!token) { |
| 24 | throw 'invalid authorization token'; |
| 25 | } |
| 26 | |
| 27 | if (!token.id) { |
| 28 | throw 'missing user id on authorization token'; |
| 29 | } |
| 30 | |
| 31 | return response.send(token.id); |
| 32 | } catch (error: any) { |
| 33 | console.error({ error }); |
| 34 | return response.status(error.status || 500).json({}); |
| 35 | } |
| 36 | } |