refactor(striker-ui-api): merge Express.User and SessionData declarations

main
Tsu-ba-me 2 years ago
parent 0fa614ca07
commit 448a956313
  1. 2
      striker-ui-api/src/lib/request_handlers/auth/login.ts
  2. 7
      striker-ui-api/src/passport.ts
  3. 14
      striker-ui-api/src/session.ts
  4. 15
      striker-ui-api/src/types/SessionData.d.ts
  5. 18
      striker-ui-api/src/types/User.d.ts

@ -9,7 +9,7 @@ export const login: RequestHandler<unknown, unknown, AuthLoginRequestBody> = (
const { user } = request;
if (user) {
const { name: userName } = user as User;
const { name: userName } = user;
stdout(`Successfully authenticated user [${userName}]`);
}

@ -1,3 +1,4 @@
import { Express } from 'express';
import passport from 'passport';
import { Strategy as LocalStrategy } from 'passport-local';
@ -72,7 +73,7 @@ passport.use(
return done(null, false);
}
const user: User = {
const user: Express.User = {
name: username,
uuid: userUuid,
};
@ -82,7 +83,7 @@ passport.use(
);
passport.serializeUser((user, done) => {
const { name, uuid } = user as User;
const { name, uuid } = user;
stdout(`Serialize user [${name}]`);
@ -115,7 +116,7 @@ passport.deserializeUser((id, done) => {
0: [userName],
} = rows;
const user: User = { name: userName, uuid };
const user: Express.User = { name: userName, uuid };
return done(null, user);
});

@ -1,7 +1,7 @@
import assert from 'assert';
import session, {
SessionData as BaseSessionData,
Store as BaseStore,
SessionData,
Store as BaseSessionStore,
} from 'express-session';
import {
@ -15,7 +15,7 @@ import { stdout, stdoutVar, uuidgen } from './lib/shell';
const DEFAULT_COOKIE_ORIGINAL_MAX_AGE = 1000 * 60 * 60;
export class SessionStore extends BaseStore {
export class SessionStore extends BaseSessionStore {
constructor(options = {}) {
super(options);
}
@ -41,7 +41,7 @@ export class SessionStore extends BaseStore {
public get(
sid: string,
done: (err: unknown, session?: BaseSessionData | null | undefined) => void,
done: (err: unknown, session?: SessionData | null | undefined) => void,
): void {
stdout(`Get session ${sid}`);
@ -92,14 +92,14 @@ export class SessionStore extends BaseStore {
public set(
sid: string,
session: BaseSessionData,
session: SessionData,
done?: ((err?: unknown) => void) | undefined,
): void {
stdout(`Set session ${sid}; session=${JSON.stringify(session, null, 2)}`);
const {
passport: { user: userUuid },
} = session as SessionData;
} = session;
try {
const localHostUuid = getLocalHostUUID();
@ -140,7 +140,7 @@ export class SessionStore extends BaseStore {
public touch(
sid: string,
session: BaseSessionData,
session: SessionData,
done?: ((err?: unknown) => void) | undefined,
): void {
stdout(

@ -1,3 +1,12 @@
type SessionData = import('express-session').SessionData & {
passport: { user: string };
};
declare module 'express-session' {
/**
* Extended with passport property.
*/
interface SessionData {
passport: { user: string };
returnTo?: string;
}
}
// Required to avoid overwritting the original express-session module.
export {};

@ -1,4 +1,14 @@
type User = Express.User & {
name: string;
uuid: string;
};
declare global {
namespace Express {
/**
* Extended Express.User object used by express-session and passport to
* identify which user owns a session.
*/
interface User {
name: string;
uuid: string;
}
}
}
export {};

Loading…
Cancel
Save