social-providers-fix

This commit is contained in:
Untone 2024-01-19 18:03:33 +03:00
parent f693cdefeb
commit 80a3dd5d57
2 changed files with 38 additions and 31 deletions

View File

@ -1,43 +1,33 @@
import { useLocalize } from '../../../context/localize' import { useLocalize } from '../../../context/localize'
import { hideModal } from '../../../stores/ui' import { useSession } from '../../../context/session'
import { Icon } from '../../_shared/Icon' import { Icon } from '../../_shared/Icon'
import styles from './SocialProviders.module.scss' import styles from './SocialProviders.module.scss'
type Provider = 'facebook' | 'google' | 'vk' | 'github' type Provider = 'facebook' | 'google' | 'github' // 'vk' | 'telegram'
// 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()
}
export const SocialProviders = () => { export const SocialProviders = () => {
const { t } = useLocalize() const { t } = useLocalize()
const {
actions: { oauthLogin },
} = useSession()
const handleClick = async (event: MouseEvent | TouchEvent, provider: Provider): Promise<void> => {
event.preventDefault()
await oauthLogin(provider)
}
return ( return (
<div class={styles.container}> <div className={styles.container}>
<div class={styles.text}>{t('or sign in with social networks')}</div> <div className={styles.text}>{t('or sign in with social networks')}</div>
<div class={styles.social}> <div className={styles.social}>
<a href="#" onClick={(event) => handleSocialAuthLinkClick(event, 'facebook')}> <a href="#" onClick={(ev) => handleClick(ev, 'google')}>
<Icon name="facebook" /> <Icon name={'google'} />
</a> </a>
<a href="#" onClick={(event) => handleSocialAuthLinkClick(event, 'google')}> <a href="#" onClick={(ev) => handleClick(ev, 'facebook')}>
<Icon name="google" /> <Icon name={'facebook'} />
</a> </a>
<a href="#" onClick={(event) => handleSocialAuthLinkClick(event, 'vk')}> <a href="#" class={styles.githubAuth} onClick={(ev) => handleClick(ev, 'github')}>
<Icon name="vk" />
</a>
<a
href="#"
class={styles.githubAuth}
onClick={(event) => handleSocialAuthLinkClick(event, 'github')}
>
<Icon name="github" /> <Icon name="github" />
</a> </a>
</div> </div>

View File

@ -9,6 +9,7 @@ import {
Authorizer, Authorizer,
ConfigType, ConfigType,
SignupInput, SignupInput,
AuthorizeResponse,
} from '@authorizerdev/authorizer-js' } from '@authorizerdev/authorizer-js'
import { import {
createContext, createContext,
@ -34,8 +35,8 @@ import { useSnackbar } from './snackbar'
const defaultConfig: ConfigType = { const defaultConfig: ConfigType = {
authorizerURL: 'https://auth.discours.io', authorizerURL: 'https://auth.discours.io',
redirectURL: 'https://discoursio-webapp.vercel.app', redirectURL: 'https://testing.discours.io',
clientID: '9c113377-5eea-4c89-98e1-69302462fc08', // FIXME: use env? clientID: 'b9038a34-ca59-41ae-a105-c7fbea603e24', // FIXME: use env?
} }
export type SessionContextType = { export type SessionContextType = {
@ -60,6 +61,7 @@ export type SessionContextType = {
signUp: (params: SignupInput) => Promise<AuthToken | void> signUp: (params: SignupInput) => Promise<AuthToken | void>
signIn: (params: LoginInput) => Promise<void> signIn: (params: LoginInput) => Promise<void>
signOut: () => Promise<void> signOut: () => Promise<void>
oauthLogin: (provider: string) => Promise<void>
changePassword: (password: string, token: string) => void changePassword: (password: string, token: string) => void
confirmEmail: (input: VerifyEmailInput) => Promise<AuthToken | void> // email confirm callback is in auth.discours.io confirmEmail: (input: VerifyEmailInput) => Promise<AuthToken | void> // email confirm callback is in auth.discours.io
setIsSessionLoaded: (loaded: boolean) => void 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 isAuthenticated = createMemo(() => Boolean(author()))
const actions = { const actions = {
loadSession, loadSession,
@ -296,6 +312,7 @@ export const SessionProvider = (props: {
authorizer, authorizer,
loadAuthor, loadAuthor,
changePassword, changePassword,
oauthLogin,
} }
const value: SessionContextType = { const value: SessionContextType = {
authError, authError,