Published: September 26 2020

Vue.js + Facebook - How to use the Facebook SDK in a Vue App

Other versions available:

This is a quick post to go through the steps to load, initialize and use the Facebook JS SDK in a Vue.js application.

The below code snippets are from a Facebook login tutorial I posted yesterday, for the full tutorial and live demo of the code see Vue.js - Facebook Login Tutorial & Example.

Init the Facebook SDK in a Vue App Before Startup

The below initFacebookSdk() function runs before the Vue app starts up to load and initialize the Facebook SDK, and automatically log the user into the Vue app if they are already logged in with Facebook.

The init Facebook SDK function is called from the main.js file which waits for the returned Promise to be resolved before starting the Vue app.

import { accountService } from '@/_services';

const facebookAppId = process.env.VUE_APP_FACEBOOK_APP_ID;

export function initFacebookSdk() {
    return new Promise(resolve => {
        // wait for facebook sdk to initialize before starting the vue app
        window.fbAsyncInit = function () {
                appId: facebookAppId,
                cookie: true,
                xfbml: true,
                version: 'v8.0'

            // auto authenticate with the api if already logged in with facebook
            FB.getLoginStatus(({ authResponse }) => {
                if (authResponse) {
                } else {

        // load facebook sdk script
        (function (d, s, id) {
            var js, fjs = d.getElementsByTagName(s)[0];
            if (d.getElementById(id)) { return; }
            js = d.createElement(s); = id;
            js.src = "";
            fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));    

Example Vue Main.js Startup File

The main.js file from the example project bootstraps the Vue application by mounting the App component in the #app div element defined in the main index html file.

Before starting the Vue app it performs a number of pre-startup tasks, including waiting for the Facebook SDK to load and initialize on line 18.

import Vue from 'vue'

// global stylesheet
import './styles.less';

// setup fake backend
import { fakeBackend } from './_helpers';

import { initFacebookSdk, jwtInterceptor, errorInterceptor, router } from './_helpers';
import App from './App.vue';

// enable interceptors for http requests

// wait for facebook sdk to start app

function startApp() {
    new Vue({
        render: h => h(App),

Use the Facebook SDK in your Vue App!

You're now ready to use the Facebook SDK in your Vue app, as an example here is the account service (/src/_services/account.service.js) from the Facebook login example project, it uses some Facebook SDK auth methods on lines 23, 45 and 92.

import { BehaviorSubject } from 'rxjs';
import axios from 'axios';

import { router } from '@/_helpers';

const baseUrl = `${process.env.VUE_APP_API_URL}/accounts`;
const accountSubject = new BehaviorSubject(null);

export const accountService = {
    delete: _delete,
    account: accountSubject.asObservable(),
    get accountValue () { return accountSubject.value; }

async function login() {
    // login with facebook then authenticate with the API to get a JWT auth token
    const { authResponse } = await new Promise(FB.login);
    if (!authResponse) return;

    await apiAuthenticate(authResponse.accessToken);

    // get return url from query parameters or default to home page
    const returnUrl = router.history.current.query['returnUrl'] || '/';

async function apiAuthenticate(accessToken) {
    // authenticate with the api using a facebook access token,
    // on success the api returns an account object with a JWT auth token
    const response = await`${baseUrl}/authenticate`, { accessToken });
    const account =;;
    return account;

function logout() {
    // revoke app permissions to logout completely because FB.logout() doesn't remove FB cookie
    FB.api('/me/permissions', 'delete', null, () => FB.logout());

function getAll() {
    return axios.get(baseUrl)
        .then(response =>;

function getById(id) {
    return axios.get(`${baseUrl}/${id}`)
        .then(response =>;

async function update(id, params) {
    const response = await axios.put(`${baseUrl}/${id}`, params);
    let account =;
    // update the current account if it was updated
    if ( === accountSubject.value?.id) {
        // publish updated account to subscribers
        account = { ...accountSubject.value, ...account };;
    return account;

async function _delete(id) {
    await axios.delete(`${baseUrl}/${id}`);
    if (id === accountSubject.value?.id) {
        // auto logout if the logged in account was deleted

// helper methods

let authenticateTimeout;

function startAuthenticateTimer() {
    // parse json object from base64 encoded jwt token
    const jwtToken = JSON.parse(atob(accountSubject.value.token.split('.')[1]));

    // set a timeout to re-authenticate with the api one minute before the token expires
    const expires = new Date(jwtToken.exp * 1000);
    const timeout = expires.getTime() - - (60 * 1000);
    const { accessToken } = FB.getAuthResponse();
    authenticateTimeout = setTimeout(() => apiAuthenticate(accessToken), timeout);

function stopAuthenticateTimer() {
    // cancel timer for re-authenticating with the api


Need Some Facebook Help?

Search fiverr for freelance Facebook developers.

Follow me for updates

On Twitter or RSS.

When I'm not coding...

Me and Tina are on a motorcycle adventure around Australia.
Come along for the ride!


Supported by