Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Add logout endpoint, invalidate session
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Sep 3, 2019
1 parent 5b484b8 commit da895a6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "staart-manager",
"version": "1.1.22",
"version": "1.1.23",
"main": "index.js",
"repository": "[email protected]:AnandChowdhary/staart.git",
"author": "Anand Chowdhary <[email protected]>",
Expand Down Expand Up @@ -147,5 +147,5 @@
"setup"
],
"snyk": true,
"staart-version": "1.1.22"
"staart-version": "1.1.23"
}
11 changes: 10 additions & 1 deletion src/controllers/v1/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
approveLocation,
verifyEmail,
register,
login2FA
login2FA,
invalidateRefreshToken
} from "../../rest/auth";
import { verifyToken, LoginResponse } from "../../helpers/jwt";
import {
Expand Down Expand Up @@ -181,6 +182,14 @@ export class AuthController {
res.json(await validateRefreshToken(token, res.locals));
}

@Post("logout")
async postLogout(req: Request, res: Response) {
const token =
req.body.token || (req.get("Authorization") || "").replace("Bearer ", "");
joiValidate({ token: Joi.string().required() }, { token });
res.json(await invalidateRefreshToken(token, res.locals));
}

@Post("reset-password/request")
@Middleware(
validator(
Expand Down
13 changes: 13 additions & 0 deletions src/crud/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,19 @@ export const updateSessionByJwt = async (
);
};

/**
* Update a user's details
*/
export const deleteSessionByJwt = async (
userId: string,
sessionJwt: string
) => {
return await query(
`DELETE FROM ${tableName("sessions")} WHERE jwtToken = ? AND userId = ?`,
[sessionJwt, userId]
);
};

/**
* Invalidate a session
*/
Expand Down
9 changes: 8 additions & 1 deletion src/rest/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
addApprovedLocation,
getUserBackupCode,
updateBackupCode,
checkUsernameAvailability
checkUsernameAvailability,
deleteSessionByJwt
} from "../crud/user";
import { InsertResult } from "../interfaces/mysql";
import {
Expand Down Expand Up @@ -64,6 +65,12 @@ export const validateRefreshToken = async (token: string, locals: Locals) => {
return await postLoginTokens(user, locals, token);
};

export const invalidateRefreshToken = async (token: string, locals: Locals) => {
const data = <User>await verifyToken(token, Tokens.REFRESH);
if (!data.id) throw new Error(ErrorCode.USER_NOT_FOUND);
await deleteSessionByJwt(data.id, token);
};

export const login = async (
email: string,
password: string,
Expand Down

0 comments on commit da895a6

Please sign in to comment.