oauth-dummy
This commit is contained in:
parent
80a3dd5d57
commit
070e0113ef
|
@ -9,25 +9,20 @@ type Provider = 'facebook' | 'google' | 'github' // 'vk' | 'telegram'
|
||||||
export const SocialProviders = () => {
|
export const SocialProviders = () => {
|
||||||
const { t } = useLocalize()
|
const { t } = useLocalize()
|
||||||
const {
|
const {
|
||||||
actions: { oauthLogin },
|
actions: { oauth },
|
||||||
} = useSession()
|
} = useSession()
|
||||||
|
|
||||||
const handleClick = async (event: MouseEvent | TouchEvent, provider: Provider): Promise<void> => {
|
|
||||||
event.preventDefault()
|
|
||||||
await oauthLogin(provider)
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.container}>
|
<div className={styles.container}>
|
||||||
<div className={styles.text}>{t('or sign in with social networks')}</div>
|
<div className={styles.text}>{t('or sign in with social networks')}</div>
|
||||||
<div className={styles.social}>
|
<div className={styles.social}>
|
||||||
<a href="#" onClick={(ev) => handleClick(ev, 'google')}>
|
<a href="#" onClick={(_e) => oauth('google')}>
|
||||||
<Icon name={'google'} />
|
<Icon name={'google'} />
|
||||||
</a>
|
</a>
|
||||||
<a href="#" onClick={(ev) => handleClick(ev, 'facebook')}>
|
<a href="#" onClick={(_e) => oauth('facebook')}>
|
||||||
<Icon name={'facebook'} />
|
<Icon name={'facebook'} />
|
||||||
</a>
|
</a>
|
||||||
<a href="#" class={styles.githubAuth} onClick={(ev) => handleClick(ev, 'github')}>
|
<a href="#" class={styles.githubAuth} onClick={(_e) => oauth('github')}>
|
||||||
<Icon name="github" />
|
<Icon name="github" />
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -10,6 +10,7 @@ import {
|
||||||
ConfigType,
|
ConfigType,
|
||||||
SignupInput,
|
SignupInput,
|
||||||
AuthorizeResponse,
|
AuthorizeResponse,
|
||||||
|
GraphqlQueryInput,
|
||||||
} from '@authorizerdev/authorizer-js'
|
} from '@authorizerdev/authorizer-js'
|
||||||
import {
|
import {
|
||||||
createContext,
|
createContext,
|
||||||
|
@ -61,7 +62,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>
|
oauth: (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
|
||||||
|
@ -89,10 +90,32 @@ export const SessionProvider = (props: {
|
||||||
actions: { showSnackbar },
|
actions: { showSnackbar },
|
||||||
} = useSnackbar()
|
} = useSnackbar()
|
||||||
const { searchParams, changeSearchParams } = useRouter()
|
const { searchParams, changeSearchParams } = useRouter()
|
||||||
|
const [configuration, setConfig] = createSignal<ConfigType>(defaultConfig)
|
||||||
|
const authorizer = createMemo(() => new Authorizer(configuration()))
|
||||||
|
|
||||||
|
createEffect(() => {
|
||||||
|
if (authorizer()) {
|
||||||
|
}
|
||||||
|
})
|
||||||
|
const [oauthState, setOauthState] = createSignal<string>()
|
||||||
// handle callback's redirect_uri
|
// handle callback's redirect_uri
|
||||||
createEffect(async () => {
|
createEffect(async () => {
|
||||||
// TODO: handle oauth here too
|
// oauth
|
||||||
|
const state = searchParams()?.state
|
||||||
|
if (state) {
|
||||||
|
setOauthState((_s) => state)
|
||||||
|
const scope = searchParams()?.scope
|
||||||
|
? searchParams()?.scope?.toString().split(' ')
|
||||||
|
: ['openid', 'profile', 'email']
|
||||||
|
if (scope) console.info(`[context.session] scope: ${scope}`)
|
||||||
|
const url = searchParams()?.redirect_uri || searchParams()?.redirectURL || window.location.href
|
||||||
|
setConfig((c: ConfigType) => ({ ...c, redirectURL: url })) // .split('?')[0]
|
||||||
|
changeSearchParams({ mode: 'confirm-email', modal: 'auth' }, true)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// handle email confirm
|
||||||
|
createEffect(() => {
|
||||||
const token = searchParams()?.token
|
const token = searchParams()?.token
|
||||||
const access_token = searchParams()?.access_token
|
const access_token = searchParams()?.access_token
|
||||||
if (access_token) changeSearchParams({ mode: 'confirm-email', modal: 'auth', access_token })
|
if (access_token) changeSearchParams({ mode: 'confirm-email', modal: 'auth', access_token })
|
||||||
|
@ -102,8 +125,6 @@ export const SessionProvider = (props: {
|
||||||
// load
|
// load
|
||||||
let minuteLater
|
let minuteLater
|
||||||
|
|
||||||
const [configuration, setConfig] = createSignal<ConfigType>(defaultConfig)
|
|
||||||
const authorizer = createMemo(() => new Authorizer(defaultConfig))
|
|
||||||
const [isSessionLoaded, setIsSessionLoaded] = createSignal(false)
|
const [isSessionLoaded, setIsSessionLoaded] = createSignal(false)
|
||||||
const [authError, setAuthError] = createSignal('')
|
const [authError, setAuthError] = createSignal('')
|
||||||
const [session, { refetch: loadSession, mutate: setSession }] = createResource<AuthToken>(
|
const [session, { refetch: loadSession, mutate: setSession }] = createResource<AuthToken>(
|
||||||
|
@ -246,13 +267,8 @@ export const SessionProvider = (props: {
|
||||||
}
|
}
|
||||||
|
|
||||||
// authorizer api proxy methods
|
// authorizer api proxy methods
|
||||||
|
const signUp = async (params: SignupInput) => {
|
||||||
const signUp = async (params: Partial<SignupInput>) => {
|
const authResult: void | AuthToken = await authorizer().signup(params)
|
||||||
const authResult: void | AuthToken = await authorizer().signup({
|
|
||||||
...params,
|
|
||||||
password: params.password,
|
|
||||||
confirm_password: params.password,
|
|
||||||
})
|
|
||||||
if (authResult) setSession(authResult)
|
if (authResult) setSession(authResult)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -283,13 +299,16 @@ export const SessionProvider = (props: {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const oauthLogin = async (oauthProvider: string) => {
|
const oauth = async (oauthProvider: string) => {
|
||||||
console.debug(`[context.session] calling authorizer's oauth for`)
|
console.debug(`[context.session] calling authorizer's oauth for`)
|
||||||
try {
|
try {
|
||||||
|
// const data: GraphqlQueryInput = {}
|
||||||
|
// await authorizer().graphqlQuery(data)
|
||||||
const ar: AuthorizeResponse | void = await authorizer().oauthLogin(
|
const ar: AuthorizeResponse | void = await authorizer().oauthLogin(
|
||||||
oauthProvider,
|
oauthProvider,
|
||||||
[],
|
[],
|
||||||
window.location.origin,
|
window.location.origin,
|
||||||
|
oauthState(),
|
||||||
)
|
)
|
||||||
console.debug(ar)
|
console.debug(ar)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -312,7 +331,7 @@ export const SessionProvider = (props: {
|
||||||
authorizer,
|
authorizer,
|
||||||
loadAuthor,
|
loadAuthor,
|
||||||
changePassword,
|
changePassword,
|
||||||
oauthLogin,
|
oauth,
|
||||||
}
|
}
|
||||||
const value: SessionContextType = {
|
const value: SessionContextType = {
|
||||||
authError,
|
authError,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user