Closed9
NestJSに入門してFirebase Authenticationを使ってみる
Firebase Authenticationは仕事以外で使ったことはないから使ってみたい。
NestJSって存在は今日知ったくらい。
$ nest new firebase-test
⚡ We will scaffold your app in a few seconds..
? Which package manager would you ❤️ to use? npm
CREATE firebase-test/.eslintrc.js (663 bytes)
CREATE firebase-test/.prettierrc (51 bytes)
CREATE firebase-test/README.md (3340 bytes)
CREATE firebase-test/nest-cli.json (171 bytes)
CREATE firebase-test/package.json (1954 bytes)
CREATE firebase-test/tsconfig.build.json (97 bytes)
CREATE firebase-test/tsconfig.json (546 bytes)
CREATE firebase-test/src/app.controller.ts (274 bytes)
CREATE firebase-test/src/app.module.ts (249 bytes)
CREATE firebase-test/src/app.service.ts (142 bytes)
CREATE firebase-test/src/main.ts (208 bytes)
CREATE firebase-test/src/app.controller.spec.ts (617 bytes)
CREATE firebase-test/test/jest-e2e.json (183 bytes)
CREATE firebase-test/test/app.e2e-spec.ts (630 bytes)
✔ Installation in progress... ☕
🚀 Successfully created project firebase-test
👉 Get started with the following commands:
$ cd firebase-test
$ npm run start
Thanks for installing Nest 🙏
Please consider donating to our open collective
to help us maintain this package.
🍷 Donate: https://opencollective.com/nest
firebase-adminのインストール
$ npm i firebase-admin
そういえば最近Firebaseのコンソールのデザインが変わった。
人がいっぱい出てくるようになった。
Webアプリも追加しておく
プロジェクトの設定>サービスアカウントから新しい秘密鍵を生成しておく。
間違えてPushしないように.gitignoreにも追加しておく。
Firebase AuthenticationでプロバイダはGoogleのみにしてみる。
main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ConfigService } from '@nestjs/config';
import { ServiceAccount } from 'firebase-admin';
import * as admin from 'firebase-admin';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const configService: ConfigService = app.get(ConfigService);
const adminConfig: ServiceAccount = {
projectId: configService.get<string>('FIREBASE_PROJECT_ID'),
privateKey: configService
.get<string>('FIREBASE_PRIVATE_KEY')
.replace(/\\n/g, '\n'),
clientEmail: configService.get<string>('FIREBASE_CLIENT_EMAIL'),
};
admin.initializeApp({
credential: admin.credential.cert(adminConfig),
});
await app.listen(3000);
}
bootstrap();
コントローラーの作成
nest g controller auth
このスクラップは2023/11/21にクローズされました