feat: add afterMe afterLogout and afterRefresh
This commit is contained in:
@@ -10,21 +10,25 @@ export type Arguments = {
|
||||
collection: Collection
|
||||
}
|
||||
|
||||
async function logout(args: Arguments): Promise<string> {
|
||||
async function logout(incomingArgs: Arguments): Promise<string> {
|
||||
let args = incomingArgs;
|
||||
const {
|
||||
res,
|
||||
req: {
|
||||
payload: {
|
||||
config,
|
||||
},
|
||||
user,
|
||||
},
|
||||
req,
|
||||
collection: {
|
||||
config: collectionConfig,
|
||||
},
|
||||
} = args;
|
||||
collection,
|
||||
} = incomingArgs;
|
||||
|
||||
if (!args.req.user) throw new APIError('No User', httpStatus.BAD_REQUEST);
|
||||
if (args.req.user.collection !== collectionConfig.slug) throw new APIError('Incorrect collection', httpStatus.FORBIDDEN);
|
||||
if (!user) throw new APIError('No User', httpStatus.BAD_REQUEST);
|
||||
if (user.collection !== collectionConfig.slug) throw new APIError('Incorrect collection', httpStatus.FORBIDDEN);
|
||||
|
||||
const cookieOptions = {
|
||||
path: '/',
|
||||
@@ -36,6 +40,14 @@ async function logout(args: Arguments): Promise<string> {
|
||||
|
||||
if (collectionConfig.auth.cookies.domain) cookieOptions.domain = collectionConfig.auth.cookies.domain;
|
||||
|
||||
await collection.config.hooks.afterLogout.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
|
||||
args = (await hook({
|
||||
req,
|
||||
})) || args;
|
||||
}, Promise.resolve());
|
||||
|
||||
res.clearCookie(`${config.cookiePrefix}-token`, cookieOptions);
|
||||
|
||||
return 'Logged out successfully.';
|
||||
|
||||
@@ -21,6 +21,9 @@ async function me({
|
||||
collection,
|
||||
}: Arguments): Promise<Result> {
|
||||
const extractJWT = getExtractJWT(req.payload.config);
|
||||
let response: Result = {
|
||||
user: null,
|
||||
};
|
||||
|
||||
if (req.user) {
|
||||
const user = { ...req.user };
|
||||
@@ -33,7 +36,7 @@ async function me({
|
||||
|
||||
delete user.collection;
|
||||
|
||||
const response: Result = {
|
||||
response = {
|
||||
user,
|
||||
collection: req.user.collection,
|
||||
};
|
||||
@@ -45,13 +48,22 @@ async function me({
|
||||
const decoded = jwt.decode(token) as jwt.JwtPayload;
|
||||
if (decoded) response.exp = decoded.exp;
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
return {
|
||||
user: null,
|
||||
};
|
||||
// /////////////////////////////////////
|
||||
// After Me - Collection
|
||||
// /////////////////////////////////////
|
||||
|
||||
await collection.config.hooks.afterMe.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
|
||||
response = await hook({
|
||||
req,
|
||||
response,
|
||||
}) || response;
|
||||
}, Promise.resolve());
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
export default me;
|
||||
|
||||
@@ -61,6 +61,7 @@ async function refresh(incomingArgs: Arguments): Promise<Result> {
|
||||
delete payload.iat;
|
||||
delete payload.exp;
|
||||
const refreshedToken = jwt.sign(payload, secret, opts);
|
||||
const exp = (jwt.decode(refreshedToken) as Record<string, unknown>).exp as number;
|
||||
|
||||
if (args.res) {
|
||||
const cookieOptions = {
|
||||
@@ -77,13 +78,27 @@ async function refresh(incomingArgs: Arguments): Promise<Result> {
|
||||
args.res.cookie(`${config.cookiePrefix}-token`, refreshedToken, cookieOptions);
|
||||
}
|
||||
|
||||
// /////////////////////////////////////
|
||||
// After Refresh - Collection
|
||||
// /////////////////////////////////////
|
||||
|
||||
await collectionConfig.hooks.afterRefresh.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
|
||||
args = (await hook({
|
||||
req: args.req,
|
||||
exp,
|
||||
token: refreshedToken,
|
||||
})) || args;
|
||||
}, Promise.resolve());
|
||||
|
||||
// /////////////////////////////////////
|
||||
// Return results
|
||||
// /////////////////////////////////////
|
||||
|
||||
return {
|
||||
refreshedToken,
|
||||
exp: (jwt.decode(refreshedToken) as Record<string, unknown>).exp as number,
|
||||
exp,
|
||||
user: payload,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user