@qelos/auth
Version:
Express Passport authentication service
35 lines (31 loc) • 1.05 kB
text/typescript
import { Response } from "express";
import { AuthRequest } from "../../types";
import { deleteToken, getCookieTokenName, getCookieTokenValue } from '../services/users';
import {getRequestHost} from '../services/req-host';
const { setCookie } = require("../services/tokens");
function removeToken(
tenant: string,
userId: string,
authType: string,
token: string
) {
deleteToken(tenant, userId, authType, token, authType === "oauth").catch(
Promise.resolve
);
}
export async function logout(req: AuthRequest, res: Response) {
const tenant = (req.headers.tenant = (req.headers.tenant as string) || "0");
const userId = req.userPayload.sub;
const cookieToken = getCookieTokenValue(req);
let token, authType;
if (cookieToken) {
token = cookieToken;
authType = "cookie";
setCookie(res, getCookieTokenName(tenant), "", -1, getRequestHost(req));
} else {
token = req.headers.authorization?.split(" ")[1] ?? "";
authType = "oauth";
}
removeToken(tenant, userId, authType, token);
res.status(200).end();
}