0.2.0-alpha.0 • Published 5 years ago

react-tinacms-github v0.2.0-alpha.0

Weekly downloads
812
License
Apache-2.0
Repository
-
Last release
5 years ago

react-tinacms-github

This package provides helpers for setting up TinaCMS to use the Github API, with Github authentication.

Installation

npm install --save react-tinacms-github

or

yarn add react-tinacms-github

Getting Started

Register the GithubClient

We will want to use the GithubClient to load/save our content using the Github API. Let's add it as an API plugin.

import { TinaCMS } from 'tinacms'
import { GithubClient } from 'react-tinacms-github'

const cms = new TinaCMS({
  apis: {
    github: new GithubClient({
      proxy: '/api/proxy-github',
      authCallbackRoute: '/api/create-github-access-token'
      clientId: process.env.GITHUB_CLIENT_ID,
      baseRepoFullName: process.env.REPO_FULL_NAME // e.g: tinacms/tinacms.org,
    })
  }
})

Managing "edit-mode" state

Add the root TinacmsGithubProvider component to our main layout. We will supply it with handlers for authenticating and entering/exiting edit-mode. In this case, we will hit our /api server functions.

// YourLayout.ts
import { TinacmsGithubProvider } from 'react-tinacms-github';

const enterEditMode = () => {
  return fetch(`/api/preview`).then(() => {
    window.location.href = window.location.pathname
  })
}

const exitEditMode = () => {
  return fetch(`/api/reset-preview`).then(() => {
    window.location.reload()
  })
}

const YourLayout = ({ editMode, error, children }) => {
  return (
    <TinacmsGithubProvider
      editMode={editMode}
      enterEditMode={enterEditMode}
      exitEditMode={exitEditMode}
      error={error}>
      {children}
    </TinacmsGithubProvider>
  )
}

Auth Redirects

We will also need a few Github Specific pages to redirect the user to while authenticating with Github

//pages/github/authorizing.tsx
// Our Github app redirects back to this page with auth code
import { useGithubAuthRedirect } from 'react-tinacms-github'

export default function Authorizing() {
  // Let the main app know, that we receieved an auth code from the Github redirect
  useGithubAuthRedirect()
  return (
      <h2>Authorizing with Github, Please wait...</h2>
  )
}

Entering / Exiting "edit-mode"

We will need a way to enter/exit mode from our site. Let's create an "Edit Link" button. Ours will take isEditing as a parameter.

If you are using Next.js's preview-mode for the editing environment, this isEditing value might get sent from your getStaticProps function.

//...EditLink.tsx
import { useGithubEditing } from 'react-tinacms-github'

export interface EditLinkProps {
  isEditing: boolean
}

export const EditLink = ({ isEditing }: EditLinkProps) => {
  const github = useGithubEditing()

  return (
    <button
      onClick={
        isEditing ? github.exitEditMode : github.enterEditMode
      }
    >
      {isEditing ? 'Exit Edit Mode' : 'Edit This Site'}
    </button>
  )
}

Github Oauth App:

In GitHub, within your account Settings, click Oauth Apps under Developer Settings.

click "New Oauth App".

For the Authorization callback URL, enter the url for the "authorizing" page that you created above (e.g https://your-url/github/authorizing). Fill out the other fields with your custom values.

The generated Client ID will be used in your site (remember, we passed this value into the Github authenticate method earlier).

The Client Secret will likely be used by your backend.

Using Github Forms

Any forms that we have on our site can be created with the useGithubJsonForm or useGithubMarkdownForm helpers

function BlogTemplate({ jsonFile }) {
  const formOptions = {
    label: 'Blog Post',
    fields: [],
  }

  // Registers a JSON Tina Form
  const [data, form] = useGithubJsonForm(jsonFile, formOptions)

  // ...
}

useGithubJsonForm will use the GithubClient api that we registered earlier.

Next steps

Now that we have configured our front-end to use Github, we will need to setup some backend functions to handle authentication. If you are using Nextjs, you may want to use the next-tinacms-github package.

0.51.11

4 years ago

0.51.12

4 years ago

0.51.13

4 years ago

0.51.14

4 years ago

0.51.10

4 years ago

0.51.15

4 years ago

0.51.16

4 years ago

0.51.17

4 years ago

0.51.9

4 years ago

0.52.0

4 years ago

0.51.8

4 years ago

0.51.6

4 years ago

0.51.7

4 years ago

0.51.5

4 years ago

0.0.0-2021103118

4 years ago

0.51.4

4 years ago

0.51.3

4 years ago

0.51.2

4 years ago

0.51.0

4 years ago

0.51.1

4 years ago

0.50.7

4 years ago

0.50.8

4 years ago

0.50.6

4 years ago

0.50.5

4 years ago

0.50.4

4 years ago

0.50.3

4 years ago

0.50.2

4 years ago

0.50.1

4 years ago

0.50.0

4 years ago

0.43.3

4 years ago

0.43.1-beta

4 years ago

0.43.1

4 years ago

0.43.0

4 years ago

0.43.0-alpha.1

4 years ago

0.42.0

4 years ago

0.42.1

4 years ago

0.42.0-alpha.1

4 years ago

0.42.0-alpha.0

4 years ago

0.41.2-alpha.0

4 years ago

0.41.1

4 years ago

0.41.1-alpha.1

4 years ago

0.41.1-alpha.0

4 years ago

0.41.0

4 years ago

0.40.1-alpha.0

4 years ago

0.40.1

4 years ago

0.41.0-alpha.0

4 years ago

0.40.0

4 years ago

0.40.0-alpha.0

4 years ago

0.39.0

4 years ago

0.39.0-alpha.1

4 years ago

0.38.0

4 years ago

0.39.0-alpha.0

4 years ago

0.38.0-alpha.0

4 years ago

0.37.0

5 years ago

0.37.0-alpha.0

5 years ago

0.36.1

5 years ago

0.36.0

5 years ago

0.36.0-alpha.0

5 years ago

0.35.1

5 years ago

0.35.0

5 years ago

0.35.0-alpha.4

5 years ago

0.35.0-alpha.3

5 years ago

0.35.0-alpha.2

5 years ago

0.35.0-alpha.1

5 years ago

0.35.0-alpha.0

5 years ago

0.34.0

5 years ago

0.34.0-alpha.1

5 years ago

0.34.0-alpha.0

5 years ago

0.33.0

5 years ago

0.33.0-alpha.2

5 years ago

0.33.0-alpha.1

5 years ago

0.33.0-alpha.0

5 years ago

0.32.0

5 years ago

0.32.0-alpha.2

5 years ago

0.32.0-alpha.1

5 years ago

0.31.0-alpha.6

5 years ago

0.31.0

5 years ago

0.31.0-alpha.5

5 years ago

0.31.0-alpha.4

5 years ago

0.31.0-alpha.3

5 years ago

0.31.0-alpha.2

5 years ago

0.31.0-alpha.0

5 years ago

0.31.0-alpha.1

5 years ago

0.30.0

5 years ago

0.30.0-alpha.0

5 years ago

0.29.0

5 years ago

0.28.0

5 years ago

0.28.0-alpha.1

5 years ago

0.28.0-alpha.0

5 years ago

0.27.1

5 years ago

0.27.0

5 years ago

0.27.0-alpha.0

5 years ago

0.26.0

5 years ago

0.24.0

5 years ago

0.7.0-alpha.0

5 years ago

0.6.0

5 years ago

0.6.0-alpha.0

5 years ago

0.5.1

5 years ago

0.5.1-alpha.1

5 years ago

0.5.1-alpha.0

5 years ago

0.5.0

5 years ago

0.5.0-alpha.0

5 years ago

0.4.3

5 years ago

0.4.3-alpha.1

5 years ago

0.4.3-alpha.0

5 years ago

0.4.2

5 years ago

0.4.1-alpha.3

5 years ago

0.4.1

5 years ago

0.4.1-alpha.2

5 years ago

0.4.1-alpha.1

5 years ago

0.4.1-alpha.0

5 years ago

0.4.0

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.3.0-alpha.2

5 years ago

0.3.0-alpha.3

5 years ago

0.3.0-alpha.1

5 years ago

0.3.0-alpha.0

5 years ago

0.2.0

5 years ago

0.2.0-alpha.0

5 years ago

0.1.0

5 years ago

0.1.0-canary3.2

5 years ago

0.1.0-canary3.1

5 years ago

0.1.0-canary3.0

5 years ago

0.0.1-canary3.2

5 years ago

0.0.1-canary3.1

5 years ago

0.0.1-canary3.0

5 years ago

0.0.1-canary3

5 years ago

0.0.1-canary2

5 years ago

0.0.1-canary

5 years ago