Skip to content

Commit b6523ff

Browse files
author
sw-yx
committed
tryloginprovider
1 parent bee1f88 commit b6523ff

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

example/src/App.tsx

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react"
22
import { Router, Link, navigate } from "@reach/router"
33
import "./App.css"
4-
import { useNetlifyIdentity } from "react-netlify-identity"
4+
import { useNetlifyIdentity, Settings } from "react-netlify-identity"
55
import useLoading from "./useLoading"
66

77
let IdentityContext = React.createContext<ReturnType<typeof useNetlifyIdentity>>(undefined!)
@@ -20,13 +20,14 @@ function PrivateRoute(props: React.PropsWithoutRef<MaybePathProps & { as: React.
2020
}
2121

2222
function Login({ }: MaybePathProps) {
23-
const { loginUser, signupUser, settings } = React.useContext(IdentityContext)
23+
const { loginUser, signupUser, settings, loginProvider } = React.useContext(IdentityContext)
2424
const formRef = React.useRef<HTMLFormElement>(null!)
2525
const [msg, setMsg] = React.useState("")
2626
const [isLoading, load] = useLoading()
27+
const [setting, setSetting] = React.useState<Settings | null>(null)
2728
React.useEffect(() => {
28-
settings().then((...args) => console.log({ args }))
29-
}, [settings])
29+
settings().then(x => setSetting(x))
30+
}, [])
3031
const signup = () => {
3132
const email = formRef.current.email.value
3233
const password = formRef.current.password.value
@@ -73,6 +74,8 @@ function Login({ }: MaybePathProps) {
7374
{msg && <pre>{msg}</pre>}
7475
</div>
7576
)}
77+
{setting && <pre>{JSON.stringify(setting, null, 2)}</pre>}
78+
{setting && setting.external.github && <div onClick={() => loginProvider("github")}>GitHub</div>}
7679
</form>
7780
)
7881
}

src/index.tsx

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "react"
22

3-
import GoTrue, { User } from "gotrue-js"
3+
import GoTrue, { User, Settings } from "gotrue-js"
44

55
type authChangeParam = (user?: User) => string | void
66

@@ -10,6 +10,7 @@ interface NIProps {
1010
onAuthChange?: authChangeParam
1111
}
1212

13+
export type Settings = Settings
1314
export type User = User
1415
export default function NetlifyIdentity({ children, domain, onAuthChange }: NIProps) {
1516
return children(useNetlifyIdentity(domain, onAuthChange))
@@ -34,10 +35,15 @@ export function useNetlifyIdentity(domain: string, onAuthChange: authChangeParam
3435
}
3536

3637
/******* external oauth */
37-
const loginExternalUrl = (provider: string) => goTrueInstance.loginExternalUrl(provider)
38-
const acceptInviteExternalUrl = (provider: string, token: string) =>
38+
type Provider = "bitbucket" | "facebook" | "github" | "gitlab" | "google"
39+
40+
const loginProvider = (provider: Provider) => {
41+
const url = goTrueInstance.loginExternalUrl(provider)
42+
if (window) window.location.href = url
43+
}
44+
const acceptInviteExternalUrl = (provider: Provider, token: string) =>
3945
goTrueInstance.acceptInviteExternalUrl(provider, token)
40-
const settings = goTrueInstance.settings.bind(goTrueInstance)
46+
const settings: () => Promise<Settings> = goTrueInstance.settings.bind(goTrueInstance)
4147

4248
/******* OPERATIONS */
4349
// make sure the Registration preferences under Identity settings in your Netlify dashboard are set to Open.
@@ -121,7 +127,7 @@ export function useNetlifyIdentity(domain: string, onAuthChange: authChangeParam
121127
authedFetch,
122128
_goTrueInstance: goTrueInstance,
123129
_domain: domain,
124-
loginExternalUrl,
130+
loginProvider,
125131
acceptInviteExternalUrl,
126132
settings
127133
}

0 commit comments

Comments
 (0)