fix(striker-ui-api): add de/serialize user functions to passport

main
Tsu-ba-me 2 years ago
parent de2e8e45e1
commit eb2c66de9f
  1. 42
      striker-ui-api/src/passport.ts
  2. 4
      striker-ui-api/src/types/User.d.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;

@ -1,4 +1,4 @@
interface User extends Express.User {
type User = Express.User & {
name: string;
uuid: string;
}
};

Loading…
Cancel
Save