This commit is contained in:
22
lib/passport/strategies/jwt.ts
Normal file
22
lib/passport/strategies/jwt.ts
Normal 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);
|
||||
});
|
||||
18
lib/passport/strategies/local.ts
Normal file
18
lib/passport/strategies/local.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import passport from 'koa-passport';
|
||||
// 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);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user