refactor: login

This commit is contained in:
monoid 2022-07-06 15:53:40 +09:00
parent 8d20c30d32
commit beafeb7ace

View File

@ -147,9 +147,11 @@ export const LogoutMiddleware = (ctx: Koa.Context, next: Koa.Next) => {
const setting = get_setting()
ctx.cookies.set(accessTokenName, null);
ctx.cookies.set(refreshTokenName, null);
ctx.body = { ok: true,
ctx.body = {
ok: true,
username: "",
permission: setting.guest };
permission: setting.guest
};
return;
};
export const createUserMiddleWare = (userController: UserAccessor) =>
@ -166,15 +168,33 @@ export const createUserMiddleWare = (userController: UserAccessor) =>
};
const refreshTokenHandler = (cntr: UserAccessor) =>
async (ctx: Koa.Context, fail: Koa.Next, next: Koa.Next) => {
const payload = ctx.cookies.get(accessTokenName);
const accessPayload = ctx.cookies.get(accessTokenName);
const setting = get_setting();
const secretKey = setting.jwt_secretkey;
const checkRefreshAndUpdate = async () => {
const payload2 = ctx.cookies.get(refreshTokenName);
if (payload2 === undefined) return await fail(); // refresh token doesn't exist
else {
if (accessPayload == undefined) {
return await checkRefreshAndUpdate();
}
try {
const o = verify(payload2, secretKey);
const o = verify(accessPayload, secretKey);
if (isUserState(o)) {
ctx.state.user = o;
return await next();
} else {
console.error("invalid token detected");
throw new Error("token form invalid");
}
} catch (e) {
if (e instanceof TokenExpiredError) {
return await checkRefreshAndUpdate();
} else throw e;
}
async function checkRefreshAndUpdate() {
const refreshPayload = ctx.cookies.get(refreshTokenName);
if (refreshPayload === undefined) {
return await fail(); // refresh token doesn't exist
} else {
try {
const o = verify(refreshPayload, secretKey);
if (isRefreshToken(o)) {
const user = await cntr.findUser(o.username);
if (user === undefined) return await fail(); //already non-existence user
@ -199,28 +219,12 @@ const refreshTokenHandler = (cntr: UserAccessor) =>
}
return await next();
};
if (payload == undefined) {
return await checkRefreshAndUpdate();
}
try {
const o = verify(payload, secretKey);
if (isUserState(o)) {
ctx.state.user = o;
return await next();
} else {
console.error("invalid token detected");
throw new Error("token form invalid");
}
} catch (e) {
if (e instanceof TokenExpiredError) {
return await checkRefreshAndUpdate();
} else throw e;
}
};
export const createRefreshTokenMiddleware = (cntr: UserAccessor) =>
async (ctx: Koa.Context, next: Koa.Next) => {
const handler = refreshTokenHandler(cntr);
const fail = async () => {
await handler(ctx, fail, success);
async function fail() {
const user = ctx.state.user as PayloadInfo;
ctx.body = {
refresh: false,
@ -228,7 +232,7 @@ export const createRefreshTokenMiddleware = (cntr: UserAccessor) =>
};
ctx.type = "json";
};
const success = async () => {
async function success() {
const user = ctx.state.user as PayloadInfo;
ctx.body = {
...user,
@ -237,7 +241,6 @@ export const createRefreshTokenMiddleware = (cntr: UserAccessor) =>
};
ctx.type = "json";
};
await handler(ctx, fail, success);
};
export const resetPasswordMiddleware = (cntr: UserAccessor) =>
async (ctx: Koa.Context, next: Koa.Next) => {