mirror of
https://github.com/hackku21/loc-chain-backend.git
synced 2024-10-27 20:34:03 +00:00
Add firebase user auth middleware
This commit is contained in:
parent
b0123afee8
commit
b81e571901
42
src/app/http/middlewares/api/FirebaseUserOnly.middleware.ts
Normal file
42
src/app/http/middlewares/api/FirebaseUserOnly.middleware.ts
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
import {Config, error, Middleware} from "@extollo/lib"
|
||||||
|
import {Inject, Injectable} from "@extollo/di"
|
||||||
|
import {FirebaseUnit} from "../../../units/FirebaseUnit"
|
||||||
|
import {HTTPStatus} from "@extollo/util"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FirebaseUserOnly Middleware
|
||||||
|
* --------------------------------------------
|
||||||
|
* Authenticates the request based on the user ID token.
|
||||||
|
*/
|
||||||
|
@Injectable()
|
||||||
|
export class FirebaseUserOnly extends Middleware {
|
||||||
|
@Inject()
|
||||||
|
protected readonly firebase!: FirebaseUnit
|
||||||
|
|
||||||
|
@Inject()
|
||||||
|
protected readonly config!: Config
|
||||||
|
|
||||||
|
get headerName(): string {
|
||||||
|
return String(this.config.get('app.firebase.api_auth_header'))
|
||||||
|
}
|
||||||
|
|
||||||
|
getAuthHeader(): string {
|
||||||
|
const tokens = this.request.getHeader(this.headerName)
|
||||||
|
if ( Array.isArray(tokens) ) return tokens[0]
|
||||||
|
return String(tokens)
|
||||||
|
}
|
||||||
|
|
||||||
|
public async apply() {
|
||||||
|
const token = this.getAuthHeader()
|
||||||
|
|
||||||
|
if ( !token ) {
|
||||||
|
return error(`Missing ${this.headerName} header`, HTTPStatus.UNAUTHORIZED, 'json')
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await this.firebase.get().auth().verifyIdToken(token)
|
||||||
|
} catch (e) {
|
||||||
|
return error('Invalid API token.', HTTPStatus.UNAUTHORIZED, 'json')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user