-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from Nhogs/develop
add first version of code with unit tests
- Loading branch information
Showing
24 changed files
with
717 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
"port": 9199 | ||
}, | ||
"ui": { | ||
"enabled": false | ||
"enabled": true | ||
}, | ||
"auth": { | ||
"port": 9099 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export const FIREBASE_CONFIG: string = 'FirebaseConfig'; | ||
export const FIREBASE_APP: string = 'FirebaseApp'; | ||
export const FIREBASE_CONFIG: string = "FIREBASE_CONFIG"; | ||
export const FIREBASE_APP: string = "FIREBASE_APP"; | ||
export const DEFAULT_APP_NAME: string = "DEFAULT_APP_NAME"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export interface FirebaseConfig { | ||
apiKey: string; | ||
authDomain: string; | ||
projectId: string; | ||
apiKey?: string; | ||
authDomain?: string; | ||
emulator?: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
export * from './firebase-config'; | ||
import { FirestoreDataConverter } from "@firebase/firestore"; | ||
|
||
export * from "./firebase-config"; | ||
export { FirestoreDataConverter }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Inject, Injectable, Logger } from "@nestjs/common"; | ||
import { FirebaseApp } from "firebase/app"; | ||
import { FIREBASE_APP, FIREBASE_CONFIG } from "../firebase.constants"; | ||
import { | ||
connectAuthEmulator, | ||
createUserWithEmailAndPassword, | ||
getAuth, | ||
signInWithEmailAndPassword, | ||
UserCredential, | ||
} from "firebase/auth"; | ||
import { FirebaseConfig } from "../interface"; | ||
|
||
@Injectable() | ||
export class AuthService { | ||
private readonly _logger = new Logger(AuthService.name); | ||
private readonly _auth; | ||
constructor( | ||
@Inject(FIREBASE_APP) private readonly firebaseApp: FirebaseApp, | ||
@Inject(FIREBASE_CONFIG) private readonly firebaseConfig: FirebaseConfig | ||
) { | ||
this._auth = getAuth(this.firebaseApp); | ||
if (this.firebaseConfig.emulator) { | ||
connectAuthEmulator(this._auth, "http://localhost:9099"); | ||
} | ||
} | ||
|
||
async createUserWithEmailAndPassword( | ||
email: string, | ||
password: string | ||
): Promise<UserCredential> { | ||
return createUserWithEmailAndPassword(this._auth, email, password); | ||
} | ||
|
||
async signInWithEmailAndPassword( | ||
email: string, | ||
password: string | ||
): Promise<UserCredential> { | ||
return signInWithEmailAndPassword(this._auth, email, password); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,26 @@ | ||
import { Inject, Injectable, Logger } from '@nestjs/common'; | ||
import { FirebaseApp } from '@firebase/app'; | ||
import { getDownloadURL, getStorage, ref } from '@firebase/storage'; | ||
import { FIREBASE_APP } from '../firebase.constants'; | ||
import {Inject, Injectable, Logger} from "@nestjs/common"; | ||
import {FirebaseApp} from "@firebase/app"; | ||
import {connectStorageEmulator, getStorage,} from "@firebase/storage"; | ||
import {FIREBASE_APP, FIREBASE_CONFIG} from "../firebase.constants"; | ||
import {FirebaseConfig} from "../interface"; | ||
|
||
@Injectable() | ||
export class StorageService { | ||
constructor( | ||
@Inject(FIREBASE_APP) private readonly firebaseApp: FirebaseApp, | ||
) {} | ||
private readonly _logger = new Logger(StorageService.name); | ||
private storage = getStorage(this.firebaseApp); | ||
private readonly _storage; | ||
|
||
async getDownloadURL(url: string): Promise<string> { | ||
this._logger.debug('getDownloadURL=>' + url); | ||
return getDownloadURL(ref(this.storage, url)); | ||
constructor( | ||
@Inject(FIREBASE_APP) private readonly firebaseApp: FirebaseApp, | ||
@Inject(FIREBASE_CONFIG) private readonly firebaseConfig: FirebaseConfig | ||
) { | ||
this._storage = getStorage(this.firebaseApp); | ||
if (this.firebaseConfig.emulator) { | ||
connectStorageEmulator(this._storage, "localhost", 9199); | ||
} | ||
} | ||
// | ||
// async getDownloadURL(url: string): Promise<string> { | ||
// this._logger.debug('getDownloadURL=>' + url); | ||
// return getDownloadURL(ref(this.storage, url)); | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { INestApplication } from "@nestjs/common"; | ||
import { Test, TestingModule } from "@nestjs/testing"; | ||
import axios from "axios"; | ||
import { AppAsyncModule } from "../src/app.async.module"; | ||
import { AuthService } from "../../lib/service/auth.service"; | ||
|
||
describe("Firebase", () => { | ||
let module: TestingModule; | ||
let app: INestApplication; | ||
let authService: AuthService; | ||
|
||
beforeAll(async () => { | ||
module = await Test.createTestingModule({ | ||
imports: [AppAsyncModule], | ||
}).compile(); | ||
|
||
app = module.createNestApplication(); | ||
await app.init(); | ||
authService = module.get<AuthService>(AuthService); | ||
}); | ||
|
||
it(`should create and sign in user`, async () => { | ||
const user = ( | ||
await authService.createUserWithEmailAndPassword( | ||
"[email protected]", | ||
"userpassword" | ||
) | ||
).user; | ||
|
||
expect(user.email).toMatchInlineSnapshot(`"[email protected]"`); | ||
|
||
const user2 = ( | ||
await authService.signInWithEmailAndPassword( | ||
"[email protected]", | ||
"userpassword" | ||
) | ||
).user; | ||
|
||
expect(user2.email).toMatchInlineSnapshot(`"[email protected]"`); | ||
}); | ||
|
||
afterEach(async () => { | ||
return await axios | ||
.delete( | ||
"http://localhost:9099/emulator/v1/projects/demo-nhogs-nestjs-firebase/accounts" | ||
) | ||
.then(function (response) { | ||
expect(response.status).toMatchInlineSnapshot(`200`); | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
return await app.close(); | ||
}); | ||
}); |
Oops, something went wrong.