Package breakdown - initial commit 1.0.0
This commit is contained in:
23
lib/passport/index.ts
Normal file
23
lib/passport/index.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import passport from 'koa-passport';
|
||||
import { Types } from 'mongoose';
|
||||
|
||||
import { AuthDocument } from '@mifi/services-common/lib/db/schema/auth';
|
||||
import { readOneByRecord } from '@mifi/services-common/lib/db/dao/readOneByRecord';
|
||||
import { readOneById } from '@mifi/services-common/lib/db/dao/readOneById';
|
||||
|
||||
import LocalStrategy from './strategies/local';
|
||||
import JwtStrategy from './strategies/jwt';
|
||||
|
||||
passport.use(LocalStrategy);
|
||||
passport.use(JwtStrategy);
|
||||
|
||||
passport.serializeUser((user, done) => {
|
||||
done(null, (user as AuthDocument).record || (user as AuthDocument).id);
|
||||
});
|
||||
|
||||
passport.deserializeUser(async (id, done) => {
|
||||
const user = await readOneByRecord(<Types.ObjectId>id).catch(async () => await readOneById(<Types.ObjectId>id));
|
||||
done(user ? null : 'user not found', user);
|
||||
});
|
||||
|
||||
export default passport;
|
||||
Reference in New Issue
Block a user