Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Firebase to the AuthClients #593

Merged
merged 6 commits into from
May 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/api/src/auth/authHeaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { AuthenticationError } from 'apollo-server-lambda'

import { verifyAuth0Token } from './verifyAuth0Token'

export type SupportedAuthTypes = 'auth0' | 'netlify' | 'gotrue' | 'magic.link'
export type SupportedAuthTypes = 'auth0' | 'netlify' | 'gotrue' | 'magic.link' | 'firebase'

// This is shared by `@redwoodjs/web`
const AUTH_PROVIDER_HEADER = 'auth-provider'
Expand Down Expand Up @@ -67,6 +67,9 @@ export const decodeAuthToken = async ({
}
break
}
case 'firebase':
decoded = token
break
case 'auth0': {
decoded = await verifyAuth0Token(token)
break
Expand Down
1 change: 1 addition & 0 deletions packages/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"devDependencies": {
"@auth0/auth0-spa-js": "^1.7.0",
"@types/react": "16.9.35",
"firebase": "^7.14.5",
"gotrue-js": "^0.9.25",
"magic-sdk": "^1.3.5",
"netlify-identity-widget": "1.6.0",
Expand Down
23 changes: 21 additions & 2 deletions packages/auth/src/authClient.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import type { default as GoTrue, User as GoTrueUser } from 'gotrue-js'
import type { Auth0Client as Auth0 } from '@auth0/auth0-spa-js'
import type NetlifyIdentityNS from 'netlify-identity-widget'
import * as firebase from 'firebase/app'
import type { Magic, MagicUserMetadata } from 'magic-sdk'
// TODO: Can also return an Auth0 user which doesn't have a definition.
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface Auth0User {}
export type { GoTrueUser }
export type NetlifyIdentity = typeof NetlifyIdentityNS
export type FirebaseClient = typeof firebase
export type MagicLinks = Magic
export type MagicUser = MagicUserMetadata

export type SupportedAuthClients = Auth0 | GoTrue | NetlifyIdentity | MagicLinks
export type SupportedAuthTypes = 'auth0' | 'gotrue' | 'netlify' | 'magic.link'
export type SupportedAuthClients = Auth0 | GoTrue | NetlifyIdentity | MagicLinks | FirebaseClient
export type SupportedAuthTypes = 'auth0' | 'gotrue' | 'netlify' | 'magic.link' | 'firebase'

export interface AuthClient {
restoreAuthState?(): void | Promise<any>
Expand Down Expand Up @@ -120,6 +122,21 @@ const mapAuthClientNetlify = (client: NetlifyIdentity): AuthClient => {
}
}

const mapAuthClientFirebase = (client: FirebaseClient): AuthClient => {
return {
type: 'firebase',
client,
restoreAuthState: () => client.auth().getRedirectResult(),
login: async () => {
const provider = new client.auth.GoogleAuthProvider()
return client.auth().signInWithRedirect(provider)
},
logout: () => client.auth().signOut(),
getToken: async () => client.auth().currentUser?.getIdToken() ?? null,
currentUser: async () => client.auth().currentUser,
}
}

const mapAuthClientMagicLinks = (client: MagicLinks): MagicLinksClient => {
return {
type: 'magic.link',
Expand All @@ -140,6 +157,8 @@ export const createAuthClient = (
type: SupportedAuthTypes
): AuthClient => {
switch (type) {
case 'firebase':
return mapAuthClientFirebase(client as FirebaseClient)
case 'auth0':
return mapAuthClientAuth0(client as Auth0)
case 'gotrue':
Expand Down
Loading