-
Notifications
You must be signed in to change notification settings - Fork 973
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 #643 from noire-munich/firebase_generator
Firebase generator
- Loading branch information
Showing
6 changed files
with
90 additions
and
14 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
30 changes: 30 additions & 0 deletions
30
packages/cli/src/commands/generate/auth/providers/firebase.js
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,30 @@ | ||
export const config = { | ||
imports: [ | ||
`import * as firebase from 'firebase/app'`, | ||
`import 'firebase/auth'`, | ||
], | ||
init: `const firebaseClientConfig = { | ||
apiKey: process.env.FIREBASE_API_KEY, | ||
authDomain: process.env.FIREBASE_AUTH_DOMAIN, | ||
databaseURL: process.env.FIREBASE_DATABASE_URL, | ||
projectId: process.env.FIREBASE_PROJECT_ID, | ||
storageBucket: process.env.FIREBASE_STORAGE_BUCKET, | ||
messagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID, | ||
appId: process.env.FIREBASE_APP_ID, | ||
} | ||
const firebaseClient = ((config) => { | ||
firebase.initializeApp(config) | ||
return firebase | ||
})(firebaseClientConfig) | ||
`, | ||
authProvider: { client: 'firebaseClient', type: 'firebase' }, | ||
} | ||
|
||
export const packages = ['firebase'] | ||
|
||
export const notes = [ | ||
'You will need to create several environment variables with your Firebase config options.', | ||
'Check out web/src/index.js for the variables you need to add.', | ||
'See: https://firebase.google.com/docs/web/setup#config-object', | ||
] |
2 changes: 1 addition & 1 deletion
2
packages/cli/src/commands/generate/auth/providers/magic-link.js
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
37 changes: 37 additions & 0 deletions
37
packages/cli/src/commands/generate/auth/templates/firebase.auth.js.template
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,37 @@ | ||
// Define what you want `currentUser` to return throughout your app. For example, | ||
// to return a real user from your database, you could do something like: | ||
// | ||
// export const getCurrentUser = async ({ email }) => { | ||
// return await db.user.findOne({ where: { email } }) | ||
// } | ||
|
||
import { AuthenticationError } from '@redwoodjs/api' | ||
import admin from 'firebase-admin' | ||
|
||
import { db } from './db' | ||
|
||
const config = { | ||
apiKey: process.env.FIREBASE_API_KEY, | ||
authDomain: process.env.FIREBASE_AUTH_DOMAIN, | ||
databaseURL: process.env.FIREBASE_DATABASE_URL, | ||
projectId: process.env.FIREBASE_PROJECT_ID, | ||
storageBucket: process.env.FIREBASE_STORAGE_BUCKET, | ||
messagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID, | ||
appId: process.env.FIREBASE_APP_ID, | ||
} | ||
|
||
const adminApp = admin.initializeApp(config) | ||
|
||
export const getCurrentUser = async (token) => { | ||
const { email, uid } = await adminApp.auth().verifyToken(token) | ||
return { email, uid } | ||
} | ||
|
||
// Use this function in your services to check that a user is logged in, and | ||
// optionally raise an error if they're not. | ||
|
||
export const requireAuth = () => { | ||
if (!context.currentUser) { | ||
throw new AuthenticationError("You don't have permission to do that.") | ||
} | ||
} |