From 80a3dd5d57de2991ef15944228314210d5e39208 Mon Sep 17 00:00:00 2001 From: Untone Date: Fri, 19 Jan 2024 18:03:33 +0300 Subject: [PATCH] social-providers-fix --- .../Nav/AuthModal/SocialProviders.tsx | 48 ++++++++----------- src/context/session.tsx | 21 +++++++- 2 files changed, 38 insertions(+), 31 deletions(-) diff --git a/src/components/Nav/AuthModal/SocialProviders.tsx b/src/components/Nav/AuthModal/SocialProviders.tsx index c29f91ff..39f5c717 100644 --- a/src/components/Nav/AuthModal/SocialProviders.tsx +++ b/src/components/Nav/AuthModal/SocialProviders.tsx @@ -1,43 +1,33 @@ import { useLocalize } from '../../../context/localize' -import { hideModal } from '../../../stores/ui' +import { useSession } from '../../../context/session' import { Icon } from '../../_shared/Icon' import styles from './SocialProviders.module.scss' -type Provider = 'facebook' | 'google' | 'vk' | 'github' - -// 3rd party provider auth handler -const handleSocialAuthLinkClick = (event: MouseEvent, provider: Provider): void => { - event.preventDefault() - const popup = window.open( - `https://auth.discours.io/oauth_login/${provider}`, - provider, - 'width=740, height=420', - ) // TODO: precalculate window size - popup?.focus() - hideModal() -} +type Provider = 'facebook' | 'google' | 'github' // 'vk' | 'telegram' export const SocialProviders = () => { const { t } = useLocalize() + const { + actions: { oauthLogin }, + } = useSession() + + const handleClick = async (event: MouseEvent | TouchEvent, provider: Provider): Promise => { + event.preventDefault() + await oauthLogin(provider) + } + return ( -
-
{t('or sign in with social networks')}
-
- handleSocialAuthLinkClick(event, 'facebook')}> - +
+
{t('or sign in with social networks')}
+
diff --git a/src/context/session.tsx b/src/context/session.tsx index 7e477e1c..2dbb7e6c 100644 --- a/src/context/session.tsx +++ b/src/context/session.tsx @@ -9,6 +9,7 @@ import { Authorizer, ConfigType, SignupInput, + AuthorizeResponse, } from '@authorizerdev/authorizer-js' import { createContext, @@ -34,8 +35,8 @@ import { useSnackbar } from './snackbar' const defaultConfig: ConfigType = { authorizerURL: 'https://auth.discours.io', - redirectURL: 'https://discoursio-webapp.vercel.app', - clientID: '9c113377-5eea-4c89-98e1-69302462fc08', // FIXME: use env? + redirectURL: 'https://testing.discours.io', + clientID: 'b9038a34-ca59-41ae-a105-c7fbea603e24', // FIXME: use env? } export type SessionContextType = { @@ -60,6 +61,7 @@ export type SessionContextType = { signUp: (params: SignupInput) => Promise signIn: (params: LoginInput) => Promise signOut: () => Promise + oauthLogin: (provider: string) => Promise changePassword: (password: string, token: string) => void confirmEmail: (input: VerifyEmailInput) => Promise // email confirm callback is in auth.discours.io setIsSessionLoaded: (loaded: boolean) => void @@ -281,6 +283,20 @@ export const SessionProvider = (props: { } } + const oauthLogin = async (oauthProvider: string) => { + console.debug(`[context.session] calling authorizer's oauth for`) + try { + const ar: AuthorizeResponse | void = await authorizer().oauthLogin( + oauthProvider, + [], + window.location.origin, + ) + console.debug(ar) + } catch (error) { + console.warn(error) + } + } + const isAuthenticated = createMemo(() => Boolean(author())) const actions = { loadSession, @@ -296,6 +312,7 @@ export const SessionProvider = (props: { authorizer, loadAuthor, changePassword, + oauthLogin, } const value: SessionContextType = { authError,