diff options
| author | Wes Bos <wesbos@gmail.com> | 2018-01-30 12:41:01 -0500 |
|---|---|---|
| committer | Wes Bos <wesbos@gmail.com> | 2018-01-30 12:41:01 -0500 |
| commit | cd0b07b3adb36fb5ec098f9e511ab45d774fee7b (patch) | |
| tree | a203e5010219ccea1acc6bbd2c0a4bcfa0a78615 /backend/src/utils.js | |
| parent | 0a1648bf47490b312169c531aeb51ef4725e8d2e (diff) | |
Move to Prisma Backend
Diffstat (limited to 'backend/src/utils.js')
| -rw-r--r-- | backend/src/utils.js | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/backend/src/utils.js b/backend/src/utils.js new file mode 100644 index 0000000..35587db --- /dev/null +++ b/backend/src/utils.js @@ -0,0 +1,23 @@ +const jwt = require('jsonwebtoken'); + +function getUserId(ctx) { + const Authorization = ctx.request.get('Authorization'); + if (Authorization) { + const token = Authorization.replace('Bearer ', ''); + const { userId } = jwt.verify(token, process.env.APP_SECRET); + return userId; + } + + throw new AuthError(); +} + +class AuthError extends Error { + constructor() { + super('Not authorized'); + } +} + +module.exports = { + getUserId, + AuthError, +}; |
