diff --git a/striker-ui-api/src/passport.ts b/striker-ui-api/src/passport.ts index 09a85481..ff08b434 100644 --- a/striker-ui-api/src/passport.ts +++ b/striker-ui-api/src/passport.ts @@ -2,12 +2,13 @@ import passport from 'passport'; import { Strategy as LocalStrategy } from 'passport-local'; import { dbQuery, sub } from './lib/accessModule'; +import { sanitize } from './lib/sanitize'; import { stdout } from './lib/shell'; passport.use( 'login', new LocalStrategy((username, password, done) => { - stdout(`Attempting passport local strategy [login] for user [${username}]`); + stdout(`Attempting passport local strategy "login" for user [${username}]`); let rows: [ userUuid: string, @@ -80,4 +81,43 @@ passport.use( }), ); +passport.serializeUser((user, done) => { + const { name, uuid } = user as User; + + stdout(`Serialize user [${name}]`); + + return done(null, uuid); +}); + +passport.deserializeUser((id, done) => { + const uuid = sanitize(id, 'string', { modifierType: 'sql' }); + + stdout(`Deserialize user identified by ${uuid}`); + + let rows: [userName: string][]; + + try { + rows = dbQuery( + `SELECT user_name + FROM users + WHERE user_algorithm != 'DELETED' + AND user_uuid = '${uuid}';`, + ).stdout; + } catch (error) { + return done(error); + } + + if (!rows.length) { + return done(null, false); + } + + const { + 0: [userName], + } = rows; + + const user: User = { name: userName, uuid }; + + return done(null, user); +}); + export default passport; diff --git a/striker-ui-api/src/types/User.d.ts b/striker-ui-api/src/types/User.d.ts index a0a3a79c..9c70c3bc 100644 --- a/striker-ui-api/src/types/User.d.ts +++ b/striker-ui-api/src/types/User.d.ts @@ -1,4 +1,4 @@ -interface User extends Express.User { +type User = Express.User & { name: string; uuid: string; -} +};