Skip to content

Commit

Permalink
docs: fix local-passport
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Nov 1, 2024
1 parent b03b742 commit 9f62383
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 34 deletions.
23 changes: 14 additions & 9 deletions site/docs/extensions/passport.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ Midway 对此进行了改造,通过 `@CustomStrategy` 和 `PassportStrategy`
// src/strategy/local.strategy.ts

import { CustomStrategy, PassportStrategy } from '@midwayjs/passport';
import { Strategy } from 'passport-local';
import { Strategy, IStrategyOptions } from 'passport-local';
import { Repository } from 'typeorm';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { UserEntity } from './user';
Expand All @@ -141,19 +141,24 @@ export class LocalStrategy extends PassportStrategy(Strategy) {
// 策略的验证
async validate(username, password) {
const user = await this.userModel.findOneBy({ username });
if (await bcrypt.compare(password, user.password)) {
throw new Error('error password ' + username);
if (!user) {
throw new Error('用户不存在 ' + username);
}
if (!await bcrypt.compare(password, user.password)) {
throw new Error('密码错误 ' + username);
}

return {
username,
password,
};
return user;
}

// 当前策略的构造器参数
getStrategyOptions(): any {
return {};
getStrategyOptions(): IStrategyOptions {
return {
usernameField: 'username',
passwordField: 'password',
passReqToCallback: true,
session: false
};
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,21 @@ We use `passport-local` to introduce how to use the Passport policy in Midway. T

```typescript
passport.use(
//Initialize a strategy
new LocalStrategy({
usernameField: 'username',
passwordField: 'password',
passReqToCallback: true,
session: false
},
function verify(username, password, done) {
User.findOne({ username: username }, function (err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
if (!user.verifyPassword(password)) { return done(null, false); }
return done(null, user);
});
}
)
//Initialize a strategy
new LocalStrategy({
usernameField: 'username',
passwordField: 'password',
passReqToCallback: true,
session: false
},
function verify(username, password, done) {
User.findOne({ username: username }, function (err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
if (!user.verifyPassword(password)) { return done(null, false); }
return done(null, user);
});
})
);
```

Expand All @@ -139,7 +138,7 @@ The effect written in Midway is as follows:
// src/strategy/local.strategy.ts

import { CustomStrategy, PassportStrategy } from '@midwayjs/passport';
import { Strategy } from 'passport-local';
import { Strategy, IStrategyOptions } from 'passport-local';
import { Repository } from 'typeorm';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { UserEntity } from './user';
Expand All @@ -153,19 +152,24 @@ export class LocalStrategy extends PassportStrategy(Strategy) {
//Verification of strategy
async validate(username, password) {
const user = await this.userModel.findOneBy({ username });
if (await bcrypt.compare(password, user.password)) {
throw new Error('error password ' + username);
if (!user) {
throw new Error('User does not exist ' + username);
}
if (!(await bcrypt.compare(password, user.password))) {
throw new Error('Password is incorrect ' + username);
}

return {
username,
password,
};
return user;
}

// Constructor parameters of the current strategy
getStrategyOptions(): any {
return {};
getStrategyOptions(): IStrategyOptions {
return {
usernameField: 'username',
passwordField: 'password',
passReqToCallback: true,
session: false
};
}
}
```
Expand Down

0 comments on commit 9f62383

Please sign in to comment.