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. 13
      striker-ui-api/src/types/SessionData.d.ts
  5. 14
      striker-ui-api/src/types/User.d.ts

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

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

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

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

@ -1,4 +1,14 @@
type User = Express.User & { 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; name: string;
uuid: string; uuid: string;
}; }
}
}
export {};

Loading…
Cancel
Save