Last active
September 27, 2021 11:02
-
-
Save xtiannyeto/2cbd8a674a0a728d62a7396181c5fd3e to your computer and use it in GitHub Desktop.
Encapsulate facebook-sdk-wra
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface FaceBookResponse { | |
email: string; | |
name: string; | |
picture: string; | |
authResponse: any; | |
} | |
// import * as Facebook from 'fb-sdk-wrapper'; | |
// eslint-disable-next-line @typescript-eslint/no-var-requires | |
import * as Facebook from 'fb-sdk-wrapper'; | |
export async function initFacebook(appId: string | undefined): Promise<void> { | |
if (!appId) return; | |
await Facebook.load(); | |
return await Facebook.init({ | |
appId: appId | |
}); | |
} | |
export async function login(): Promise<FaceBookResponse | null> { | |
const response = await Facebook.login({ | |
scope: 'public_profile , email', | |
return_scopes: true | |
}); | |
if (response.status !== 'connected') { | |
return null; | |
} | |
const profile = await Facebook.api( | |
`/${response.authResponse.userID}`, | |
'get', | |
{ | |
fields: ['email', 'name', 'picture'] | |
} | |
); | |
const facebookResponse: FaceBookResponse = { | |
name: profile.name, | |
email: profile.email, | |
picture: profile.picture.data.url, | |
authResponse: response.authResponse | |
}; | |
return facebookResponse; | |
} | |
export async function logout(): Promise<void> { | |
return await Facebook.logout(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment