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

GitHub OAuth #10

Closed
intfract opened this issue Jul 20, 2023 · 2 comments
Closed

GitHub OAuth #10

intfract opened this issue Jul 20, 2023 · 2 comments
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed

Comments

@intfract
Copy link
Owner

Bug

The application is unable to retrieve user data after GitHub OAuth redirect. No errors are thrown in the console. No request parameters are visible.

Code

Snippet

oAuth: async (provider: string, redirectURL: string) => {
  sdk.account.createOAuth2Session(provider, redirectURL, redirectURL)
  const user = await sdk.account.get() // doesn't work
  state.init(user) // nothing happens
}
@intfract intfract added bug Something isn't working help wanted Extra attention is needed good first issue Good for newcomers labels Jul 20, 2023
@bitlogist
Copy link
Collaborator

Recommendation

It might be possible to fix the issue by checking for URL query parameters.

Code

Snippet

const createState = () => {
  const { subscribe, set } = writable<State>({
    account: null,
  })

  return {
    subscribe,
    signUp: async (email: string, password: string, name: string) => {
      return await sdk.account.create('unique()', email, password, name)
    },
    signIn: async (email: string, password: string) => {
      await sdk.account.createEmailSession(email, password)
      const user = await sdk.account.get()
      state.init(user)
    },
    signOut: async () => {
      state.init(null)
      await sdk.account.deleteSession('current')
    },
    init: async (account: Models.User<Models.Preferences> | null = null) => {
      set({ account })
    },
    oAuth: async (provider: string, redirectURL: string) => {
      sdk.account.createOAuth2Session(provider, redirectURL)

      const urlParams = new URLSearchParams(window.location.search)
      const code = urlParams.get('code')

      const response = await fetch('/api/github-oauth', {
        method: 'POST',
        body: JSON.stringify({ code }), 

        headers: {
          'Content-Type': 'application/json',
        },
      })

      if (response.ok) {
        const user = await response.json()
        state.init(user)
      }
    },
  }
}

@bitlogist bitlogist assigned bitlogist and unassigned bitlogist Jul 21, 2023
@bitlogist bitlogist pinned this issue Jul 21, 2023
@bitlogist
Copy link
Collaborator

bitlogist commented Jul 23, 2023

Solution

The above code does not work due to the way Appwrite redirects work. However, the Account API has an accountGetSession method available. The providerAccessToken should be accessible by a cookie or by the cookieFallback in local storage.

Code

  • Example
  • Independent
const session = await account.getSession(SESSION_ID)

@intfract intfract closed this as completed Sep 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants