express#CookieOptions TypeScript Examples
The following examples show how to use
express#CookieOptions.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: security.constants.ts From master-frontend-lemoncode with MIT License | 5 votes |
cookieOptions: CookieOptions = {
httpOnly: true,
secure: envConstants.isProduction,
}
Example #2
Source File: OAuthAdapter.ts From backstage with Apache License 2.0 | 5 votes |
private readonly baseCookieOptions: CookieOptions;
Example #3
Source File: authUtil.ts From flood with GNU General Public License v3.0 | 5 votes |
getCookieOptions = (): CookieOptions => ({
expires: new Date(Date.now() + EXPIRATION_SECONDS * 1000),
httpOnly: true,
sameSite: 'strict',
})
Example #4
Source File: login.ts From payload with MIT License | 4 votes |
async function login(this: Payload, incomingArgs: Arguments): Promise<Result> {
const { config, operations, secret } = this;
let args = incomingArgs;
// /////////////////////////////////////
// beforeOperation - Collection
// /////////////////////////////////////
await args.collection.config.hooks.beforeOperation.reduce(async (priorHook, hook) => {
await priorHook;
args = (await hook({
args,
operation: 'login',
})) || args;
}, Promise.resolve());
const {
collection: {
Model,
config: collectionConfig,
},
data,
req,
depth,
overrideAccess,
showHiddenFields,
} = args;
// /////////////////////////////////////
// Login
// /////////////////////////////////////
const { email: unsanitizedEmail, password } = data;
const email = unsanitizedEmail ? (unsanitizedEmail as string).toLowerCase() : null;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore Improper typing in library, additional args should be optional
const userDoc = await Model.findByUsername(email);
if (!userDoc || (args.collection.config.auth.verify && userDoc._verified === false)) {
throw new AuthenticationError();
}
if (userDoc && isLocked(userDoc.lockUntil)) {
throw new LockedAuth();
}
const authResult = await userDoc.authenticate(password);
const maxLoginAttemptsEnabled = args.collection.config.auth.maxLoginAttempts > 0;
if (!authResult.user) {
if (maxLoginAttemptsEnabled) await userDoc.incLoginAttempts();
throw new AuthenticationError();
}
if (maxLoginAttemptsEnabled) {
await operations.collections.auth.unlock({
collection: {
Model,
config: collectionConfig,
},
req,
data,
overrideAccess: true,
});
}
let user = userDoc.toJSON({ virtuals: true });
user = JSON.parse(JSON.stringify(user));
user = sanitizeInternalFields(user);
const fieldsToSign = collectionConfig.fields.reduce((signedFields, field: Field) => {
const result = {
...signedFields,
};
if (!fieldAffectsData(field) && fieldHasSubFields(field)) {
field.fields.forEach((subField) => {
if (fieldAffectsData(subField) && subField.saveToJWT) {
result[subField.name] = user[subField.name];
}
});
}
if (fieldAffectsData(field) && field.saveToJWT) {
result[field.name] = user[field.name];
}
return result;
}, {
email,
id: user.id,
collection: collectionConfig.slug,
});
const token = jwt.sign(
fieldsToSign,
secret,
{
expiresIn: collectionConfig.auth.tokenExpiration,
},
);
if (args.res) {
const cookieOptions: CookieOptions = {
path: '/',
httpOnly: true,
expires: getCookieExpiration(collectionConfig.auth.tokenExpiration),
secure: collectionConfig.auth.cookies.secure,
sameSite: collectionConfig.auth.cookies.sameSite,
domain: undefined,
};
if (collectionConfig.auth.cookies.domain) cookieOptions.domain = collectionConfig.auth.cookies.domain;
args.res.cookie(`${config.cookiePrefix}-token`, token, cookieOptions);
}
req.user = user;
// /////////////////////////////////////
// afterLogin - Collection
// /////////////////////////////////////
await collectionConfig.hooks.afterLogin.reduce(async (priorHook, hook) => {
await priorHook;
user = await hook({
doc: user,
req: args.req,
token,
}) || user;
}, Promise.resolve());
// /////////////////////////////////////
// afterRead - Fields
// /////////////////////////////////////
user = await afterRead({
depth,
doc: user,
entityConfig: collectionConfig,
overrideAccess,
req,
showHiddenFields,
});
// /////////////////////////////////////
// afterRead - Collection
// /////////////////////////////////////
await collectionConfig.hooks.afterRead.reduce(async (priorHook, hook) => {
await priorHook;
user = await hook({
req,
doc: user,
}) || user;
}, Promise.resolve());
// /////////////////////////////////////
// Return results
// /////////////////////////////////////
return {
token,
user,
exp: (jwt.decode(token) as jwt.JwtPayload).exp,
};
}