passport strategy for firebase authentication
npm install passport-firebase```
npm install passport-firebase
or
yarn add passport-firebase
#### 1. Initialize
An example configuration which reads the JWT from the http
Authorization header with the scheme 'bearer':
-
`js`
const FirebaseStrategy = require('passport-firebase').Strategy;
const opts = {};
opts.issuer = 'https://securetoken.google.com/
opts.audience = '
passport.use(new FirebaseStrategy(opts, function(jwt_payload, done) {
User.findOne({id: jwt_payload.sub}, function(err, user) {
if (err) {
return done(err, false);
}
if (user) {
return done(null, user);
} else {
return done(null, false);
// or you could create a new account
}
});
}));
#### 2. Authenticate requests
Use passport.authenticate() specifying 'firebase' as the strategy.
`js`
app.post('/profile', passport.authenticate('firebase', { session: false }),
function(req, res) {
res.send(req.user.profile);
}
);
- firebase.strategy.ts
`js
import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy } from 'passport-firebase';
@Injectable()
export class FirebaseStrategy extends PassportStrategy(Strategy) {
constructor() {
super({
issuer: 'https://securetoken.google.com/
audience: '
});
}
async validate(payload: any) {
return payload;
}
}
`
- firebase-auth.guard.ts
`js`
@Injectable()
export class FirebaseAuthGuard extends AuthGuard('firebase') {}
- controller.ts
`jsreq.user
@UseGuards(FirebaseAuthGuard)
@Get('auth')
getAuth(@Req() req: any): void {
// user info can get by ``
console.log(req.user);
}
The MIT License
Copyright (c) 2022 Naoto Sato