Skip to content

Commit

Permalink
#update: fixed bug pretent click when close window by user
Browse files Browse the repository at this point in the history
- get info user on google method (new mechanism)
  • Loading branch information
cuongdevjs committed Jun 26, 2022
1 parent a798586 commit f8eee2d
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 226 deletions.
1 change: 1 addition & 0 deletions example/src/User.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const User = memo(({ provider, profile, onLogout }: Props) => {
const avatar =
profile?.avatar ||
profile?.avatar_url ||
profile?.picture ||
profile?.picture?.data?.url ||
profile?.profile_image_url_https ||
'https://maxcdn.icons8.com/Share/icon/p1em/users//gender_neutral_user1600.png'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reactjs-social-login",
"version": "2.1.0",
"version": "2.1.2",
"description": "Group social login for ReactJS",
"author": "Nguyen Manh Cuong ",
"license": "MIT",
Expand Down
15 changes: 5 additions & 10 deletions src/LoginSocialAmazon/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export const LoginSocialAmazon = forwardRef(
) => {
const [isLogged, setIsLogged] = useState(false)
const [isSdkLoaded, setIsSdkLoaded] = useState(false)
const [isProcessing, setIsProcessing] = useState(false)

useEffect(() => {
!isSdkLoaded && load()
Expand Down Expand Up @@ -103,20 +102,19 @@ export const LoginSocialAmazon = forwardRef(
onReject(err)
})
.finally(() => {
setIsProcessing(false)
setIsLogged(true)
})
},
[onReject, onResolve]
)

const handleResponse = useCallback((res: objectType) => getUserInfo(res), [
getUserInfo
])
const handleResponse = useCallback(
(res: objectType) => getUserInfo(res),
[getUserInfo]
)

const handleError = useCallback(
(err: objectType | string) => {
setIsProcessing(false)
onReject(err)
},
[onReject]
Expand All @@ -134,10 +132,8 @@ export const LoginSocialAmazon = forwardRef(
}, [checkIsExistsSDKScript, client_id, insertScriptGoogle])

const onLogin = useCallback(() => {
if (isProcessing || !isSdkLoaded) return
setIsProcessing(true)
if (!isSdkLoaded) return
if (!_window.amazon) {
setIsProcessing(false)
load()
onReject("Google SDK isn't loaded!")
} else {
Expand All @@ -151,7 +147,6 @@ export const LoginSocialAmazon = forwardRef(
)
}
}, [
isProcessing,
isSdkLoaded,
load,
onReject,
Expand Down
9 changes: 3 additions & 6 deletions src/LoginSocialFacebook/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ const LoginSocialFacebook = forwardRef(
) => {
const [isLogged, setIsLogged] = useState(false)
const [isSdkLoaded, setIsSdkLoaded] = useState(false)
const [isProcessing, setIsProcessing] = useState(false)

useEffect(() => {
!isSdkLoaded && load()
Expand Down Expand Up @@ -115,7 +114,7 @@ const LoginSocialFacebook = forwardRef(
{ locale: language, fields: fieldsProfile },
(me: any) => {
setIsLogged(true)
setIsProcessing(false)

onResolve({
provider: 'facebook',
data: { ...authResponse, ...me }
Expand Down Expand Up @@ -170,10 +169,9 @@ const LoginSocialFacebook = forwardRef(
])

const loginFB = useCallback(() => {
if (isProcessing || !isSdkLoaded) return
setIsProcessing(true)
if (!isSdkLoaded) return

if (!_window.FB) {
setIsProcessing(false)
load()
onReject("Fb isn't loaded!")
} else {
Expand All @@ -185,7 +183,6 @@ const LoginSocialFacebook = forwardRef(
})
}
}, [
isProcessing,
isSdkLoaded,
load,
onReject,
Expand Down
51 changes: 21 additions & 30 deletions src/LoginSocialGithub/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export const LoginSocialGithub = forwardRef(
ref: React.Ref<TypeCrossFunction>
) => {
const [isLogged, setIsLogged] = useState(false)
const [isProcessing, setIsProcessing] = useState(false)

useEffect(() => {
const popupWindowURL = new URL(window.location.href)
Expand All @@ -76,11 +75,9 @@ export const LoginSocialGithub = forwardRef(
.then((res) => res.json())
.then((response: any) => {
setIsLogged(true)
setIsProcessing(false)
onResolve({ provider: 'github', data: { ...response, ...data } })
})
.catch((err) => {
setIsProcessing(false)
onReject(err)
})
},
Expand Down Expand Up @@ -108,7 +105,6 @@ export const LoginSocialGithub = forwardRef(
})
.then((response) => response.text())
.then((response) => {
setIsProcessing(false)
const data: objectType = {}
const searchParams: any = new URLSearchParams(response)
for (const p of searchParams) {
Expand All @@ -118,7 +114,6 @@ export const LoginSocialGithub = forwardRef(
else onReject('no data')
})
.catch((err) => {
setIsProcessing(false)
onReject(err)
})
},
Expand All @@ -138,38 +133,34 @@ export const LoginSocialGithub = forwardRef(
window.removeEventListener('storage', onChangeLocalStorage, false)
const code = localStorage.getItem('github')
if (code) {
setIsProcessing(true)
handlePostMessage({ provider: 'github', type: 'code', code })
localStorage.removeItem('instagram')
}
}, [handlePostMessage])

const onLogin = useCallback(() => {
if (!isProcessing) {
onLoginStart && onLoginStart()
window.addEventListener('storage', onChangeLocalStorage, false)
const oauthUrl = `${GITHUB_URL}/login/oauth/authorize?client_id=${client_id}&scope=${scope}&state=${
state + '_github'
}&redirect_uri=${redirect_uri}&allow_signup=${allow_signup}`
const width = 450
const height = 730
const left = window.screen.width / 2 - width / 2
const top = window.screen.height / 2 - height / 2
window.open(
oauthUrl,
'Github',
'menubar=no,location=no,resizable=no,scrollbars=no,status=no, width=' +
width +
', height=' +
height +
', top=' +
top +
', left=' +
left
)
}
onLoginStart && onLoginStart()
window.addEventListener('storage', onChangeLocalStorage, false)
const oauthUrl = `${GITHUB_URL}/login/oauth/authorize?client_id=${client_id}&scope=${scope}&state=${
state + '_github'
}&redirect_uri=${redirect_uri}&allow_signup=${allow_signup}`
const width = 450
const height = 730
const left = window.screen.width / 2 - width / 2
const top = window.screen.height / 2 - height / 2
window.open(
oauthUrl,
'Github',
'menubar=no,location=no,resizable=no,scrollbars=no,status=no, width=' +
width +
', height=' +
height +
', top=' +
top +
', left=' +
left
)
}, [
isProcessing,
onLoginStart,
onChangeLocalStorage,
client_id,
Expand Down
58 changes: 38 additions & 20 deletions src/LoginSocialGoogle/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ interface Props {
// const JS_SRC = 'https://apis.google.com/js/api.js'
const JS_SRC = 'https://accounts.google.com/gsi/client'
const SCRIPT_ID = 'google-login'
const PREVENT_CORS_URL: string = 'https://cors.bridged.cc'
const _window = window as any

const LoginSocialGoogle = forwardRef(
(
{
client_id,
scope = 'email profile',
scope = 'https://www.googleapis.com/auth/userinfo.profile',
prompt = 'select_account',
ux_mode,
className = '',
Expand All @@ -67,7 +68,6 @@ const LoginSocialGoogle = forwardRef(
) => {
const [isLogged, setIsLogged] = useState(false)
const [isSdkLoaded, setIsSdkLoaded] = useState(false)
const [isProcessing, setIsProcessing] = useState(false)
const [instance, setInstance] = useState<any>(null!)
const [token, setToken] = useState<string>('')

Expand Down Expand Up @@ -105,22 +105,42 @@ const LoginSocialGoogle = forwardRef(
const handleResponse = useCallback(
(res: objectType) => {
setIsLogged(true)
setIsProcessing(false)
setToken(res?.access_token)
// _window.google.accounts.id.initialize({
// client_id,
// auto_select: true,
// callback: (value: any) =>
// console.log('🚀 ~ file: index.tsx ~ line 113 ~ value', value)
// })
// _window.google.accounts.id.prompt()
const data: objectType = res
onResolve({
provider: 'google',
data
})

if (res?.access_token) {
const headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded',
'x-cors-grida-api-key': '875c0462-6309-4ddf-9889-5227b1acc82c',
Authorization: 'Bearer ' + res.access_token
})

fetch(
`${PREVENT_CORS_URL}/https://www.googleapis.com/oauth2/v1/userinfo?alt=json`,
{
method: 'GET',
headers
}
)
.then((response) => response.json())
.then((response) => {
const data: objectType = { ...res, ...response }
onResolve({
provider: 'google',
data
})
})
.catch((err) => {
onReject(err)
})
} else {
const data: objectType = res
onResolve({
provider: 'google',
data
})
}
},
[onResolve]
[onReject, onResolve]
)

const load = useCallback(() => {
Expand Down Expand Up @@ -168,17 +188,15 @@ const LoginSocialGoogle = forwardRef(
])

const loginGoogle = useCallback(() => {
if (isProcessing || !isSdkLoaded) return
setIsProcessing(true)
if (!isSdkLoaded) return
if (!_window.google) {
setIsProcessing(false)
load()
onReject("Google SDK isn't loaded!")
} else {
onLoginStart && onLoginStart()
if (instance) instance.requestAccessToken()
}
}, [instance, isProcessing, isSdkLoaded, load, onLoginStart, onReject])
}, [instance, isSdkLoaded, load, onLoginStart, onReject])

useImperativeHandle(ref, () => ({
onLogout: () => {
Expand Down
53 changes: 22 additions & 31 deletions src/LoginSocialInstagram/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export const LoginSocialInstagram = forwardRef(
ref: React.Ref<TypeCrossFunction>
) => {
const [isLogged, setIsLogged] = useState(false)
const [isProcessing, setIsProcessing] = useState(false)

useEffect(() => {
const popupWindowURL = new URL(window.location.href)
Expand All @@ -78,11 +77,9 @@ export const LoginSocialInstagram = forwardRef(
.then((res) => res.json())
.then((res) => {
setIsLogged(true)
setIsProcessing(false)
onResolve({ provider: 'instagram', data: { ...res, ...data } })
})
.catch((err) => {
setIsProcessing(false)
onReject(err)
})
},
Expand Down Expand Up @@ -115,9 +112,7 @@ export const LoginSocialInstagram = forwardRef(
.catch((err) => {
onReject(err)
})
.finally(() => {
setIsProcessing(false)
})
.finally(() => {})
},
[client_id, client_secret, getProfile, onReject, redirect_uri]
)
Expand All @@ -135,38 +130,34 @@ export const LoginSocialInstagram = forwardRef(
window.removeEventListener('storage', onChangeLocalStorage, false)
const code = localStorage.getItem('instagram')
if (code) {
setIsProcessing(true)
handlePostMessage({ provider: 'instagram', type: 'code', code })
localStorage.removeItem('instagram')
}
}, [handlePostMessage])

const onLogin = useCallback(() => {
if (!isProcessing) {
onLoginStart && onLoginStart()
window.addEventListener('storage', onChangeLocalStorage, false)
const oauthUrl = `${INSTAGRAM_URL}/oauth/authorize?response_type=${response_type}&client_id=${client_id}&scope=${scope}&state=${
state + '_instagram'
}&redirect_uri=${redirect_uri}`
const width = 450
const height = 730
const left = window.screen.width / 2 - width / 2
const top = window.screen.height / 2 - height / 2
window.open(
oauthUrl,
'Instagram',
'menubar=no,location=no,resizable=no,scrollbars=no,status=no, width=' +
width +
', height=' +
height +
', top=' +
top +
', left=' +
left
)
}
onLoginStart && onLoginStart()
window.addEventListener('storage', onChangeLocalStorage, false)
const oauthUrl = `${INSTAGRAM_URL}/oauth/authorize?response_type=${response_type}&client_id=${client_id}&scope=${scope}&state=${
state + '_instagram'
}&redirect_uri=${redirect_uri}`
const width = 450
const height = 730
const left = window.screen.width / 2 - width / 2
const top = window.screen.height / 2 - height / 2
window.open(
oauthUrl,
'Instagram',
'menubar=no,location=no,resizable=no,scrollbars=no,status=no, width=' +
width +
', height=' +
height +
', top=' +
top +
', left=' +
left
)
}, [
isProcessing,
onLoginStart,
onChangeLocalStorage,
response_type,
Expand Down
Loading

0 comments on commit f8eee2d

Please sign in to comment.