Skip to content

Commit

Permalink
Rename create auth functions (#7137)
Browse files Browse the repository at this point in the history
* auth0

* azure

* clerk

* custom

* dbAuth

* firebase

* netlify

* supabase

* supertokens

* cli-helpers

* update test project fixture
  • Loading branch information
jtoar authored Dec 13, 2022
1 parent 0b33c59 commit aa6fb1e
Show file tree
Hide file tree
Showing 39 changed files with 95 additions and 102 deletions.
4 changes: 2 additions & 2 deletions __fixtures__/test-project/web/src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createDbAuthClient, createDbAuth } from '@redwoodjs/auth-dbauth-web'
import { createDbAuthClient, createAuth } from '@redwoodjs/auth-dbauth-web'

const dbAuthClient = createDbAuthClient()

export const { AuthProvider, useAuth } = createDbAuth(dbAuthClient)
export const { AuthProvider, useAuth } = createAuth(dbAuthClient)
4 changes: 2 additions & 2 deletions docs/docs/auth/firebase.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ We're using [Firebase Google Sign-In](https://firebase.google.com/docs/auth/web/
import * as firebaseAuth from '@firebase/auth'
import { initializeApp, getApp, getApps } from 'firebase/app'

import { createFirebaseAuth } from '@redwoodjs/auth-providers-web'
import { createAuth } from '@redwoodjs/auth-providers-web'

const firebaseConfig = {
apiKey: process.env.FIREBASE_API_KEY,
Expand Down Expand Up @@ -50,7 +50,7 @@ export const firebaseClient = {
firebaseApp,
}

export const { AuthProvider, useAuth } = createFirebaseAuth(firebaseClient)
export const { AuthProvider, useAuth } = createAuth(firebaseClient)
```

## Usage
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Auth0Client } from '@auth0/auth0-spa-js'

import { createAuth0Auth } from '@redwoodjs/auth-auth0-web'
import { createAuth } from '@redwoodjs/auth-auth0-web'

const auth0 = new Auth0Client({
domain: process.env.AUTH0_DOMAIN || '',
Expand All @@ -21,4 +21,4 @@ const auth0 = new Auth0Client({
// useRefreshTokens: true,
})

export const { AuthProvider, useAuth } = createAuth0Auth(auth0)
export const { AuthProvider, useAuth } = createAuth(auth0)
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { renderHook, act } from '@testing-library/react-hooks'

import { CurrentUser } from '@redwoodjs/auth'

import { createAuth0Auth } from '../auth0'
import { createAuth } from '../auth0'

const user: User = {
sub: 'unique_user_id',
Expand Down Expand Up @@ -85,7 +85,7 @@ function getAuth0Auth(customProviderHooks?: {
currentUser: CurrentUser | null
) => (rolesToCheck: string | string[]) => boolean
}) {
const { useAuth, AuthProvider } = createAuth0Auth(
const { useAuth, AuthProvider } = createAuth(
auth0MockClient as Auth0Client,
customProviderHooks
)
Expand Down
6 changes: 3 additions & 3 deletions packages/auth-providers/auth0/web/src/auth0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { CurrentUser, createAuthentication } from '@redwoodjs/auth'
// TODO: Map out this user properly.
export interface Auth0User {}

export function createAuth0Auth(
export function createAuth(
auth0Client: Auth0Client,
customProviderHooks?: {
useCurrentUser?: () => Promise<Record<string, unknown>>
Expand All @@ -18,12 +18,12 @@ export function createAuth0Auth(
) => (rolesToCheck: string | string[]) => boolean
}
) {
const authImplementation = createAuth0AuthImplementation(auth0Client)
const authImplementation = createAuthImplementation(auth0Client)

return createAuthentication(authImplementation, customProviderHooks)
}

function createAuth0AuthImplementation(auth0Client: Auth0Client) {
function createAuthImplementation(auth0Client: Auth0Client) {
return {
type: 'auth0',
client: auth0Client,
Expand Down
2 changes: 1 addition & 1 deletion packages/auth-providers/auth0/web/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { createAuth0Auth } from './auth0'
export { createAuth } from './auth0'
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PublicClientApplication } from '@azure/msal-browser'

import { createAzureActiveDirectoryAuth } from '@redwoodjs/azure-active-directory-web'
import { createAuth } from '@redwoodjs/azure-active-directory-web'

const azureActiveDirectoryClient = new PublicClientApplication({
auth: {
Expand All @@ -12,6 +12,4 @@ const azureActiveDirectoryClient = new PublicClientApplication({
},
})

export const { AuthProvider, useAuth } = createAzureActiveDirectoryAuth(
azureActiveDirectoryClient
)
export const { AuthProvider, useAuth } = createAuth(azureActiveDirectoryClient)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { renderHook, act } from '@testing-library/react-hooks'

import { CurrentUser } from '@redwoodjs/auth'

import { createAzureActiveDirectoryAuth } from '../azureActiveDirectory'
import { createAuth } from '../azureActiveDirectory'

const user: AccountInfo = {
name: 'John',
Expand Down Expand Up @@ -120,7 +120,7 @@ function getAzureActiveDirectoryAuth(customProviderHooks?: {
currentUser: CurrentUser | null
) => (rolesToCheck: string | string[]) => boolean
}) {
const { useAuth, AuthProvider } = createAzureActiveDirectoryAuth(
const { useAuth, AuthProvider } = createAuth(
azureActiveDirectoryMockClient as AzureActiveDirectoryClient,
customProviderHooks
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {

import { CurrentUser, createAuthentication } from '@redwoodjs/auth'

export function createAzureActiveDirectoryAuth(
export function createAuth(
azureActiveDirectoryClient: AzureActiveDirectoryClient,
customProviderHooks?: {
useCurrentUser?: () => Promise<Record<string, unknown>>
Expand All @@ -16,14 +16,14 @@ export function createAzureActiveDirectoryAuth(
) => (rolesToCheck: string | string[]) => boolean
}
) {
const authImplementation = createAzureActiveDirectoryAuthImplementation(
const authImplementation = createAuthImplementation(
azureActiveDirectoryClient
)

return createAuthentication(authImplementation, customProviderHooks)
}

function createAzureActiveDirectoryAuthImplementation(
function createAuthImplementation(
azureActiveDirectoryClient: AzureActiveDirectoryClient
) {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { createAzureActiveDirectoryAuth } from './azureActiveDirectory'
export { createAuth } from './azureActiveDirectory'
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useEffect } from 'react'

import { ClerkLoaded, ClerkProvider, useUser } from '@clerk/clerk-react'

import { createClerkAuth } from '@redwoodjs/auth-clerk-web'
import { createAuth } from '@redwoodjs/auth-clerk-web'
import { navigate } from '@redwoodjs/router'

// You can set user roles in a "roles" array on the public metadata in Clerk.
Expand All @@ -14,7 +14,7 @@ import { navigate } from '@redwoodjs/router'
// Lastly, be sure to add the key "CLERK_FRONTEND_API_URL" in your app's redwood.toml
// [web] config "includeEnvironmentVariables" setting.

export const { AuthProvider: ClerkRwAuthProvider, useAuth } = createClerkAuth()
export const { AuthProvider: ClerkRwAuthProvider, useAuth } = createAuth()

interface Props {
children: React.ReactNode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { renderHook, act } from '@testing-library/react-hooks'

import { CurrentUser } from '@redwoodjs/auth'

import { createClerkAuth } from '../clerk'
import { createAuth } from '../clerk'

const user: Partial<UserResource> = {
id: 'unique_user_id',
Expand Down Expand Up @@ -106,7 +106,7 @@ function getClerkAuth(customProviderHooks?: {
currentUser: CurrentUser | null
) => (rolesToCheck: string | string[]) => boolean
}) {
const { useAuth, AuthProvider } = createClerkAuth(customProviderHooks)
const { useAuth, AuthProvider } = createAuth(customProviderHooks)
const { result } = renderHook(() => useAuth(), {
wrapper: AuthProvider,
})
Expand Down
6 changes: 3 additions & 3 deletions packages/auth-providers/clerk/web/src/clerk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ import { CurrentUser, createAuthentication } from '@redwoodjs/auth'

type Clerk = ClerkClient | undefined | null

export function createClerkAuth(customProviderHooks?: {
export function createAuth(customProviderHooks?: {
useCurrentUser?: () => Promise<Record<string, unknown>>
useHasRole?: (
currentUser: CurrentUser | null
) => (rolesToCheck: string | string[]) => boolean
}) {
const authImplementation = createClerkAuthImplementation()
const authImplementation = createAuthImplementation()

return createAuthentication(authImplementation, customProviderHooks)
}

function createClerkAuthImplementation() {
function createAuthImplementation() {
return {
type: 'clerk',
client: (window as any).Clerk as Clerk,
Expand Down
2 changes: 1 addition & 1 deletion packages/auth-providers/clerk/web/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { createClerkAuth } from './clerk'
export { createAuth } from './clerk'
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { createAuthentication } from '@redwoodjs/auth'

// If you're integrating with an auth service provider you should delete this
// interface
// Instead you should import the type from their auth client sdk
export interface CustomAuthClient {
// If you're integrating with an auth service provider you should delete this interface.
// Instead you should import the type from their auth client sdk.
export interface AuthClient {
login: () => User
logout: () => void
signup: () => User
getToken: () => string
getUserMetadata: () => User | null
}

// If you're integrating with an auth service provider you should delete this
// interface
// This type should be inferred from the general interface above
// If you're integrating with an auth service provider you should delete this interface.
// This type should be inferred from the general interface above.
interface User {
// The name of the id variable will vary depending on what auth service
// provider you're integrating with. Another common name is `sub`
Expand All @@ -23,16 +21,15 @@ interface User {
roles: string[]
}

// If you're integrating with an auth service provider you should delete this
// interface
// If you're integrating with an auth service provider you should delete this interface
// This type should be inferred from the general interface above
export interface ValidateResetTokenResponse {
error?: string
[key: string]: string | undefined
}

// Replace this with the auth service provider client sdk
const customClient = {
const client = {
login: () => ({
id: 'unique-user-id',
email: '[email protected]',
Expand All @@ -52,8 +49,8 @@ const customClient = {
}),
}

function createCustomAuth() {
const authImplementation = createCustomAuthImplementation(customClient)
function createAuth() {
const authImplementation = createAuthImplementation(client)

// You can pass custom provider hooks here if you need to as a second
// argument. See the Redwood framework source code for how that's used
Expand All @@ -64,14 +61,14 @@ function createCustomAuth() {
// the shape of this object (i.e. keep all the key names) but change all the
// values/functions to use methods from the auth service provider client sdk
// you're integrating with
function createCustomAuthImplementation(customClient: CustomAuthClient) {
function createAuthImplementation(client: AuthClient) {
return {
type: 'custom-auth',
client: customClient,
login: async () => customClient.login(),
logout: async () => customClient.logout(),
signup: async () => customClient.signup(),
getToken: async () => customClient.getToken(),
client,
login: async () => client.login(),
logout: async () => client.logout(),
signup: async () => client.signup(),
getToken: async () => client.getToken(),
/**
* Actual user metadata might look something like this
* {
Expand All @@ -88,8 +85,8 @@ function createCustomAuthImplementation(customClient: CustomAuthClient) {
* "updated_at": "2016-05-15T19:53:12.368652374-07:00"
* }
*/
getUserMetadata: async () => customClient.getUserMetadata(),
getUserMetadata: async () => client.getUserMetadata(),
}
}

export const { AuthProvider, useAuth } = createCustomAuth()
export const { AuthProvider, useAuth } = createAuth()
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createDbAuthClient, createDbAuth } from '@redwoodjs/auth-dbauth-web'
import { createDbAuthClient, createAuth } from '@redwoodjs/auth-dbauth-web'

const dbAuthClient = createDbAuthClient()

export const { AuthProvider, useAuth } = createDbAuth(dbAuthClient)
export const { AuthProvider, useAuth } = createAuth(dbAuthClient)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createDbAuthClient, createDbAuth } from '@redwoodjs/auth-dbauth-web'
import { createDbAuthClient, createAuth } from '@redwoodjs/auth-dbauth-web'
import WebAuthnClient from '@redwoodjs/auth-dbauth-web/webAuthn'

const dbAuthClient = createDbAuthClient({ webAuthn: new WebAuthnClient() })

export const { AuthProvider, useAuth } = createDbAuth(new WebAuthnClient())
export const { AuthProvider, useAuth } = createAuth(new WebAuthnClient())
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { renderHook, act } from '@testing-library/react-hooks'

import { CurrentUser } from '@redwoodjs/auth'

import { createDbAuthClient, DbAuthClientArgs, createDbAuth } from '../dbAuth'
import { createDbAuthClient, DbAuthClientArgs, createAuth } from '../dbAuth'

process.env.RWJS_API_URL = '/.redwood/functions'
process.env.RWJS_API_GRAPHQL_URL = '/.redwood/functions/graphql'
Expand Down Expand Up @@ -89,7 +89,7 @@ const defaultArgs: DbAuthClientArgs & {

function getDbAuth(args = defaultArgs) {
const dbAuthClient = createDbAuthClient(args)
const { useAuth, AuthProvider } = createDbAuth(dbAuthClient, {
const { useAuth, AuthProvider } = createAuth(dbAuthClient, {
useHasRole: args.useHasRole,
useCurrentUser: args.useCurrentUser,
})
Expand Down
2 changes: 1 addition & 1 deletion packages/auth-providers/dbAuth/web/src/dbAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type SignupAttributes = Record<string, unknown> & LoginAttributes

const TOKEN_CACHE_TIME = 5000

export function createDbAuth(
export function createAuth(
dbAuthClient: ReturnType<typeof createDbAuthClient>,
customProviderHooks?: {
useCurrentUser?: () => Promise<Record<string, unknown>>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as firebaseAuth from 'firebase/auth'
import { initializeApp, getApp, getApps } from 'firebase/app'

import { createFirebaseAuth } from '@redwoodjs/auth-firebase-web'
import { createAuth } from '@redwoodjs/auth-firebase-web'

const firebaseConfig = {
apiKey: process.env.FIREBASE_API_KEY,
Expand Down Expand Up @@ -29,4 +29,4 @@ export const firebaseClient = {
firebaseApp, // optional
}

export const { AuthProvider, useAuth } = createFirebaseAuth(firebaseClient)
export const { AuthProvider, useAuth } = createAuth(firebaseClient)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { User, OperationType, OAuthProvider, Auth } from 'firebase/auth'

import { CurrentUser } from '@redwoodjs/auth'

import { createFirebaseAuth, FirebaseClient } from '../firebase'
import { createAuth, FirebaseClient } from '../firebase'

const user: User = {
uid: 'unique_user_id',
Expand Down Expand Up @@ -148,7 +148,7 @@ function getFirebaseAuth(customProviderHooks?: {
currentUser: CurrentUser | null
) => (rolesToCheck: string | string[]) => boolean
}) {
const { useAuth, AuthProvider } = createFirebaseAuth(
const { useAuth, AuthProvider } = createAuth(
firebaseMockClient as FirebaseClient,
customProviderHooks
)
Expand Down
6 changes: 3 additions & 3 deletions packages/auth-providers/firebase/web/src/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const applyProviderOptions = (
return provider
}

export function createFirebaseAuth(
export function createAuth(
firebaseClient: FirebaseClient,
customProviderHooks?: {
useCurrentUser?: () => Promise<Record<string, unknown>>
Expand All @@ -83,7 +83,7 @@ export function createFirebaseAuth(
) => (rolesToCheck: string | string[]) => boolean
}
) {
const authImplementation = createFirebaseAuthImplementation(firebaseClient)
const authImplementation = createAuthImplementation(firebaseClient)

return createAuthentication(authImplementation, customProviderHooks)
}
Expand All @@ -93,7 +93,7 @@ export interface FirebaseClient {
firebaseApp?: FirebaseApp
}

function createFirebaseAuthImplementation({
function createAuthImplementation({
firebaseAuth,
firebaseApp,
}: FirebaseClient) {
Expand Down
Loading

0 comments on commit aa6fb1e

Please sign in to comment.