Skip to content

Commit

Permalink
Wrap up auth provider
Browse files Browse the repository at this point in the history
  • Loading branch information
jamespohalloran committed Aug 29, 2023
1 parent dc294a6 commit 3903b86
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 101 deletions.
35 changes: 17 additions & 18 deletions examples/kitchen-sink/tina/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,26 @@ const router = ({ document, collection }) => {
const extendedRouter = ({ document, collection }) => {
return `/${collection.name}/${document._sys.breadcrumbs.join('/')}`
}

const customAuthProvider = {
getToken: async () => {
return { id_token: 'some-token' }
},
logout: async () => {
localStorage.removeItem(TINA_TOKEN_KEY)
},
authenticate: async () => {
localStorage.setItem(TINA_TOKEN_KEY, 'some-token')
return true
},
getUser: async () => {
return localStorage.getItem(TINA_TOKEN_KEY)
},
}
export default defineConfig({
// contentApiUrlOverride: '/api/gql',
admin: {
auth: {
useLocalAuth: true,
// If you wanted to use custom auth
customAuth: true,
getToken: async () => {
return { id_token: 'some-token' }
},
logout: async () => {
localStorage.removeItem(TINA_TOKEN_KEY)
},
authenticate: async () => {
localStorage.setItem(TINA_TOKEN_KEY, 'some-token')
return true
},
getUser: async () => {
return localStorage.getItem(TINA_TOKEN_KEY)
},
},
auth: customAuthProvider,
},
build: {
outputFolder: 'admin',
Expand Down
34 changes: 16 additions & 18 deletions examples/tina-self-hosted-demo/tina/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,22 @@ import { iconSchema } from '../components/util/icon'
const config = defineStaticConfig({
contentApiUrlOverride: '/api/gql',
admin: {
auth: {
useLocalAuth: true,
// If you wanted to use custom auth
customAuth: true,
getToken: async () => {
return { id_token: 'some-token' }
},
logout: async () => {
localStorage.removeItem(TINA_TOKEN_KEY)
},
authenticate: async () => {
localStorage.setItem(TINA_TOKEN_KEY, 'some-token')
return true
},
getUser: async () => {
return localStorage.getItem(TINA_TOKEN_KEY)
},
},
// auth: {
// // If you wanted to use custom auth
// getToken: async () => {
// return { id_token: 'some-token' }
// },
// logout: async () => {
// localStorage.removeItem(TINA_TOKEN_KEY)
// },
// authenticate: async () => {
// localStorage.setItem(TINA_TOKEN_KEY, 'some-token')
// return true
// },
// getUser: async () => {
// return localStorage.getItem(TINA_TOKEN_KEY)
// },
// },
},
clientId: process.env.NEXT_PUBLIC_TINA_CLIENT_ID!,
branch:
Expand Down
5 changes: 2 additions & 3 deletions experimental-examples/tina-cloud-starter/.tina/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ const config = defineConfig({
process.env.HEAD!, // Netlify branch env
token: process.env.TINA_TOKEN!,
admin: {
auth: {
useLocalAuth: true,
},
// auth: {
// },
},
media: {
// If you wanted cloudinary do this
Expand Down
2 changes: 0 additions & 2 deletions packages/@tinacms/cli/src/cmds/init/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,6 @@ async function apply({
contentApiUrlOverride: '/api/gql',
admin: {
auth: {
useLocalAuth: isLocal,
customAuth: !isLocal,
...createTinaNextAuthHandler({
callbackUrl: '/admin/index.html',
isLocalDevelopment: isLocal,
Expand Down
6 changes: 1 addition & 5 deletions packages/@tinacms/cli/src/cmds/init/templates/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,11 @@ export const configExamples: {
next: (args, opts) => {
const authConfig = opts.nextAuth
? `admin: {
auth: {
useLocalAuth: isLocal,
customAuth: !isLocal,
...createTinaNextAuthHandler({
auth: createTinaNextAuthHandler({
callbackUrl: '/admin/index.html',
isLocalDevelopment: isLocal,
name: '${args.nextAuthCredentialsProviderName}',
})
}
},`
: `clientId: ${
args.clientId
Expand Down
89 changes: 40 additions & 49 deletions packages/@tinacms/schema-tools/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,45 @@ type TokenObject = {
refresh_token?: string
}

interface AuthProvider {
/**
* Used for getting the token from the custom auth provider
*
* @returns {Promise<TokenObject | null>}
**/
getToken: () => Promise<TokenObject | null>
/**
* Used to logout from the custom auth provider
*
**/
logout: () => Promise<void>
/**
* Used for getting the user from the custom auth provider. If this returns a truthy value, the user will be logged in and the CMS will be enabled.
*
* If this returns a falsy value, the user will be logged out and the CMS will be disabled.
*
**/
getUser: () => Promise<any | null>
/**
* Used to authorize the user with the custom auth provider.
*
* If this returns a truthy value, the user will be logged in and the CMS will be enabled.
*
* If not provided, the existence of a user will be enough to authorize the user.
*
* @param context
*/
authorize?: (context?: any) => Promise<any | null>
/**
* Used to authenticate the user with the custom auth provider. This is called when the user clicks the login button.
*
**/
authenticate: () => Promise<any | null>

onLogin?: (args: { token: TokenObject }) => Promise<void>
onLogout?: () => Promise<void>
}

export interface Config<
CMSCallback = undefined,
FormifyCallback = undefined,
Expand All @@ -439,55 +478,7 @@ export interface Config<
> {
contentApiUrlOverride?: string
admin?: {
auth?: {
/**
* If you wish to use the local auth provider, set this to true
*
* This will take precedence over the customAuth option (if set to true)
*
**/
useLocalAuth?: boolean
/**
* If you are using a custom auth provider, set this to true
**/
customAuth?: boolean
/**
* Used for getting the token from the custom auth provider
*
* @returns {Promise<TokenObject | null>}
**/
getToken?: () => Promise<TokenObject | null>
/**
* Used to logout from the custom auth provider
*
**/
logout?: () => Promise<void>
/**
* Used for getting the user from the custom auth provider. If this returns a truthy value, the user will be logged in and the CMS will be enabled.
*
* If this returns a falsy value, the user will be logged out and the CMS will be disabled.
*
**/
getUser?: () => Promise<any | null>
/**
* Used to authorize the user with the custom auth provider.
*
* If this returns a truthy value, the user will be logged in and the CMS will be enabled.
*
* If not provided, the existence of a user will be enough to authorize the user.
*
* @param context
*/
authorize?: (context?: any) => Promise<any | null>
/**
* Used to authenticate the user with the custom auth provider. This is called when the user clicks the login button.
*
**/
authenticate?: () => Promise<any | null>

onLogin?: (args: { token: TokenObject }) => Promise<void>
onLogout?: () => Promise<void>
}
auth?: AuthProvider
}
/**
* The Schema is used to define the shape of the content.
Expand Down
4 changes: 4 additions & 0 deletions packages/tinacms/src/admin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ export const TinaAdmin = ({
}
/>
)}
<Route
path="login"
element={<DefaultWrapper cms={cms}></DefaultWrapper>}
/>
<Route
path="graphql"
element={
Expand Down
6 changes: 2 additions & 4 deletions packages/tinacms/src/auth/TinaCloudProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ export const AuthWallInner = ({
const client: Client = cms.api.tina
// Weather or not we are using Tina Cloud for auth
const isTinaCloud =
!client.isLocalMode &&
!client.schema?.config?.config?.admin?.auth?.customAuth
!client.isLocalMode && !client.schema?.config?.config?.admin?.auth

const [activeModal, setActiveModal] = useState<ModalNames>(null)
const [errorMessage, setErrorMessage] = useState<
Expand Down Expand Up @@ -321,8 +320,7 @@ export const TinaCloudProvider = (
const client: Client = cms.api.tina
// Weather or not we are using Tina Cloud for auth
const isTinaCloud =
!client.isLocalMode &&
!client.schema?.config?.config?.admin?.auth?.customAuth
!client.isLocalMode && !client.schema?.config?.config?.admin?.auth

const handleListBranches = async (): Promise<Branch[]> => {
const branches = await cms.api.tina.listBranches({
Expand Down
3 changes: 1 addition & 2 deletions packages/tinacms/src/tina-cms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ export const TinaCMSProvider2 = ({
)
}
const apiURL = props?.client?.apiUrl || props?.apiURL
const isLocalOverride = schema?.config?.admin?.auth?.useLocalAuth
const { branch, clientId, isLocalClient } = apiURL
? parseURL(apiURL)
: {
Expand Down Expand Up @@ -180,7 +179,7 @@ export const TinaCMSProvider2 = ({
branch={branch}
clientId={clientId || schema?.config?.clientId}
tinaioConfig={props.tinaioConfig}
isLocalClient={isLocalOverride || isLocalClient}
isLocalClient={isLocalClient}
isSelfHosted={!!schema?.config?.contentApiUrlOverride}
cmsCallback={props.cmsCallback}
mediaStore={props.mediaStore}
Expand Down

0 comments on commit 3903b86

Please sign in to comment.