<script>
init: () => {
window.fbAsyncInit = () => {
window.FB.init({
appId: 'appId',
autoLogAppEvents: true,
xfbml: true,
version: 'v15.0',
cookie: true
})
window.FB.AppEvents.logPageView()
}
actions.setSdk(document, 'script', 'facebook-jssdk')
},
setSdk: (d, s, id) => {
const js = d.createElement(s)
const fjs = d.getElementsByTagName(s)[0]
if (d.getElementById(id)) { return }
js.id = id
js.src = 'https://connect.facebook.net/ko_KR/sdk.js'
fjs.parentNode.insertBefore(js, fjs)
}
</script>
<script>
// 호출 코드
window.FB.getLoginStatus((response) => {}) // 로그인 상태 체크
window.FB.login(
response => {
// 상태가 connected 일때 원하는 정보 호출
if (response.status === 'connected') {
window.FB.api(
'/me',
{ fields: 'id,name,email,birthday,hometown,likes,events,photos,videos,friends,posts,link,age_range,fundraisers,groups,location,picture' },
res => {
if (!res) return
const setResult = {
...res,
social: 'Facebook'
}
resolve(setResult)
}
)
} else {
alert('걍 로그인하게~')
}
},
{ scope: 'public_profile, email' }
)
// 페이스북 지원 버튼 호출 방법
window.login = async () => {
await actions.getInfo()
}
</script>
<template>
<button
type="button"
@click="actions.logout"
>
LOGOUT
</button>
<div
class="fb-login-button"
data-max-rows="1"
data-size="large"
data-button-type="login_with"
data-show-faces="false"
data-auto-logout-link="false"
data-use-continue-as="false"
data-onlogin="login()"
/>
</template>
<script setup>
const baseData = reactive({
appId: config.public.APPID_FB
})
window.login = async () => {
await actions.getInfo()
}
const actions = {
init: () => {
console.log(baseData.appId)
window.fbAsyncInit = () => {
window.FB.init({
appId: baseData.appId,
autoLogAppEvents: true,
xfbml: true,
version: 'v15.0',
cookie: true
})
window.FB.AppEvents.logPageView()
}
actions.setSdk(document, 'script', 'facebook-jssdk')
},
setSdk: (d, s, id) => {
const js = d.createElement(s)
const fjs = d.getElementsByTagName(s)[0]
if (d.getElementById(id)) { return }
js.id = id
js.src = 'https://connect.facebook.net/ko_KR/sdk.js'
fjs.parentNode.insertBefore(js, fjs)
},
login: async () => {
const res = await actions.getInfo()
console.log('login', res)
},
getInfo: () => {
return new Promise((resolve, reject) => {
window.FB.getLoginStatus((response) => {
window.FB.login(
response => {
console.log(response)
if (response.status === 'connected') {
window.FB.api(
'/me',
{ fields: 'id,name,email,birthday,hometown,likes,events,photos,videos,friends,posts,link,age_range,fundraisers,groups,location,picture' },
res => {
if (!res) return
const setResult = {
...res,
social: 'Facebook'
}
resolve(setResult)
}
)
} else {
alert('걍 로그인하게~')
}
},
{ scope: 'public_profile, email' }
)
})
})
}
}
onMounted(() => {
actions.init()
})
</script>
처음엔 nuxt니까 nuxt.config.js head에 넣어야 되는 줄 알고 head에 넣었는데 되는 듯 안됨
페이스북 자체 로그인 버튼이 안나왔다
그래서 페이지에 넣어주니 잘됐다...
오늘도삽질잘했다ㅎ
(nuxt.config.js head에 넣어도 커스텀 버튼은 잘 작동했다)
<script>
app: {
head: {
viewport: 'width=device-width, initial-scale=1.0',
title: 'study'
script: [
{ src: `https://connect.facebook.net/ko_KR/sdk.js#xfbml=1&version=v15.0&appId=${process.env.VITE_APPID_FB}&autoLogAppEvents=1`, async: true, defer: true, crossorigin: 'anonymous', nonce: 'wBJjpBHM', body: true }
]
}
}
</script>