python |
Pythonタグが付けられた新着投稿 - Qiita |
Ruby で解く AtCoder ABC012 D Numo::NArray ワーシャル–フロイド法 |
https://qiita.com/superrino130/items/e6d8efd0bbe5c28d2a00
|
atcoder |
2023-01-28 14:56:17 |
python |
Pythonタグが付けられた新着投稿 - Qiita |
悩みすぎた男の末路 |
https://qiita.com/kromiii/items/d6bd2b1de2e27561f7d1
|
googlema |
2023-01-28 14:43:12 |
python |
Pythonタグが付けられた新着投稿 - Qiita |
Blenderでライフゲームのアニメーション |
https://qiita.com/SaitoTsutomu/items/711989ba3e5ebcb78730
|
blender |
2023-01-28 14:37:28 |
python |
Pythonタグが付けられた新着投稿 - Qiita |
Julia でデータフレームをフィルターすると |
https://qiita.com/WolfMoon/items/5115a9397f08b7e632ab
|
julia |
2023-01-28 14:08:29 |
Ruby |
Rubyタグが付けられた新着投稿 - Qiita |
Ruby で解く AtCoder ABC012 D Numo::NArray ワーシャル–フロイド法 |
https://qiita.com/superrino130/items/e6d8efd0bbe5c28d2a00
|
atcoder |
2023-01-28 14:56:17 |
AWS |
AWSタグが付けられた新着投稿 - Qiita |
lambdaでEC2の停止起動をしてみる |
https://qiita.com/masakazu193/items/3593183cc80f0a524f36
|
lambda |
2023-01-28 14:58:58 |
AWS |
AWSタグが付けられた新着投稿 - Qiita |
AWS Network FirewallのARNを取得する |
https://qiita.com/omochi_1190/items/6b6b75bbd41ebaa4bc6e
|
awsnetworkfirewall |
2023-01-28 14:33:17 |
海外TECH |
DEV Community |
How to Add JWT Authentication to NestJS Apps |
https://dev.to/leduc1901/how-to-add-jwt-authentication-to-nestjs-apps-6bb
|
How to Add JWT Authentication to NestJS Apps IntroductionAuthentication is an important part of our applications From time to time there are many ways to handle authentication With each requirement we find the suitable approach to handle authentication This article is a simple tutorial on how to implement authentication with NestJS before go into the guide I m going to demonstrate the technologies that are going to be used in the guideJWT or JSON Web Token is an industry standard RFC method for representing claims securely between two parties Passport is the most popular Node authentication library well known by the community and successfully used in many production application NestJS has supported it outside the box with nestjs passport InstallationFirst we create the projectnest new your project nameThen we add the dependenciesyarn add nestjs passport passport passport local passport jwt nestjs jwt We are going to use mongoose to store the datayarn add mongoose nestjs mongoose Generate modules services and controllersFor the authentication we need modules AuthModule and UserModule each of them need controller and service files Auth module nest g module authnest g service authnest g controller auth User modulenest g module usersnest g service usersnest g controller users Define schema and interfaceWe need an UserSchema and an User interface let s create the user model ts file Create User ServiceWe created the users service ts before next we create methods for the sign up createUser get all users getUsers and get an user getUser Create User Module an ControllerNothing much to say about these files I created routes in the UsersController to sign up and get all users and import all we have with users to the users module ts fileOnly thing to keep in mind is the UseGuards AuthGuard jwt part which means we can t access this route without logged in and have the jwt Auth ServiceThere are methods in the AuthService one is to validate if the user exist in our database with correct credentials the other one is to return an access token which is a JWT assigned with an username StrategiesWe have to create the strategies in this guide I will create strategies one is LocalStrategy and the other is JwtStrategyThe LocalStrategy serves a purpose when we need to validate the username and password before going deeper into the controller In this case I create the built in validation method with the validateUserCredentials from the AuthServiceFor the JwtStrategy we extend the PassportStrategy from the nestjs passport library just like above then we return an object consists the username The constructor need to extract the JWT from Header Bearer token the access token We also need a secret key for JWT Strategy mine is SECRET KEY but I suggest you to use a more secure way to store keys Auth module and controllerLike the UserController above we define the route for authentication in this controller is the login route You can see I used AuthGuard local from the LocalStrategy above So we only proceed to login after the validation succeeded Nothing much to say about the auth module ts file we import all the modules we need assign the providers controllers App moduleEvery NestJS project comes with the app module ts file that centralizes all the modulesNote I used MongoDB Atlas to create a cloud database but you can decide what database to use ConclusionLet s try our APIs in Postman to see if it worksFirst start the server with yarn start devThen open Postman we re gonna start with the login routeWe can see the server returns access token for us we will copy this into every guarded API like the getUsers from UserControllerThat s all isn t that hard right you can check out my source code here Last WordsAlthough my content is free for everyone but if you find this article helpful you can buy me a coffee here |
2023-01-28 05:25:06 |
海外ニュース |
Japan Times latest articles |
Putin plans new Ukraine push despite losses as he prepares for years of war |
https://www.japantimes.co.jp/news/2023/01/28/world/vladimir-putin-russia-ukraine-war-years/
|
kremlin |
2023-01-28 14:41:06 |
海外ニュース |
Japan Times latest articles |
China celebrated the Lunar New Year like COVID no longer exists |
https://www.japantimes.co.jp/news/2023/01/28/asia-pacific/china-lunar-new-year-covid-2/
|
China celebrated the Lunar New Year like COVID no longer existsMore than million trips were made during the holiday nearly of pre pandemic levels according to the Ministry of Culture and Tourism |
2023-01-28 14:23:36 |
ニュース |
BBC News - Home |
Flybe: Regional carrier ceases trading and cancels all flights |
https://www.bbc.co.uk/news/uk-64436500?at_medium=RSS&at_campaign=KARANGA
|
airline |
2023-01-28 05:10:32 |
ニュース |
BBC News - Home |
AP deletes widely mocked tweet about ‘the’ French |
https://www.bbc.co.uk/news/world-europe-64436973?at_medium=RSS&at_campaign=KARANGA
|
agency |
2023-01-28 05:20:21 |
ニュース |
BBC News - Home |
Newspaper headlines: Hunt's business tax cuts and 'oil giants cash in' |
https://www.bbc.co.uk/news/blogs-the-papers-64435047?at_medium=RSS&at_campaign=KARANGA
|
bumper |
2023-01-28 05:39:56 |
ビジネス |
東洋経済オンライン |
アカデミー作品賞「トップガン」推す声の切実事情 配信に押された映画館の存在意義を取り戻せるか | 映画・音楽 | 東洋経済オンライン |
https://toyokeizai.net/articles/-/648291?utm_source=rss&utm_medium=http&utm_campaign=link_back
|
存在意義 |
2023-01-28 14:30:00 |
コメント
コメントを投稿