Open soon530 opened 2 years ago
安裝@nestjs/passport
套件。
% yarn add @nestjs/passport
yarn add v1.22.19
[1/4] 🔍 Resolving packages...
[2/4] 🚚 Fetching packages...
[3/4] 🔗 Linking dependencies...
warning " > swagger-ui-express@4.5.0" has unmet peer dependency "express@>=4.0.0".
warning " > ts-loader@9.4.1" has unmet peer dependency "webpack@^5.0.0".
warning " > @nestjs/passport@9.0.0" has unmet peer dependency "passport@^0.4.0 || ^0.5.0 || ^0.6.0".
[4/4] 🔨 Building fresh packages...
success Saved lockfile.
success Saved 1 new dependency.
info Direct dependencies
└─ @nestjs/passport@9.0.0
info All dependencies
└─ @nestjs/passport@9.0.0
✨ Done in 6.59s.
安裝passport
套件。
% yarn add passport
yarn add v1.22.19
[1/4] 🔍 Resolving packages...
[2/4] 🚚 Fetching packages...
[3/4] 🔗 Linking dependencies...
warning " > swagger-ui-express@4.5.0" has unmet peer dependency "express@>=4.0.0".
warning " > ts-loader@9.4.1" has unmet peer dependency "webpack@^5.0.0".
[4/4] 🔨 Building fresh packages...
success Saved lockfile.
success Saved 3 new dependencies.
info Direct dependencies
└─ passport@0.6.0
info All dependencies
├─ passport-strategy@1.0.0
├─ passport@0.6.0
└─ pause@0.0.1
✨ Done in 2.58s.
安裝自己做驗證的套件。
% yarn add passport-local
yarn add v1.22.19
[1/4] 🔍 Resolving packages...
[2/4] 🚚 Fetching packages...
[3/4] 🔗 Linking dependencies...
warning " > swagger-ui-express@4.5.0" has unmet peer dependency "express@>=4.0.0".
warning " > ts-loader@9.4.1" has unmet peer dependency "webpack@^5.0.0".
[4/4] 🔨 Building fresh packages...
success Saved lockfile.
success Saved 1 new dependency.
info Direct dependencies
└─ passport-local@1.0.0
info All dependencies
└─ passport-local@1.0.0
✨ Done in 2.65s.
嗯,應該是typescript要的吧@@?
% yarn add -D @types/passport-local
yarn add v1.22.19
[1/4] 🔍 Resolving packages...
[2/4] 🚚 Fetching packages...
[3/4] 🔗 Linking dependencies...
warning " > swagger-ui-express@4.5.0" has unmet peer dependency "express@>=4.0.0".
warning " > ts-loader@9.4.1" has unmet peer dependency "webpack@^5.0.0".
[4/4] 🔨 Building fresh packages...
success Saved lockfile.
success Saved 2 new dependencies.
info Direct dependencies
└─ @types/passport-local@1.0.34
info All dependencies
├─ @types/passport-local@1.0.34
└─ @types/passport-strategy@0.2.35
✨ Done in 2.87s.
auth.controller.ts
request.user
完全是配合passport
來用的,沒有AuthGuard('local')
的話,是一點用處都沒有的。
@UseGuards(AuthGuard('local'))
@Post('/signin')
signin(@Req() request: Request) {
return request.user;
}
https://ithelp.ithome.com.tw/articles/10279148