-
Notifications
You must be signed in to change notification settings - Fork 0
/
workflow.ts
53 lines (46 loc) · 1.29 KB
/
workflow.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { Request, Response, NextFunction } from 'express'
import Joi from 'joi'
import { fullMessagesResponse } from '../utils/joiSchemas'
import { MESSAGE_TYPE } from '../utils/enums'
import runner from './runner'
import { ErrorBuilder } from '../utils/ErrorBuilder'
import { customTFunction } from '../utils/translations'
/**
* Logout from everywhere endpoint request schema - empty
*/
export const requestSchema = Joi.object({
body: Joi.object(),
query: Joi.object(),
params: Joi.object()
})
/**
* Logout from everywhere endpoint response schema - full message
*/
export const responseSchema = fullMessagesResponse
/**
* Logout from everywhere endpoint
* Usage: `router.post('/logout-everywhere', ApiAuth.guard(), schemaMiddleware(LogoutEverywhere.requestSchema), LogoutEverywhere.endpoint)`
* @param req
* @param res
* @param next
*/
export async function workflow(req: Request, res: Response, next: NextFunction) {
try {
const authHeader = req.headers.authorization
const t = req.t ?? customTFunction
if (!authHeader) {
throw new ErrorBuilder(401, t('Unauthorized'))
}
await runner(authHeader)
return res.json({
messages: [
{
type: MESSAGE_TYPE.SUCCESS,
message: t('You were successfully logged out')
}
]
})
} catch (err) {
return next(err)
}
}