Rename authenticate to login, add logout

This commit is contained in:
rijkvanzanten
2020-07-07 13:43:04 -04:00
parent e02709db93
commit 514c86822b
2 changed files with 30 additions and 16 deletions

View File

@@ -20,7 +20,7 @@ const loginSchema = Joi.object({
});
router.post(
'/authenticate',
'/login',
asyncHandler(async (req, res) => {
const { error } = loginSchema.validate(req.body);
if (error) throw new InvalidPayloadException(error.message);
@@ -32,13 +32,7 @@ router.post(
const ip = req.ip;
const userAgent = req.get('user-agent');
const {
accessToken,
refreshToken,
expires,
id,
refreshTokenExpiration,
} = await AuthService.authenticate({
const { accessToken, refreshToken, expires, id } = await AuthService.authenticate({
ip,
userAgent,
email,
@@ -82,19 +76,18 @@ router.post(
cookieParser(),
asyncHandler(async (req, res) => {
const currentRefreshToken = req.body.refresh_token || req.cookies.directus_refresh_token;
if (!currentRefreshToken)
if (!currentRefreshToken) {
throw new InvalidPayloadException(
`"refresh_token" is required in either the JSON payload or Cookie`
);
}
const mode: 'json' | 'cookie' = req.body.mode || req.body.refresh_token ? 'json' : 'cookie';
const {
accessToken,
refreshToken,
expires,
refreshTokenExpiration,
} = await AuthService.refresh(currentRefreshToken);
const { accessToken, refreshToken, expires } = await AuthService.refresh(
currentRefreshToken
);
const payload = {
data: { access_token: accessToken, expires },
@@ -119,6 +112,24 @@ router.post(
})
);
router.post(
'/logout',
cookieParser(),
asyncHandler(async (req, res) => {
const currentRefreshToken = req.body.refresh_token || req.cookies.directus_refresh_token;
if (!currentRefreshToken) {
throw new InvalidPayloadException(
`"refresh_token" is required in either the JSON payload or Cookie`
);
}
await AuthService.logout(currentRefreshToken);
res.status(200).end();
})
);
router.use(
'/sso',
session({ secret: process.env.SECRET, saveUninitialized: false, resave: false })

View File

@@ -65,7 +65,6 @@ export const authenticate = async ({ email, password, ip, userAgent }: Authentic
refreshToken,
expires: ms(process.env.ACCESS_TOKEN_TTL) / 1000,
id: user.id,
refreshTokenExpiration,
};
};
@@ -95,3 +94,7 @@ export const refresh = async (refreshToken: string) => {
return await authenticate({ email: record.email, ip: record.ip, userAgent: record.user_agent });
};
export const logout = async (refreshToken: string) => {
await database.delete().from('directus_sessions').where({ token: refreshToken });
};