soon530 / nestjs-study

來學一下nestjs吧!
0 stars 0 forks source link

Authentication password #36

Open soon530 opened 2 years ago

soon530 commented 2 years ago

https://ithelp.ithome.com.tw/articles/10279148

soon530 commented 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.
soon530 commented 2 years ago

安裝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.
soon530 commented 2 years ago

安裝自己做驗證的套件。

% 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.
soon530 commented 2 years ago

嗯,應該是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.
soon530 commented 2 years ago

auth.controller.ts request.user完全是配合passport來用的,沒有AuthGuard('local')的話,是一點用處都沒有的。

    @UseGuards(AuthGuard('local'))
    @Post('/signin')
    signin(@Req() request: Request) {
        return request.user;
    }