Reorganizing
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2023-05-03 11:12:59 -04:00
parent 27a78dd471
commit dc72cefece
23 changed files with 163 additions and 87 deletions

View File

@@ -0,0 +1,23 @@
import passport from 'koa-passport';
import Auth from '../../model/auth';
import { Auth as AuthRecord } from '../../db/schema/auth';
import LocalStrategy from './strategies/local';
import JwtStrategy from './strategies/jwt';
passport.serializeUser((user, done) => done(null, (user as AuthRecord).record));
passport.deserializeUser(async (id, done) => {
const user = await Auth.findOne({ record: id });
if (user) {
done(null, user);
}
done('user not found', null);
});
passport.use(LocalStrategy);
passport.use(JwtStrategy);
export default passport;

View File

@@ -0,0 +1,22 @@
// eslint-disable-next-line import/named
import { ExtractJwt, Strategy as JwtStrategy } from 'passport-jwt';
import Auth from '../../../model/auth';
import { getJwtSecret } from '../../../utils/jwt';
const opts = {
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: getJwtSecret(),
issuer: process.env.JWT_ISSUER,
audience: process.env.JWT_AUDIENCE,
};
export default new JwtStrategy(opts, async (jwt_payload, done) => {
const auth = await Auth.findOne({ record: jwt_payload.sub }).catch();
if (auth) {
return done(null, auth);
}
return done(null, false);
});

View File

@@ -0,0 +1,17 @@
// eslint-disable-next-line import/named
import { Strategy as LocalStrategy } from 'passport-local';
import Auth from '../../../model/auth';
export default new LocalStrategy(async (username: string, password: string, done: any) => {
const user = await Auth.findOne({
where: {
username,
},
}).catch();
if (user && user.authenticate(password)) {
done(null, user);
} else {
done(null, false);
}
});