This commit is contained in:
2023-05-02 01:51:09 -04:00
parent fda0e160d4
commit 14fe45fc9c
2 changed files with 14 additions and 14 deletions

View File

@@ -1,16 +1,16 @@
import passport from 'koa-passport'; import passport from 'koa-passport';
import Users from 'grow-db/lib/models/users'; // import Users from 'grow-db/lib/models/users';
import { User } from 'grow-db/lib/schemas/user'; // import { User } from 'grow-db/lib/schemas/user';
passport.serializeUser((user: User, done) => { done(null, user._id); }); // passport.serializeUser((user: User, done) => { done(null, user._id); });
passport.deserializeUser(async (id, done) => { // passport.deserializeUser(async (id, done) => {
const user = await Users.findById(id); // const user = await Users.findById(id);
if (user) { // if (user) {
done(null, user); // done(null, user);
} // }
done('user not found', null); // done('user not found', null);
}); // });

View File

@@ -1,12 +1,12 @@
import crypto from 'crypto'; import { pbkdf2Sync, randomBytes } from 'crypto';
export const encrypt = (password: string) => { export const encrypt = (password: string) => {
const salt = crypto.randomBytes(16).toString('hex'); const salt = randomBytes(16).toString('hex');
const hash = crypto.pbkdf2Sync(password, salt, 10000, 512, 'sha512').toString('hex'); const hash = pbkdf2Sync(password, salt, 10000, 512, 'sha512').toString('hex');
return `${salt}:${hash}`; return `${salt}:${hash}`;
}; };
export const verify = (test: string, secret: string) => { export const verify = (test: string, secret: string) => {
const [salt, hash] = secret.split(':'); const [salt, hash] = secret.split(':');
return crypto.pbkdf2Sync(test, salt, 10000, 512, 'sha512').toString('hex') === hash; return pbkdf2Sync(test, salt, 10000, 512, 'sha512').toString('hex') === hash;
}; };