From b4741a8e75d3b3f97b3ee74495e3e2b593d0587b Mon Sep 17 00:00:00 2001 From: Hailey Date: Thu, 4 Apr 2024 16:53:12 -0700 Subject: [PATCH] change --- src/lib/oauth.ts | 19 +++++++++++++++++++ src/screens/Login/hooks/useLogin.ts | 11 +++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 src/lib/oauth.ts diff --git a/src/lib/oauth.ts b/src/lib/oauth.ts new file mode 100644 index 0000000000..3843dfbf6a --- /dev/null +++ b/src/lib/oauth.ts @@ -0,0 +1,19 @@ +import {isWeb} from 'platform/detection' + +export const OAUTH_CLIENT_ID = 'https://bsky.app' +export const OAUTH_REDIRECT_URI = 'https://bsky.app/auth/callback' +export const OAUTH_SCOPE = 'openid profile email phone offline_access' +export const OAUTH_GRANT_TYPES = ['authorization_code', 'refresh_token'] +export const OAUTH_RESPONSE_TYPES = ['code', 'code id_token'] +export const DPOP_BOUND_ACCESS_TOKENS = true +export const APPLICATION_TYPE = isWeb ? 'web' : 'native' // TODO what should we put here for native + +export const buildOAuthUrl = (serviceUrl: string, state: string) => { + const url = new URL(serviceUrl) + url.searchParams.set('client_id', OAUTH_CLIENT_ID) + url.searchParams.set('redirect_uri', OAUTH_REDIRECT_URI) + url.searchParams.set('response_type', OAUTH_RESPONSE_TYPES.join(' ')) + url.searchParams.set('scope', OAUTH_SCOPE) + url.searchParams.set('state', state) + return url.toString() +} diff --git a/src/screens/Login/hooks/useLogin.ts b/src/screens/Login/hooks/useLogin.ts index 954b0060f8..d798498ee3 100644 --- a/src/screens/Login/hooks/useLogin.ts +++ b/src/screens/Login/hooks/useLogin.ts @@ -1,14 +1,21 @@ import React from 'react' import * as Browser from 'expo-web-browser' +import {buildOAuthUrl, OAUTH_REDIRECT_URI} from 'lib/oauth' + +// TODO remove hack +const serviceUrl = 'http://localhost:2583/oauth/authorize' + // Service URL here is just a placeholder, this isn't how it will actually work export function useLogin(serviceUrl: string | undefined) { const openAuthSession = React.useCallback(async () => { if (!serviceUrl) return + const url = buildOAuthUrl(serviceUrl, '123') // TODO replace '123' with the appropriate state + const authSession = await Browser.openAuthSessionAsync( - serviceUrl, // This isn't actually how this will work - 'bsky://login', // Replace this as well with the appropriate link + url, // This isn't actually how this will work + OAUTH_REDIRECT_URI, // Replace this as well with the appropriate link ) if (authSession.type !== 'success') {